diff --git a/handlers.go b/handlers.go index c982d9d..f2f5e1d 100644 --- a/handlers.go +++ b/handlers.go @@ -8,6 +8,7 @@ import ( "encoding/json" "errors" "fmt" + "html/template" "io" "log" "mime" @@ -44,7 +45,7 @@ type UploadResponse struct { 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) { if r.Method != http.MethodPost { http.Error(w, "invalid method", http.StatusMethodNotAllowed) @@ -239,19 +240,27 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc { } saved = true + response := UploadResponse{ + Upload: upload, + SHA256: hex.EncodeToString(upload.SHA256), + DeleteToken: upload.DeleteToken, + FileExtension: filepath.Ext(upload.Filename), + } + switch returnType { // 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": http.Redirect(w, r, "/"+id, http.StatusSeeOther) default: w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) - if err := json.NewEncoder(w).Encode(UploadResponse{ - Upload: upload, - SHA256: hex.EncodeToString(upload.SHA256), - DeleteToken: upload.DeleteToken, - FileExtension: filepath.Ext(upload.Filename), - }); err != nil { + if err := json.NewEncoder(w).Encode(response); err != nil { log.Println("failed to encode response:", err) } } @@ -312,6 +321,8 @@ func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc { http.NotFound(w, r) return } + redirect := r.URL.Query().Get("redirect") == "true" + var token string switch r.Method { case http.MethodDelete: @@ -362,6 +373,11 @@ func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc { return } + if redirect { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + w.WriteHeader(http.StatusNoContent) } } diff --git a/main.go b/main.go index e9faa67..d17b522 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "os" + "strings" ) func main() { @@ -27,7 +28,13 @@ func main() { 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 { log.Fatal(err) } @@ -49,7 +56,7 @@ func main() { } }) 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("POST /{id}/delete", deleteHandler(db, config)) mux.HandleFunc("GET /health", health) diff --git a/static/css/main.css b/static/css/main.css index 60ffe71..c78b90d 100644 --- a/static/css/main.css +++ b/static/css/main.css @@ -149,7 +149,7 @@ option { font: inherit; } -button[type="submit"] { +.btn { border-radius: 0; border: none; background-color: var(--text); @@ -158,3 +158,25 @@ button[type="submit"] { font: inherit; 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; +} \ No newline at end of file diff --git a/static/js/copy.js b/static/js/copy.js new file mode 100644 index 0000000..b08f444 --- /dev/null +++ b/static/js/copy.js @@ -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); + } + }; +} diff --git a/templates/header.html b/templates/header.html index 17a156b..82584c9 100644 --- a/templates/header.html +++ b/templates/header.html @@ -1,6 +1,6 @@ {{define "site-header"}}