return file extension on upload; strip extension when returning upload
This commit is contained in:
+8
-6
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user