增加文本中转功能
This commit is contained in:
@@ -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