2026-05-25 15:36:49 +03:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-25 16:26:47 +03:00
|
|
|
"errors"
|
2026-05-25 15:36:49 +03:00
|
|
|
"net/http"
|
2026-05-25 16:26:47 +03:00
|
|
|
"strconv"
|
2026-05-25 15:36:49 +03:00
|
|
|
|
|
|
|
|
"warpbox.dev/backend/libs/helpers"
|
2026-05-25 16:26:47 +03:00
|
|
|
"warpbox.dev/backend/libs/services"
|
2026-05-25 15:36:49 +03:00
|
|
|
)
|
|
|
|
|
|
2026-05-25 16:26:47 +03:00
|
|
|
func (a *App) Upload(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, a.uploadService.MaxUploadSize()*8)
|
|
|
|
|
if err := r.ParseMultipartForm(a.uploadService.MaxUploadSize() * 8); err != nil {
|
|
|
|
|
helpers.WriteJSONError(w, http.StatusBadRequest, "upload form could not be read")
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-05-25 15:36:49 +03:00
|
|
|
|
2026-05-25 16:26:47 +03:00
|
|
|
files := r.MultipartForm.File["file"]
|
|
|
|
|
result, err := a.uploadService.CreateBox(files, services.UploadOptions{
|
2026-05-25 16:52:57 +03:00
|
|
|
MaxDays: parseInt(r.FormValue("max_days")),
|
|
|
|
|
MaxDownloads: parseInt(r.FormValue("max_downloads")),
|
|
|
|
|
Password: r.FormValue("password"),
|
|
|
|
|
ObfuscateMetadata: r.FormValue("obfuscate_metadata") == "on",
|
2026-05-25 15:36:49 +03:00
|
|
|
})
|
2026-05-25 16:26:47 +03:00
|
|
|
if err != nil {
|
2026-05-25 16:52:57 +03:00
|
|
|
a.logger.Warn("upload failed", "source", "user-upload", "severity", "warn", "code", 4001, "error", err.Error())
|
2026-05-25 16:26:47 +03:00
|
|
|
helpers.WriteJSONError(w, http.StatusBadRequest, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
helpers.WriteJSON(w, http.StatusCreated, result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func parseInt(value string) int {
|
|
|
|
|
if value == "" {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
parsed, err := strconv.Atoi(value)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
return parsed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func statusForDownloadError(err error) int {
|
|
|
|
|
if errors.Is(err, http.ErrMissingFile) {
|
|
|
|
|
return http.StatusNotFound
|
|
|
|
|
}
|
|
|
|
|
return http.StatusForbidden
|
2026-05-25 15:36:49 +03:00
|
|
|
}
|