add return type query on upload, will eventually use to have an after upload page
This commit is contained in:
+18
-15
@@ -64,9 +64,10 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
||||
if r.URL.Query().Get("idlength") == "short" {
|
||||
idLength = 10
|
||||
}
|
||||
redirect := false
|
||||
if r.URL.Query().Get("redirect") == "true" {
|
||||
redirect = true
|
||||
|
||||
returnType := strings.TrimSpace(r.URL.Query().Get("return"))
|
||||
if returnType == "" {
|
||||
returnType = "json"
|
||||
}
|
||||
|
||||
expLabel := r.URL.Query().Get("expiry")
|
||||
@@ -238,21 +239,23 @@ func uploadHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
|
||||
}
|
||||
saved = true
|
||||
|
||||
if redirect {
|
||||
switch returnType {
|
||||
// html & maybe xml? cant think of other return types
|
||||
case "redirect":
|
||||
http.Redirect(w, r, "/"+id, http.StatusSeeOther)
|
||||
return
|
||||
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 {
|
||||
log.Println("failed to encode response:", err)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
log.Println("failed to encode response:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user