79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package file
|
|
|
|
import (
|
|
"ResendIt/internal/config"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// UploadMulti accepts up to 10 files, zips them server-side, and returns a single download/view key.
|
|
func (h *Handler) UploadMulti(c *gin.Context) {
|
|
if err := c.Request.ParseMultipartForm(0); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
form, err := c.MultipartForm()
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid multipart form"})
|
|
return
|
|
}
|
|
|
|
files := form.File["files"]
|
|
if len(files) == 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "missing files"})
|
|
return
|
|
}
|
|
maxFiles := h.configService.GetIntDefault(config.KeyUploadMultiMaxFiles, config.DefaultUploadMultiMaxFiles)
|
|
if len(files) > maxFiles {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "too many files"})
|
|
return
|
|
}
|
|
|
|
maxSize := h.configService.GetInt64Default(config.KeyUploadMaxFileSizeBytes, config.DefaultUploadMaxFileSizeBytes)
|
|
var total int64
|
|
for _, fh := range files {
|
|
if fh.Size > maxSize {
|
|
c.JSON(http.StatusRequestEntityTooLarge, gin.H{"error": "file too large"})
|
|
return
|
|
}
|
|
total += fh.Size
|
|
if total > maxSize {
|
|
// crude guard against huge bundles; you can add a separate config later.
|
|
c.JSON(http.StatusRequestEntityTooLarge, gin.H{"error": "bundle too large"})
|
|
return
|
|
}
|
|
}
|
|
|
|
once := c.PostForm("once") == "true"
|
|
|
|
durationStr := c.PostForm("duration")
|
|
hours, err := strconv.Atoi(durationStr)
|
|
if err != nil || hours <= 0 {
|
|
hours = 24
|
|
}
|
|
maxHours := h.configService.GetIntDefault(config.KeyUploadMaxHours, config.DefaultUploadMaxHours)
|
|
if hours > maxHours {
|
|
hours = maxHours
|
|
}
|
|
duration := time.Duration(hours) * time.Hour
|
|
|
|
record, err := h.service.UploadBundle(files, once, duration)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"id": record.ID,
|
|
"deletion_id": record.DeletionID,
|
|
"filename": record.Filename,
|
|
"size": record.Size,
|
|
"expires_at": record.ExpiresAt,
|
|
"view_key": record.ViewID,
|
|
})
|
|
}
|