2026-05-25 15:36:49 +03:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
2026-05-25 16:26:47 +03:00
|
|
|
"errors"
|
2026-05-29 23:44:05 +03:00
|
|
|
"fmt"
|
|
|
|
|
"mime/multipart"
|
2026-05-25 15:36:49 +03:00
|
|
|
"net/http"
|
2026-05-25 16:26:47 +03:00
|
|
|
"strconv"
|
2026-05-29 23:44:05 +03:00
|
|
|
"strings"
|
2026-05-25 15:36:49 +03:00
|
|
|
|
|
|
|
|
"warpbox.dev/backend/libs/helpers"
|
2026-05-29 23:44:05 +03:00
|
|
|
"warpbox.dev/backend/libs/jobs"
|
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-29 23:44:05 +03:00
|
|
|
files := uploadFiles(r)
|
2026-05-25 16:26:47 +03:00
|
|
|
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
|
|
|
|
|
}
|
2026-05-29 23:44:05 +03:00
|
|
|
jobs.GenerateThumbnailsForBoxAsync(a.uploadService, a.logger, result.BoxID)
|
2026-05-25 16:26:47 +03:00
|
|
|
|
2026-05-29 23:44:05 +03:00
|
|
|
if wantsJSON(r) {
|
|
|
|
|
helpers.WriteJSON(w, http.StatusCreated, result)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
|
_, _ = fmt.Fprintln(w, result.BoxURL)
|
2026-05-25 16:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2026-05-29 23:44:05 +03:00
|
|
|
|
|
|
|
|
func uploadFiles(r *http.Request) []*multipart.FileHeader {
|
|
|
|
|
if r.MultipartForm == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
files := make([]*multipart.FileHeader, 0)
|
|
|
|
|
files = append(files, r.MultipartForm.File["file"]...)
|
|
|
|
|
files = append(files, r.MultipartForm.File["sharex"]...)
|
|
|
|
|
return files
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func wantsJSON(r *http.Request) bool {
|
|
|
|
|
return strings.Contains(strings.ToLower(r.Header.Get("Accept")), "application/json")
|
|
|
|
|
}
|