97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
package public
|
|
|
|
import (
|
|
"FileRelay/internal/config"
|
|
"FileRelay/internal/model"
|
|
"FileRelay/internal/service"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type UploadHandler struct {
|
|
uploadService *service.UploadService
|
|
}
|
|
|
|
func NewUploadHandler() *UploadHandler {
|
|
return &UploadHandler{
|
|
uploadService: service.NewUploadService(),
|
|
}
|
|
}
|
|
|
|
type UploadResponse struct {
|
|
PickupCode string `json:"pickup_code"`
|
|
ExpireAt *time.Time `json:"expire_at"`
|
|
BatchID uint `json:"batch_id"`
|
|
}
|
|
|
|
// Upload 上传文件并生成取件码
|
|
// @Summary 上传文件
|
|
// @Description 上传一个或多个文件并创建一个提取批次
|
|
// @Tags Public
|
|
// @Accept multipart/form-data
|
|
// @Produce json
|
|
// @Param files formData file true "文件列表"
|
|
// @Param remark formData string false "备注"
|
|
// @Param expire_type formData string false "过期类型 (time/download/permanent)"
|
|
// @Param expire_days formData int false "过期天数 (针对 time 类型)"
|
|
// @Param max_downloads formData int false "最大下载次数 (针对 download 类型)"
|
|
// @Success 200 {object} model.Response{data=UploadResponse}
|
|
// @Failure 400 {object} model.Response
|
|
// @Failure 500 {object} model.Response
|
|
// @Router /api/upload [post]
|
|
func (h *UploadHandler) Upload(c *gin.Context) {
|
|
form, err := c.MultipartForm()
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, "invalid form"))
|
|
return
|
|
}
|
|
|
|
files := form.File["files"]
|
|
if len(files) == 0 {
|
|
c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, "no files uploaded"))
|
|
return
|
|
}
|
|
|
|
if len(files) > config.GlobalConfig.Upload.MaxBatchFiles {
|
|
c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, "too many files"))
|
|
return
|
|
}
|
|
|
|
remark := c.PostForm("remark")
|
|
expireType := c.PostForm("expire_type") // time / download / permanent
|
|
if expireType == "" {
|
|
expireType = "time"
|
|
}
|
|
|
|
var expireValue interface{}
|
|
switch expireType {
|
|
case "time":
|
|
days, _ := strconv.Atoi(c.PostForm("expire_days"))
|
|
if days <= 0 {
|
|
days = config.GlobalConfig.Upload.MaxRetentionDays
|
|
}
|
|
expireValue = days
|
|
case "download":
|
|
max, _ := strconv.Atoi(c.PostForm("max_downloads"))
|
|
if max <= 0 {
|
|
max = 1
|
|
}
|
|
expireValue = max
|
|
}
|
|
|
|
batch, err := h.uploadService.CreateBatch(c.Request.Context(), files, remark, expireType, expireValue)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, err.Error()))
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.SuccessResponse(UploadResponse{
|
|
PickupCode: batch.PickupCode,
|
|
ExpireAt: batch.ExpireAt,
|
|
BatchID: batch.ID,
|
|
}))
|
|
}
|