return file extension on upload; strip extension when returning upload

This commit is contained in:
2026-11-10 21:12:34 -05:00
parent a59c32e503
commit 3a336b79c8
+8 -6
View File
@@ -40,8 +40,9 @@ type UploadInput struct {
} }
type UploadResponse struct { type UploadResponse struct {
Upload Upload
SHA256 string `json:"sha256"` SHA256 string `json:"sha256"`
DeleteToken string `json:"deleteToken"` DeleteToken string `json:"deleteToken"`
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) http.HandlerFunc {
@@ -217,9 +218,10 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
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(UploadResponse{
Upload: upload, Upload: upload,
SHA256: hex.EncodeToString(upload.SHA256), SHA256: hex.EncodeToString(upload.SHA256),
DeleteToken: upload.DeleteToken, DeleteToken: upload.DeleteToken,
FileExtension: filepath.Ext(upload.Filename),
}); err != nil { }); err != nil {
log.Println("failed to encode response:", err) log.Println("failed to encode response:", err)
} }
@@ -228,7 +230,7 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
func contentHandler(db *gorm.DB, cfg Config) http.HandlerFunc { func contentHandler(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, _, _ := strings.Cut(r.PathValue("id"), ".") // remove any extension via strings.Cut
if !validPasteId(id) { if !validPasteId(id) {
http.NotFound(w, r) http.NotFound(w, r)