upload page after uploading

This commit is contained in:
2026-11-12 21:14:41 -05:00
parent 59c156f6ac
commit 350bf1842f
7 changed files with 118 additions and 15 deletions
+23 -7
View File
@@ -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)
}
}