增加文本中转功能
This commit is contained in:
@@ -22,7 +22,9 @@ type PickupResponse struct {
|
||||
ExpireType string `json:"expire_type"`
|
||||
DownloadCount int `json:"download_count"`
|
||||
MaxDownloads int `json:"max_downloads"`
|
||||
Files []model.FileItem `json:"files"`
|
||||
Type string `json:"type"`
|
||||
Content string `json:"content,omitempty"`
|
||||
Files []model.FileItem `json:"files,omitempty"`
|
||||
}
|
||||
|
||||
// DownloadBatch 批量下载文件 (ZIP)
|
||||
@@ -107,6 +109,8 @@ func (h *PickupHandler) Pickup(c *gin.Context) {
|
||||
ExpireType: batch.ExpireType,
|
||||
DownloadCount: batch.DownloadCount,
|
||||
MaxDownloads: batch.MaxDownloads,
|
||||
Type: batch.Type,
|
||||
Content: batch.Content,
|
||||
Files: batch.FileItems,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -94,3 +94,60 @@ func (h *UploadHandler) Upload(c *gin.Context) {
|
||||
BatchID: batch.ID,
|
||||
}))
|
||||
}
|
||||
|
||||
type UploadTextRequest struct {
|
||||
Content string `json:"content" binding:"required" example:"这是一段长文本内容..."`
|
||||
Remark string `json:"remark" example:"文本备注"`
|
||||
ExpireType string `json:"expire_type" example:"time"`
|
||||
ExpireDays int `json:"expire_days" example:"7"`
|
||||
MaxDownloads int `json:"max_downloads" example:"5"`
|
||||
}
|
||||
|
||||
// UploadText 发送长文本并生成取件码
|
||||
// @Summary 发送长文本
|
||||
// @Description 中转一段长文本内容并创建一个提取批次
|
||||
// @Tags Public
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body UploadTextRequest true "文本内容及配置"
|
||||
// @Success 200 {object} model.Response{data=UploadResponse}
|
||||
// @Failure 400 {object} model.Response
|
||||
// @Failure 500 {object} model.Response
|
||||
// @Router /api/upload/text [post]
|
||||
func (h *UploadHandler) UploadText(c *gin.Context) {
|
||||
var req UploadTextRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, model.ErrorResponse(model.CodeBadRequest, err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if req.ExpireType == "" {
|
||||
req.ExpireType = "time"
|
||||
}
|
||||
|
||||
var expireValue interface{}
|
||||
switch req.ExpireType {
|
||||
case "time":
|
||||
if req.ExpireDays <= 0 {
|
||||
req.ExpireDays = config.GlobalConfig.Upload.MaxRetentionDays
|
||||
}
|
||||
expireValue = req.ExpireDays
|
||||
case "download":
|
||||
if req.MaxDownloads <= 0 {
|
||||
req.MaxDownloads = 1
|
||||
}
|
||||
expireValue = req.MaxDownloads
|
||||
}
|
||||
|
||||
batch, err := h.uploadService.CreateTextBatch(c.Request.Context(), req.Content, req.Remark, req.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,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@ type FileBatch struct {
|
||||
MaxDownloads int `json:"max_downloads"`
|
||||
DownloadCount int `gorm:"default:0" json:"download_count"`
|
||||
Status string `gorm:"default:'active'" json:"status"` // active / expired / deleted
|
||||
Type string `gorm:"default:'file'" json:"type"` // file / text
|
||||
Content string `json:"content,omitempty"`
|
||||
FileItems []FileItem `gorm:"foreignKey:BatchID" json:"file_items,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
@@ -38,19 +38,10 @@ func (s *UploadService) CreateBatch(ctx context.Context, files []*multipart.File
|
||||
Remark: remark,
|
||||
ExpireType: expireType,
|
||||
Status: "active",
|
||||
Type: "file",
|
||||
}
|
||||
|
||||
switch expireType {
|
||||
case "time":
|
||||
if days, ok := expireValue.(int); ok {
|
||||
expireAt := time.Now().Add(time.Duration(days) * 24 * time.Hour)
|
||||
batch.ExpireAt = &expireAt
|
||||
}
|
||||
case "download":
|
||||
if max, ok := expireValue.(int); ok {
|
||||
batch.MaxDownloads = max
|
||||
}
|
||||
}
|
||||
s.applyExpire(batch, expireType, expireValue)
|
||||
|
||||
// 3. 处理文件上传
|
||||
return batch, s.db.Transaction(func(tx *gorm.DB) error {
|
||||
@@ -69,6 +60,47 @@ func (s *UploadService) CreateBatch(ctx context.Context, files []*multipart.File
|
||||
})
|
||||
}
|
||||
|
||||
func (s *UploadService) CreateTextBatch(ctx context.Context, content string, remark string, expireType string, expireValue interface{}) (*model.FileBatch, error) {
|
||||
// 1. 生成取件码
|
||||
pickupCode, err := s.generatePickupCode(config.GlobalConfig.Security.PickupCodeLength)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 2. 准备 Batch
|
||||
batch := &model.FileBatch{
|
||||
PickupCode: pickupCode,
|
||||
Remark: remark,
|
||||
ExpireType: expireType,
|
||||
Status: "active",
|
||||
Type: "text",
|
||||
Content: content,
|
||||
}
|
||||
|
||||
s.applyExpire(batch, expireType, expireValue)
|
||||
|
||||
// 3. 保存
|
||||
if err := s.db.Create(batch).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return batch, nil
|
||||
}
|
||||
|
||||
func (s *UploadService) applyExpire(batch *model.FileBatch, expireType string, expireValue interface{}) {
|
||||
switch expireType {
|
||||
case "time":
|
||||
if days, ok := expireValue.(int); ok {
|
||||
expireAt := time.Now().Add(time.Duration(days) * 24 * time.Hour)
|
||||
batch.ExpireAt = &expireAt
|
||||
}
|
||||
case "download":
|
||||
if max, ok := expireValue.(int); ok {
|
||||
batch.MaxDownloads = max
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *UploadService) processFile(ctx context.Context, tx *gorm.DB, batchID uint, fileHeader *multipart.FileHeader) (*model.FileItem, error) {
|
||||
file, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user