add info handler
This commit is contained in:
+37
@@ -46,6 +46,11 @@ type UploadResponse struct {
|
||||
DeleteToken string `json:"deleteToken"`
|
||||
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) {
|
||||
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 {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.PathValue("id")
|
||||
|
||||
Reference in New Issue
Block a user