同步支持动态更新取件码长度,调整存量取件码以适配新长度并优化相关逻辑和文档。
This commit is contained in:
@@ -6,9 +6,7 @@ import (
|
||||
"FileRelay/internal/model"
|
||||
"FileRelay/internal/storage"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"mime/multipart"
|
||||
"path/filepath"
|
||||
"time"
|
||||
@@ -27,7 +25,8 @@ func NewUploadService() *UploadService {
|
||||
|
||||
func (s *UploadService) CreateBatch(ctx context.Context, files []*multipart.FileHeader, remark string, expireType string, expireValue interface{}) (*model.FileBatch, error) {
|
||||
// 1. 生成取件码
|
||||
pickupCode, err := s.generatePickupCode(config.GlobalConfig.Security.PickupCodeLength)
|
||||
batchService := NewBatchService()
|
||||
pickupCode, err := batchService.GeneratePickupCode(config.GlobalConfig.Security.PickupCodeLength)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -63,7 +62,8 @@ 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)
|
||||
batchService := NewBatchService()
|
||||
pickupCode, err := batchService.GeneratePickupCode(config.GlobalConfig.Security.PickupCodeLength)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -136,22 +136,3 @@ func (s *UploadService) processFile(ctx context.Context, tx *gorm.DB, batchID st
|
||||
|
||||
return item, nil
|
||||
}
|
||||
|
||||
func (s *UploadService) generatePickupCode(length int) (string, error) {
|
||||
const charset = "0123456789"
|
||||
b := make([]byte, length)
|
||||
for i := range b {
|
||||
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(charset))))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
b[i] = charset[num.Int64()]
|
||||
}
|
||||
// 检查是否冲突
|
||||
var count int64
|
||||
s.db.Model(&model.FileBatch{}).Where("pickup_code = ? AND status = ?", string(b), "active").Count(&count)
|
||||
if count > 0 {
|
||||
return s.generatePickupCode(length) // 递归生成
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user