同步支持动态更新取件码长度,调整存量取件码以适配新长度并优化相关逻辑和文档。

This commit is contained in:
2026-01-14 17:03:03 +08:00
parent f0c8133cb0
commit 673788b4b6
8 changed files with 108 additions and 28 deletions

View File

@@ -17,7 +17,7 @@
| 字段名 | 类型 | 含义 | 示例 |
| :--- | :--- | :--- | :--- |
| `admin_password_hash` | string | 管理员密码的 bcrypt 哈希值 | `$2a$10$...` |
| `pickup_code_length` | int | 自动生成的取件码长度(已包含在公共配置接口中) | `6` |
| `pickup_code_length` | int | 自动生成的取件码长度。变更后系统将自动对存量取件码进行右侧补零或截取以适配新长度。 | `6` |
| `pickup_fail_limit` | int | 单个 IP 对单个取件码尝试失败的最大次数,超过后将被临时封禁 | `5` |
| `jwt_secret` | string | 用于签发管理端 JWT Token 的密钥,建议设置为复杂随机字符串 | `file-relay-secret` |

View File

@@ -1077,7 +1077,7 @@ const docTemplate = `{
"type": "string"
},
"pickup_code_length": {
"description": "取件码长度",
"description": "取件码长度 (变更后将自动通过右侧补零或截取调整存量数据)",
"type": "integer"
},
"pickup_fail_limit": {

View File

@@ -1070,7 +1070,7 @@
"type": "string"
},
"pickup_code_length": {
"description": "取件码长度",
"description": "取件码长度 (变更后将自动通过右侧补零或截取调整存量数据)",
"type": "integer"
},
"pickup_fail_limit": {

View File

@@ -113,7 +113,7 @@ definitions:
description: JWT 签名密钥
type: string
pickup_code_length:
description: 取件码长度
description: 取件码长度 (变更后将自动通过右侧补零或截取调整存量数据)
type: integer
pickup_fail_limit:
description: 取件失败尝试限制

View File

@@ -4,6 +4,7 @@ import (
"FileRelay/internal/bootstrap"
"FileRelay/internal/config"
"FileRelay/internal/model"
"FileRelay/internal/service"
"net/http"
"github.com/gin-gonic/gin"
@@ -51,9 +52,21 @@ func (h *ConfigHandler) UpdateConfig(c *gin.Context) {
newConfig.Database.Path = config.GlobalConfig.Database.Path
}
// 检查取件码长度是否变化
pickupCodeLengthChanged := newConfig.Security.PickupCodeLength != config.GlobalConfig.Security.PickupCodeLength && newConfig.Security.PickupCodeLength > 0
// 更新内存配置
config.UpdateGlobalConfig(&newConfig)
// 如果长度变化,同步更新现有取件码
if pickupCodeLengthChanged {
batchService := service.NewBatchService()
if err := batchService.UpdateAllPickupCodes(newConfig.Security.PickupCodeLength); err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Failed to update existing pickup codes: "+err.Error()))
return
}
}
// 重新初始化存储(热更新业务逻辑)
if err := bootstrap.ReloadStorage(); err != nil {
c.JSON(http.StatusInternalServerError, model.ErrorResponse(model.CodeInternalError, "Failed to reload storage: "+err.Error()))

View File

@@ -24,7 +24,7 @@ type SiteConfig struct {
type SecurityConfig struct {
AdminPasswordHash string `yaml:"admin_password_hash" json:"admin_password_hash"` // 管理员密码哈希 (bcrypt)
PickupCodeLength int `yaml:"pickup_code_length" json:"pickup_code_length"` // 取件码长度
PickupCodeLength int `yaml:"pickup_code_length" json:"pickup_code_length"` // 取件码长度 (变更后将自动通过右侧补零或截取调整存量数据)
PickupFailLimit int `yaml:"pickup_fail_limit" json:"pickup_fail_limit"` // 取件失败尝试限制
JWTSecret string `yaml:"jwt_secret" json:"jwt_secret"` // JWT 签名密钥
}

View File

@@ -6,8 +6,12 @@ import (
"FileRelay/internal/storage"
"context"
"errors"
"math/big"
"strings"
"time"
"crypto/rand"
"gorm.io/gorm"
)
@@ -84,3 +88,85 @@ func (s *BatchService) IncrementDownloadCount(batchID string) error {
return s.db.Model(&model.FileBatch{}).Where("id = ?", batchID).
UpdateColumn("download_count", gorm.Expr("download_count + ?", 1)).Error
}
func (s *BatchService) 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 = ?", string(b)).Count(&count)
if count > 0 {
return s.GeneratePickupCode(length) // 递归生成
}
return string(b), nil
}
func (s *BatchService) UpdateAllPickupCodes(newLength int) error {
var batches []model.FileBatch
// 只更新未删除的记录,包括 active 和 expired
if err := s.db.Find(&batches).Error; err != nil {
return err
}
return s.db.Transaction(func(tx *gorm.DB) error {
for _, batch := range batches {
oldCode := batch.PickupCode
if len(oldCode) == newLength {
continue
}
var newCode string
if len(oldCode) < newLength {
// 右侧补零,方便用户输入原码后通过补 0 完成输入
newCode = oldCode + strings.Repeat("0", newLength-len(oldCode))
} else {
// 截取前 newLength 位,保留原码头部
newCode = oldCode[:newLength]
}
// 检查冲突 (在事务中检查)
var count int64
tx.Model(&model.FileBatch{}).Where("pickup_code = ? AND id != ?", newCode, batch.ID).Count(&count)
if count > 0 {
// 如果冲突,生成一个新的随机码
var err error
newCode, err = s.generateUniquePickupCodeInTx(tx, newLength)
if err != nil {
return err
}
}
if err := tx.Model(&batch).Update("pickup_code", newCode).Error; err != nil {
return err
}
}
return nil
})
}
func (s *BatchService) generateUniquePickupCodeInTx(tx *gorm.DB, length int) (string, error) {
const charset = "0123456789"
for {
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
tx.Model(&model.FileBatch{}).Where("pickup_code = ?", string(b)).Count(&count)
if count == 0 {
return string(b), nil
}
}
}

View File

@@ -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
}