upload page after uploading
This commit is contained in:
+23
-7
@@ -8,6 +8,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"mime"
|
"mime"
|
||||||
@@ -44,7 +45,7 @@ type UploadResponse struct {
|
|||||||
FileExtension string `json:"extension"` // useful for things like sharex to append the extension
|
FileExtension string `json:"extension"` // useful for things like sharex to append the extension
|
||||||
}
|
}
|
||||||
|
|
||||||
func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
func uploadHandler(db *gorm.DB, cfg Config, templates *template.Template) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
http.Error(w, "invalid method", http.StatusMethodNotAllowed)
|
http.Error(w, "invalid method", http.StatusMethodNotAllowed)
|
||||||
@@ -239,19 +240,27 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
saved = true
|
saved = true
|
||||||
|
|
||||||
|
response := UploadResponse{
|
||||||
|
Upload: upload,
|
||||||
|
SHA256: hex.EncodeToString(upload.SHA256),
|
||||||
|
DeleteToken: upload.DeleteToken,
|
||||||
|
FileExtension: filepath.Ext(upload.Filename),
|
||||||
|
}
|
||||||
|
|
||||||
switch returnType {
|
switch returnType {
|
||||||
// html & maybe xml? cant think of other return types
|
// html & maybe xml? cant think of other return types
|
||||||
|
case "html":
|
||||||
|
log.Println(upload)
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
if err := templates.ExecuteTemplate(w, "upload.html", response); err != nil {
|
||||||
|
log.Println("render upload page:", err)
|
||||||
|
}
|
||||||
case "redirect":
|
case "redirect":
|
||||||
http.Redirect(w, r, "/"+id, http.StatusSeeOther)
|
http.Redirect(w, r, "/"+id, http.StatusSeeOther)
|
||||||
default:
|
default:
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
if err := json.NewEncoder(w).Encode(UploadResponse{
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||||
Upload: upload,
|
|
||||||
SHA256: hex.EncodeToString(upload.SHA256),
|
|
||||||
DeleteToken: upload.DeleteToken,
|
|
||||||
FileExtension: filepath.Ext(upload.Filename),
|
|
||||||
}); err != nil {
|
|
||||||
log.Println("failed to encode response:", err)
|
log.Println("failed to encode response:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -312,6 +321,8 @@ func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
|||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
redirect := r.URL.Query().Get("redirect") == "true"
|
||||||
|
|
||||||
var token string
|
var token string
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodDelete:
|
case http.MethodDelete:
|
||||||
@@ -362,6 +373,11 @@ func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if redirect {
|
||||||
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteHeader(http.StatusNoContent)
|
w.WriteHeader(http.StatusNoContent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -27,7 +28,13 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pages, err := template.ParseFiles("templates/home.html", "templates/header.html")
|
pages, err := template.New("").Funcs(template.FuncMap{
|
||||||
|
"hasPrefix": strings.HasPrefix,
|
||||||
|
}).ParseFiles(
|
||||||
|
"templates/home.html",
|
||||||
|
"templates/header.html",
|
||||||
|
"templates/upload.html",
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@@ -49,7 +56,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
mux.HandleFunc("GET /{id}", contentHandler(db, config))
|
mux.HandleFunc("GET /{id}", contentHandler(db, config))
|
||||||
mux.HandleFunc("POST /upload", uploadHandler(db, config))
|
mux.HandleFunc("POST /upload", uploadHandler(db, config, pages))
|
||||||
mux.HandleFunc("DELETE /{id}", deleteHandler(db, config))
|
mux.HandleFunc("DELETE /{id}", deleteHandler(db, config))
|
||||||
mux.HandleFunc("POST /{id}/delete", deleteHandler(db, config))
|
mux.HandleFunc("POST /{id}/delete", deleteHandler(db, config))
|
||||||
mux.HandleFunc("GET /health", health)
|
mux.HandleFunc("GET /health", health)
|
||||||
|
|||||||
+23
-1
@@ -149,7 +149,7 @@ option {
|
|||||||
font: inherit;
|
font: inherit;
|
||||||
}
|
}
|
||||||
|
|
||||||
button[type="submit"] {
|
.btn {
|
||||||
border-radius: 0;
|
border-radius: 0;
|
||||||
border: none;
|
border: none;
|
||||||
background-color: var(--text);
|
background-color: var(--text);
|
||||||
@@ -158,3 +158,25 @@ button[type="submit"] {
|
|||||||
font: inherit;
|
font: inherit;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.link-btn {
|
||||||
|
border: none;
|
||||||
|
background: none;
|
||||||
|
padding: 0;
|
||||||
|
text-decoration: underline;
|
||||||
|
color: var(--text);
|
||||||
|
cursor: pointer;
|
||||||
|
font: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
.upload-links {
|
||||||
|
margin-top: 12px;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
gap: 12px;
|
||||||
|
align-items: baseline;
|
||||||
|
}
|
||||||
|
|
||||||
|
.upload-links form {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
const link = document.getElementById("copy-link");
|
||||||
|
|
||||||
|
if (link && navigator.clipboard?.writeText) {
|
||||||
|
link.style.display = "";
|
||||||
|
link.hidden = false;
|
||||||
|
link.onclick = async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(`${location.origin}/${link.dataset.id}`);
|
||||||
|
link.textContent = "copied";
|
||||||
|
setTimeout(() => {
|
||||||
|
link.textContent = "copy link";
|
||||||
|
}, 2000);
|
||||||
|
} catch (error) {
|
||||||
|
console.error("failed to copy link:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
{{define "site-header"}}
|
{{define "site-header"}}
|
||||||
<div class="site-header">
|
<div class="site-header">
|
||||||
<h1>minibin</h1>
|
<h1><a style="text-decoration: none;" href="/">minibin</a></h1>
|
||||||
<div>
|
<div>
|
||||||
<a href="/tos">tos</a>
|
<a href="/tos">tos</a>
|
||||||
<a href="/privacy">privacy</a>
|
<a href="/privacy">privacy</a>
|
||||||
|
|||||||
+4
-4
@@ -16,7 +16,7 @@
|
|||||||
<input type="radio" name="upload-type" id="type-paste" />
|
<input type="radio" name="upload-type" id="type-paste" />
|
||||||
<label for="type-paste">paste</label>
|
<label for="type-paste">paste</label>
|
||||||
<div class="file-panel">
|
<div class="file-panel">
|
||||||
<form action="/upload?return=redirect" method="post" enctype="multipart/form-data">
|
<form action="/upload?return=html" method="post" enctype="multipart/form-data">
|
||||||
<label for="file-expiry">expiry</label>
|
<label for="file-expiry">expiry</label>
|
||||||
<select id="paste-expiry" name="expiry">
|
<select id="paste-expiry" name="expiry">
|
||||||
{{range .Expiries}}
|
{{range .Expiries}}
|
||||||
@@ -24,11 +24,11 @@
|
|||||||
{{end}}
|
{{end}}
|
||||||
</select>
|
</select>
|
||||||
<input type="file" required name="file" />
|
<input type="file" required name="file" />
|
||||||
<button type="submit">upload file</button>
|
<button type="submit" class="btn">upload file</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<div class="paste-panel">
|
<div class="paste-panel">
|
||||||
<form action="/upload?return=redirect" method="post">
|
<form action="/upload?return=html" method="post">
|
||||||
<label for="paste-expiry">expiry</label>
|
<label for="paste-expiry">expiry</label>
|
||||||
|
|
||||||
<select id="paste-expiry" name="expiry">
|
<select id="paste-expiry" name="expiry">
|
||||||
@@ -37,7 +37,7 @@
|
|||||||
{{end}}
|
{{end}}
|
||||||
</select>
|
</select>
|
||||||
<textarea id="content" name="content" rows="10" required placeholder="cmon, paste something already!"></textarea>
|
<textarea id="content" name="content" rows="10" required placeholder="cmon, paste something already!"></textarea>
|
||||||
<button type="submit">upload paste</button>
|
<button type="submit" class="btn">upload paste</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
<!doctype html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
|
<title>minibin</title>
|
||||||
|
<link rel="stylesheet" href="/css/main.css" />
|
||||||
|
</head>
|
||||||
|
<script src="/js/copy.js" defer></script>
|
||||||
|
<body>
|
||||||
|
{{template "site-header"}}
|
||||||
|
<main>
|
||||||
|
<div>
|
||||||
|
{{if hasPrefix .MIMEType "image/"}}
|
||||||
|
<img src="/{{.ID}}" />
|
||||||
|
{{else if hasPrefix .MIMEType "video/"}}
|
||||||
|
<video src="/{{.ID}}" controls></video>
|
||||||
|
{{else if hasPrefix .MIMEType "audio/"}}
|
||||||
|
<audio src="/{{.ID}}" controls></audio>
|
||||||
|
{{else}}
|
||||||
|
<h2>{{.Filename}}</h2>
|
||||||
|
<h2>{{.ID}}{{.FileExtension}}</h2>
|
||||||
|
{{end}}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="upload-links">
|
||||||
|
<a href="/{{.ID}}">view raw upload</a>
|
||||||
|
<!--
|
||||||
|
copying is only available with javascript
|
||||||
|
we will make this element visible with javascript
|
||||||
|
-->
|
||||||
|
<a id="copy-link" data-id="{{.ID}}" href="#" style="display: none">copy link</a>
|
||||||
|
<form action="/{{.ID}}/delete?redirect=true" method="post">
|
||||||
|
<input type="hidden" name="delete_token" value="{{.DeleteToken}}" />
|
||||||
|
<button type="submit" class="link-btn">delete upload</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user