add info handler

This commit is contained in:
2026-11-14 17:02:17 -05:00
parent 40246c9ad5
commit f33236a6fe
2 changed files with 41 additions and 1 deletions
+37
View File
@@ -46,6 +46,11 @@ type UploadResponse struct {
DeleteToken string `json:"deleteToken"` DeleteToken string `json:"deleteToken"`
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
} }
type ContentInfoResponse struct {
Upload
SHA256 string `json:"sha256"`
Filename string `json:"-"`
}
func render(pages *template.Template, w http.ResponseWriter, name string, data any) { func render(pages *template.Template, w http.ResponseWriter, name string, data any) {
w.Header().Set("Conten-Type", "text/html; charset=utf-8") w.Header().Set("Conten-Type", "text/html; charset=utf-8")
@@ -353,6 +358,38 @@ func contentHandler(db *gorm.DB, cfg Config, tracker *AccessTracker) http.Handle
} }
} }
func contentInfoHandler(db *gorm.DB, tracker *AccessTracker) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, _, _ := strings.Cut(r.PathValue("id"), ".") // remove any extension via strings.Cut
if !validPasteId(id) {
http.NotFound(w, r)
return
}
upload, err := getUploadById(db, id)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
http.NotFound(w, r)
return
}
log.Println("get upload:", err)
http.Error(w, "internal server error", http.StatusInternalServerError)
return
}
tracker.Record(id)
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(ContentInfoResponse{
Upload: *upload,
SHA256: hex.EncodeToString(upload.SHA256),
}); err != nil {
log.Println("failed to encode response:", err)
}
}
}
func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc { func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id") id := r.PathValue("id")
+4 -1
View File
@@ -119,7 +119,10 @@ func main() {
"NormalizeMIMETypes": config.NormalizeMIMETypes, "NormalizeMIMETypes": config.NormalizeMIMETypes,
}) })
}) })
mux.HandleFunc("GET /{id}", contentHandler(db, config, tracker)) uploads := http.NewServeMux()
uploads.HandleFunc("GET /{id}", contentHandler(db, config, tracker))
uploads.HandleFunc("GET /{id}/info", contentInfoHandler(db, tracker))
mux.Handle("/", uploads)
mux.HandleFunc("POST /upload", uploadHandler(db, config, pages)) mux.HandleFunc("POST /upload", uploadHandler(db, config, pages))
mux.HandleFunc("PUT /upload", uploadHandler(db, config, pages)) mux.HandleFunc("PUT /upload", uploadHandler(db, config, pages))
mux.HandleFunc("DELETE /{id}", deleteHandler(db, config)) mux.HandleFunc("DELETE /{id}", deleteHandler(db, config))