mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-11-16 23:04:24 +08:00
fix(i18n): use English runtime text and localize system UI
This commit is contained in:
@@ -65,15 +65,15 @@ func ServeFileByID(c *gin.Context) {
|
||||
upload, err := GetUploadRecordByID(c)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
response.AbortNotFound(c, "文件记录未找到")
|
||||
response.AbortNotFound(c, "File record was not found")
|
||||
return
|
||||
}
|
||||
var numErr *strconv.NumError
|
||||
if errors.As(err, &numErr) {
|
||||
response.AbortBadRequest(c, "无效的上传ID")
|
||||
response.AbortBadRequest(c, "Invalid upload ID")
|
||||
return
|
||||
}
|
||||
response.AbortInternal(c, "服务器内部错误")
|
||||
response.AbortInternal(c, "Internal server error")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -266,7 +266,7 @@ func ImageCompressionCacheKey(upload *model.Upload, quality string) string {
|
||||
func serveOriginal(c *gin.Context, upload *model.Upload) {
|
||||
obj, err := uploadstorage.OpenStoredObject(c.Request.Context(), upload)
|
||||
if err != nil {
|
||||
response.AbortNotFound(c, "文件未找到")
|
||||
response.AbortNotFound(c, "File was not found")
|
||||
return
|
||||
}
|
||||
defer func() { _ = obj.Body.Close() }()
|
||||
|
||||
@@ -118,7 +118,7 @@ func DeleteFile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if isRecordNotFound(err) {
|
||||
response.AbortNotFound(c, "文件记录未找到")
|
||||
response.AbortNotFound(c, "File record was not found")
|
||||
return
|
||||
}
|
||||
response.AbortBadRequest(c, shared.ErrDeleteFileFailed)
|
||||
@@ -244,11 +244,11 @@ func DeleteMyFile(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if isRecordNotFound(err) {
|
||||
response.AbortNotFound(c, "文件记录未找到")
|
||||
response.AbortNotFound(c, "File record was not found")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, ingest.ErrForbidden) {
|
||||
response.AbortForbidden(c, "无权操作")
|
||||
response.AbortForbidden(c, "Operation not permitted")
|
||||
return
|
||||
}
|
||||
response.AbortBadRequest(c, shared.ErrDeleteFileFailed)
|
||||
@@ -298,14 +298,14 @@ func UpdateMyFile(c *gin.Context) {
|
||||
upload, err := updateOwnedUpload(ctx, currUser.ID, uploadID, updateMyUploadInput(req))
|
||||
if err != nil {
|
||||
if isRecordNotFound(err) {
|
||||
response.AbortNotFound(c, "文件记录未找到")
|
||||
response.AbortNotFound(c, "File record was not found")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, ingest.ErrForbidden) {
|
||||
response.AbortForbidden(c, "无权操作")
|
||||
response.AbortForbidden(c, "Operation not permitted")
|
||||
return
|
||||
}
|
||||
response.AbortBadRequest(c, "更新文件记录失败")
|
||||
response.AbortBadRequest(c, "Failed to update file record")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -175,7 +175,7 @@ func DownloadFile(c *gin.Context) {
|
||||
upload, err := filesrv.GetUploadRecordByID(c)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
response.AbortNotFound(c, "文件记录未找到")
|
||||
response.AbortNotFound(c, "File record was not found")
|
||||
return
|
||||
}
|
||||
var numErr *strconv.NumError
|
||||
@@ -281,13 +281,13 @@ func BatchDownloadFiles(c *gin.Context) {
|
||||
|
||||
zipFileEntry, err := zipWriter.Create(fileName)
|
||||
if err != nil {
|
||||
logger.ErrorF(ctx, "ZIP 添加条目失败 [%s]: %v", fileName, err)
|
||||
logger.ErrorF(ctx, "Failed to add ZIP entry [%s]: %v", fileName, err)
|
||||
continue
|
||||
}
|
||||
|
||||
obj, err := uploadstorage.OpenStoredObject(ctx, &upload)
|
||||
if err != nil {
|
||||
logger.ErrorF(ctx, "打包时读取文件失败: %v", err)
|
||||
logger.ErrorF(ctx, "Failed to read file while creating archive: %v", err)
|
||||
continue
|
||||
}
|
||||
rc := obj.Body
|
||||
@@ -295,7 +295,7 @@ func BatchDownloadFiles(c *gin.Context) {
|
||||
_, err = io.Copy(zipFileEntry, rc)
|
||||
_ = rc.Close()
|
||||
if err != nil {
|
||||
logger.ErrorF(ctx, "写入 ZIP 流失败: %v", err)
|
||||
logger.ErrorF(ctx, "Failed to write ZIP stream: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -311,7 +311,7 @@ func resolveUploadAccessMode(c *gin.Context, uploadType string) (int, string) {
|
||||
|
||||
accessMode, err := strconv.Atoi(accessModeStr)
|
||||
if err != nil || (accessMode != 0 && accessMode != 1) {
|
||||
return 0, "无效的 access_mode 参数"
|
||||
return 0, "Invalid access_mode parameter"
|
||||
}
|
||||
return accessMode, ""
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Rain-kl/Wavelet/internal/apps/upload/shared"
|
||||
uploadstats "github.com/Rain-kl/Wavelet/internal/apps/upload/stats"
|
||||
"github.com/Rain-kl/Wavelet/internal/model"
|
||||
"github.com/Rain-kl/Wavelet/internal/shared/response"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -71,7 +72,7 @@ func GetFileStats(c *gin.Context) {
|
||||
categories []distributionItem
|
||||
)
|
||||
|
||||
categoriesList := []string{"图片", "视频", "音频", "文档", "压缩包", "其他"}
|
||||
categoriesList := uploadstats.Categories()
|
||||
categoryMap := make(map[string]distributionItem, len(categoriesList))
|
||||
for _, cat := range categoriesList {
|
||||
categoryMap[cat] = distributionItem{Name: cat}
|
||||
|
||||
@@ -86,13 +86,13 @@ func storeObject(ctx context.Context, objectKey string, reader io.Reader, size i
|
||||
|
||||
driver, backend, err := objectstore.Active(ctx)
|
||||
if err != nil {
|
||||
logger.ErrorF(ctx, "初始化活动存储失败: %v", err)
|
||||
logger.ErrorF(ctx, "Failed to initialize active storage: %v", err)
|
||||
return "", errors.New(shared.ErrSaveFileFailed)
|
||||
}
|
||||
|
||||
result, err := backend.Put(ctx, objectKey, reader, size, mimeType)
|
||||
if err != nil {
|
||||
logger.ErrorF(ctx, "写入 %s 存储失败: %v", driver, err)
|
||||
logger.ErrorF(ctx, "Failed to write to %s storage: %v", driver, err)
|
||||
return "", errors.New(shared.ErrSaveFileFailed)
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ func cleanupUnpersistedObject(ctx context.Context, objectKey string) {
|
||||
return
|
||||
}
|
||||
if err := backend.Delete(ctx, objectKey); err != nil {
|
||||
logger.WarnF(ctx, "清理未写入数据库的上传对象失败: %v", err)
|
||||
logger.WarnF(ctx, "Failed to clean up upload object without a database record: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +152,7 @@ func createDedupRecord(ctx context.Context, existing model.Upload, req Request)
|
||||
if err := persistUploadRecord(ctx, &newUpload, existing.FilePath, false); err != nil {
|
||||
return Result{}, err
|
||||
}
|
||||
logger.InfoF(ctx, "文件触发秒传成功! ID: %d, Path: %s", newUpload.ID, existing.FilePath)
|
||||
logger.InfoF(ctx, "Upload deduplication succeeded ID: %d, Path: %s", newUpload.ID, existing.FilePath)
|
||||
return Result{
|
||||
Upload: newUpload,
|
||||
Created: true,
|
||||
|
||||
@@ -7,36 +7,36 @@ package shared
|
||||
|
||||
// 文件管理常量
|
||||
const (
|
||||
ErrNoFileSelected = "请选择要上传的文件"
|
||||
ErrUnsupportedFormat = "只支持 JPG、PNG、WEBP 格式的图片"
|
||||
ErrProcessFileFailed = "处理文件失败"
|
||||
ErrSaveFileFailed = "保存文件失败"
|
||||
ErrOpenFileFailed = "打开文件失败"
|
||||
ErrSaveUploadRecordFailed = "保存上传记录失败"
|
||||
ErrGenericFileTooLarge = "文件大小不能超过 32MB"
|
||||
ErrFileContentExtensionMismatch = "文件内容与扩展名不匹配,可能包含安全风险"
|
||||
ErrFileValidationFailed = "文件校验失败"
|
||||
ErrInvalidMetadataJSON = "元数据 JSON 格式不合法"
|
||||
ErrInvalidFileID = "无效的文件 ID"
|
||||
ErrQueryUploadRecordFailed = "查询文件记录失败"
|
||||
ErrInvalidBatchDownloadRequest = "参数绑定失败,请传入有效的文件 ID 数组"
|
||||
ErrInvalidIDValueFormat = "无效的 ID 值: %s"
|
||||
ErrRetrieveUploadRecordsFailed = "检索文件记录失败"
|
||||
ErrNoValidFilesForArchive = "没有找到任何有效的文件记录进行打包"
|
||||
ErrInvalidParams = "参数错误"
|
||||
ErrQueryFileCountFailed = "查询文件数量失败"
|
||||
ErrQueryFileListFailed = "查询文件列表失败"
|
||||
ErrDeleteFileFailed = "删除文件失败"
|
||||
ErrReservedUploadType = "系统保留的文件类型不能通过通用文件接口操作"
|
||||
ErrStorageReadOnly = "存储迁移维护中,当前仅允许读取文件"
|
||||
ErrNoFileSelected = "Select a file to upload"
|
||||
ErrUnsupportedFormat = "only JPG, PNG and WEBP images are supported"
|
||||
ErrProcessFileFailed = "Failed to process file"
|
||||
ErrSaveFileFailed = "failed to save file"
|
||||
ErrOpenFileFailed = "Failed to open file"
|
||||
ErrSaveUploadRecordFailed = "Failed to save upload record"
|
||||
ErrGenericFileTooLarge = "File size must not exceed 32 MB"
|
||||
ErrFileContentExtensionMismatch = "File content does not match its extension and may be unsafe"
|
||||
ErrFileValidationFailed = "File validation failed"
|
||||
ErrInvalidMetadataJSON = "Invalid metadata JSON format"
|
||||
ErrInvalidFileID = "Invalid file ID"
|
||||
ErrQueryUploadRecordFailed = "Failed to query file record"
|
||||
ErrInvalidBatchDownloadRequest = "Failed to bind parameters; provide a valid array of file IDs"
|
||||
ErrInvalidIDValueFormat = "Invalid ID value: %s"
|
||||
ErrRetrieveUploadRecordsFailed = "Failed to retrieve file records"
|
||||
ErrNoValidFilesForArchive = "No valid file records were found to archive"
|
||||
ErrInvalidParams = "invalid parameters"
|
||||
ErrQueryFileCountFailed = "Failed to query file count"
|
||||
ErrQueryFileListFailed = "Failed to query file list"
|
||||
ErrDeleteFileFailed = "Failed to delete file"
|
||||
ErrReservedUploadType = "system-reserved file types cannot be managed through the generic file API"
|
||||
ErrStorageReadOnly = "storage migration maintenance is active; files are read-only"
|
||||
ErrS3KeyRequired = "s3 key must not be empty"
|
||||
ErrS3KeyTooLongFormat = "s3 key exceeds maximum length of %d"
|
||||
ErrS3KeyStartsWithSlash = "s3 key must not start with /"
|
||||
ErrS3KeyContainsNullBytes = "s3 key must not contain null bytes"
|
||||
ErrQueryUnusedUploadsFailed = "查询未使用的上传文件失败: %w"
|
||||
ErrImageCacheWarmupPayloadRequired = "图片缓存预热参数不能为空"
|
||||
ErrInvalidImageCacheWarmupPayload = "图片缓存预热参数格式无效: %w"
|
||||
ErrInvalidImageCacheWarmupQuality = "图片质量仅支持 low、medium、high"
|
||||
ErrParseImageCacheWarmupPayload = "解析图片缓存预热参数失败: %w"
|
||||
ErrQueryImagesForCacheWarmup = "查询待预热图片失败: %w"
|
||||
ErrQueryUnusedUploadsFailed = "failed to query unused uploads: %w"
|
||||
ErrImageCacheWarmupPayloadRequired = "image cache warmup parameters must not be empty"
|
||||
ErrInvalidImageCacheWarmupPayload = "invalid image cache warmup parameters: %w"
|
||||
ErrInvalidImageCacheWarmupQuality = "image quality must be low, medium or high"
|
||||
ErrParseImageCacheWarmupPayload = "failed to parse image cache warmup parameters: %w"
|
||||
ErrQueryImagesForCacheWarmup = "failed to query images for cache warmup: %w"
|
||||
)
|
||||
|
||||
@@ -11,6 +11,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// These are persisted counter keys, not display labels. Keep existing keys
|
||||
// stable; the frontend translates their presentation independently.
|
||||
catImage = "图片"
|
||||
catVideo = "视频"
|
||||
catAudio = "音频"
|
||||
@@ -19,6 +21,11 @@ const (
|
||||
catOther = "其他"
|
||||
)
|
||||
|
||||
// Categories returns the legacy persistent category keys in display order.
|
||||
func Categories() []string {
|
||||
return []string{catImage, catVideo, catAudio, catDocument, catArchive, catOther}
|
||||
}
|
||||
|
||||
// GetFileCategory classifies a file by mime type and extension.
|
||||
func GetFileCategory(mimeType, ext string) string {
|
||||
mimeType = strings.ToLower(mimeType)
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
func ReadOnly(ctx context.Context) bool {
|
||||
state := LoadMigrationAccessState(ctx)
|
||||
if state.LoadErr != nil {
|
||||
logger.ErrorF(ctx, "读取存储维护状态失败: %v", state.LoadErr)
|
||||
logger.ErrorF(ctx, "Failed to read storage maintenance state: %v", state.LoadErr)
|
||||
return true
|
||||
}
|
||||
return state.ReadOnly
|
||||
|
||||
@@ -33,8 +33,8 @@ const (
|
||||
var SystemCleanupMeta = task.TaskMeta{
|
||||
Type: TaskTypeSystemCleanup,
|
||||
AsynqTask: SystemCleanupTask,
|
||||
Name: "系统垃圾清理",
|
||||
Description: "定期清理未使用上传文件、历史推送记录和过期任务执行日志",
|
||||
Name: "System cleanup",
|
||||
Description: "Periodically remove unused uploads, historical notifications and expired task execution logs",
|
||||
SupportsTime: false,
|
||||
MaxRetry: task.DefaultMaxRetry,
|
||||
Queue: task.QueueDefault,
|
||||
@@ -56,12 +56,12 @@ func (h *SystemCleanupHandler) Execute(ctx context.Context, _ []byte) (*task.Tas
|
||||
|
||||
oneHourAgo := time.Now().Add(-1 * time.Hour)
|
||||
|
||||
task.AppendLog(ctx, "开始扫描未使用上传文件,阈值: %s", oneHourAgo.Format(time.RFC3339))
|
||||
task.AppendLog(ctx, "Scanning unused uploads, cutoff: %s", oneHourAgo.Format(time.RFC3339))
|
||||
|
||||
for {
|
||||
unusedUploads, err := repository.ListPendingUploadsOlderThan(ctx, lastID, oneHourAgo, batchSize)
|
||||
if err != nil {
|
||||
task.AppendLog(ctx, "查询未使用的上传文件失败: %v", err)
|
||||
task.AppendLog(ctx, "Failed to query unused uploads: %v", err)
|
||||
return nil, fmt.Errorf(shared.ErrQueryUnusedUploadsFailed, err)
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ func (h *SystemCleanupHandler) Execute(ctx context.Context, _ []byte) (*task.Tas
|
||||
break
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "本批次找到 %d 个需要清理的上传文件", len(unusedUploads))
|
||||
task.AppendLog(ctx, "Found %d uploads to clean up in this batch", len(unusedUploads))
|
||||
|
||||
for _, u := range unusedUploads {
|
||||
totalProcessed++
|
||||
@@ -88,7 +88,7 @@ func (h *SystemCleanupHandler) Execute(ctx context.Context, _ []byte) (*task.Tas
|
||||
transitioned, removeErr = ingest.RemoveLockedTx(tx, &locked)
|
||||
return removeErr
|
||||
}); err != nil {
|
||||
task.AppendLog(ctx, "清理上传文件失败 [ID:%d]: %v", u.ID, err)
|
||||
task.AppendLog(ctx, "Failed to clean up upload [ID:%d]: %v", u.ID, err)
|
||||
lastID = u.ID
|
||||
continue
|
||||
}
|
||||
@@ -101,48 +101,48 @@ func (h *SystemCleanupHandler) Execute(ctx context.Context, _ []byte) (*task.Tas
|
||||
}
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "开始清理历史推送审计日志,只保留最近7天数据...")
|
||||
task.AppendLog(ctx, "Cleaning up notification audit history; retaining the last 7 days...")
|
||||
cutoff := time.Now().AddDate(0, 0, -7)
|
||||
pushHistoryCount, err := repository.CountPushHistoriesCreatedBefore(ctx, cutoff)
|
||||
switch {
|
||||
case err != nil:
|
||||
task.AppendLog(ctx, "统计待清理的历史推送记录失败: %v", err)
|
||||
task.AppendLog(ctx, "Failed to count notification history for cleanup: %v", err)
|
||||
case pushHistoryCount == 0:
|
||||
task.AppendLog(ctx, "没有需要清理的历史推送记录 (截止时间: %s)", cutoff.Format("2006-01-02 15:04:05"))
|
||||
task.AppendLog(ctx, "No notification history requires cleanup (cutoff: %s)", cutoff.Format("2006-01-02 15:04:05"))
|
||||
default:
|
||||
if _, delErr := repository.DeletePushHistoriesCreatedBefore(ctx, cutoff); delErr != nil {
|
||||
task.AppendLog(ctx, "删除历史推送记录失败: %v", delErr)
|
||||
task.AppendLog(ctx, "Failed to delete notification history: %v", delErr)
|
||||
} else {
|
||||
task.AppendLog(ctx, "成功删除 %d 条历史推送记录 (截止时间: %s)", pushHistoryCount, cutoff.Format("2006-01-02 15:04:05"))
|
||||
task.AppendLog(ctx, "Deleted %d notification history records (cutoff: %s)", pushHistoryCount, cutoff.Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "开始清理任务执行日志:高频任务保留最近3天,低频任务保留最近30天...")
|
||||
task.AppendLog(ctx, "Cleaning up task execution logs: retaining 3 days for high-frequency tasks and 30 days for low-frequency tasks...")
|
||||
taskLogStats, err := repository.CleanupTaskExecutionLogs(ctx, time.Now())
|
||||
if err != nil {
|
||||
task.AppendLog(ctx, "清理任务执行日志失败: %v", err)
|
||||
logger.ErrorF(ctx, "清理任务执行日志失败: %v", err)
|
||||
task.AppendLog(ctx, "Failed to clean up task execution logs: %v", err)
|
||||
logger.ErrorF(ctx, "Failed to clean up task execution logs: %v", err)
|
||||
} else {
|
||||
task.AppendLog(ctx, "成功清理任务执行日志 %d 条(高频 %d 条,低频 %d 条)",
|
||||
task.AppendLog(ctx, "Cleaned up %d task execution logs (%d high-frequency, %d low-frequency)",
|
||||
taskLogStats.HighFrequencyDeleted+taskLogStats.LowFrequencyDeleted,
|
||||
taskLogStats.HighFrequencyDeleted,
|
||||
taskLogStats.LowFrequencyDeleted,
|
||||
)
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "开始清理过期日志(访问日志按当前日志库保留天数,性能指标按独立短留存)...")
|
||||
task.AppendLog(ctx, "Cleaning up expired logs using log database retention and separate short metric retention...")
|
||||
summary, err := logstore.CleanupExpired(ctx)
|
||||
switch {
|
||||
case err != nil:
|
||||
logger.ErrorF(ctx, "清理过期日志失败: %v", err)
|
||||
task.AppendLog(ctx, "清理过期日志失败: %v", err)
|
||||
logger.ErrorF(ctx, "Failed to clean up expired logs: %v", err)
|
||||
task.AppendLog(ctx, "Failed to clean up expired logs: %v", err)
|
||||
case summary.Deleted == 0:
|
||||
task.AppendLog(ctx, "没有需要清理的过期日志 (访问日志保留 %d 天,性能指标保留 %d 天)", summary.RetentionDays, summary.MetricRetentionDays)
|
||||
task.AppendLog(ctx, "No expired logs require cleanup (access log retention: %d days; metric retention: %d days)", summary.RetentionDays, summary.MetricRetentionDays)
|
||||
default:
|
||||
task.AppendLog(ctx, "日志清理完成:访问日志保留 %d 天,性能指标保留 %d 天,删除 %d 条", summary.RetentionDays, summary.MetricRetentionDays, summary.Deleted)
|
||||
task.AppendLog(ctx, "Log cleanup completed: access log retention %d days, metric retention %d days, deleted %d records", summary.RetentionDays, summary.MetricRetentionDays, summary.Deleted)
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("系统清理完成。成功清理未使用的上传文件 %d/%d 个;清理历史推送审计日志 %d 条;清理任务执行日志 %d 条。",
|
||||
msg := fmt.Sprintf("System cleanup completed. Removed %d/%d unused uploads, %d notification audit records and %d task execution logs.",
|
||||
totalDeleted,
|
||||
totalProcessed,
|
||||
pushHistoryCount,
|
||||
|
||||
@@ -23,8 +23,8 @@ const (
|
||||
var RebuildUploadStatsMeta = task.TaskMeta{
|
||||
Type: TaskTypeRebuildUploadStats,
|
||||
AsynqTask: RebuildUploadStatsTask,
|
||||
Name: "重算文件存储统计",
|
||||
Description: "根据当前 w_uploads 活跃记录全量重建 w_upload_stats(总量、类型、分类、趋势)",
|
||||
Name: "Rebuild file storage statistics",
|
||||
Description: "Rebuild w_upload_stats from active w_uploads records, including totals, types, categories and trends",
|
||||
SupportsTime: false,
|
||||
MaxRetry: task.DefaultMaxRetry,
|
||||
Queue: task.QueueDefault,
|
||||
@@ -38,25 +38,25 @@ type RebuildUploadStatsHandler struct{}
|
||||
func (h *RebuildUploadStatsHandler) Execute(ctx context.Context, _ []byte) (*task.TaskResult, error) {
|
||||
activeCount, err := repository.CountActiveUploads(ctx)
|
||||
if err != nil {
|
||||
task.AppendLog(ctx, "统计活跃上传记录失败: %v", err)
|
||||
task.AppendLog(ctx, "Failed to count active uploads: %v", err)
|
||||
return nil, fmt.Errorf("count active uploads: %w", err)
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "开始重算文件存储统计,活跃记录数: %d", activeCount)
|
||||
task.AppendLog(ctx, "Rebuilding file storage statistics; active records: %d", activeCount)
|
||||
|
||||
if err := uploadstats.RebuildUploadStats(ctx); err != nil {
|
||||
task.AppendLog(ctx, "重算文件存储统计失败: %v", err)
|
||||
task.AppendLog(ctx, "Failed to rebuild file storage statistics: %v", err)
|
||||
return nil, fmt.Errorf("rebuild upload stats: %w", err)
|
||||
}
|
||||
|
||||
totalStat, err := repository.GetTotalUploadStat(ctx)
|
||||
if err != nil {
|
||||
task.AppendLog(ctx, "读取总量统计失败: %v", err)
|
||||
task.AppendLog(ctx, "Failed to read aggregate statistics: %v", err)
|
||||
return nil, fmt.Errorf("load total upload stats: %w", err)
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"文件存储统计重算完成,活跃记录 %d 条,统计文件数 %d,总大小 %d 字节",
|
||||
"File storage statistics rebuilt: %d active records, %d files, %d bytes total",
|
||||
activeCount,
|
||||
totalStat.FileCount,
|
||||
totalStat.FileSize,
|
||||
|
||||
@@ -38,8 +38,8 @@ const (
|
||||
var StorageMigrationMeta = task.TaskMeta{
|
||||
Type: TaskTypeStorageMigration,
|
||||
AsynqTask: StorageMigrationTask,
|
||||
Name: "迁移文件存储",
|
||||
Description: "将活动存储中的文件迁移到待切换的目标存储,迁移期间文件系统保持只读",
|
||||
Name: "Migrate file storage",
|
||||
Description: "Migrate files from active storage to the target backend; files remain read-only during migration",
|
||||
SupportsTime: false,
|
||||
MaxRetry: task.DefaultMaxRetry,
|
||||
Queue: task.QueueDefault,
|
||||
@@ -47,11 +47,11 @@ var StorageMigrationMeta = task.TaskMeta{
|
||||
Params: []task.TaskParam{
|
||||
{
|
||||
Name: "target",
|
||||
Label: "目标存储配置 (JSON)",
|
||||
Label: "Target storage configuration (JSON)",
|
||||
Type: "text",
|
||||
Required: true,
|
||||
Placeholder: `{"driver": "s3", "local": {"root": "."}, "s3": {"bucket": "my-bucket", ...}}`,
|
||||
Description: "待迁移到的目标存储引擎完整配置 JSON 字符串",
|
||||
Description: "Complete JSON configuration of the target storage backend",
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func (h *MigrationHandler) Execute(ctx context.Context, payload []byte) (*task.T
|
||||
return nil, fmt.Errorf("acquire migration lock: %w", err)
|
||||
}
|
||||
if !ok {
|
||||
return nil, errors.New("另一个存储迁移任务正在运行中")
|
||||
return nil, errors.New("another storage migration task is already running")
|
||||
}
|
||||
|
||||
stopRenewal := make(chan struct{})
|
||||
@@ -132,7 +132,7 @@ func (h *MigrationHandler) Execute(ctx context.Context, payload []byte) (*task.T
|
||||
if err := objectstore.SaveActiveConfig(ctx, target); err != nil {
|
||||
return nil, fmt.Errorf("activate same-driver storage config: %w", err)
|
||||
}
|
||||
message := fmt.Sprintf("存储配置已更新,活动存储保持为 %s", target.Driver)
|
||||
message := fmt.Sprintf("Storage configuration updated; active storage remains %s", target.Driver)
|
||||
task.AppendLog(ctx, "%s", message)
|
||||
return &task.TaskResult{Message: message}, nil
|
||||
}
|
||||
@@ -145,7 +145,7 @@ func (h *MigrationHandler) Execute(ctx context.Context, payload []byte) (*task.T
|
||||
if err := objectstore.SaveActiveConfig(ctx, target); err != nil {
|
||||
return nil, fmt.Errorf("activate empty storage config: %w", err)
|
||||
}
|
||||
message := fmt.Sprintf("当前存储没有需要迁移的对象,活动存储已切换为 %s", target.Driver)
|
||||
message := fmt.Sprintf("Current storage has no objects to migrate; active storage switched to %s", target.Driver)
|
||||
task.AppendLog(ctx, "%s", message)
|
||||
return &task.TaskResult{Message: message}, nil
|
||||
}
|
||||
@@ -159,7 +159,7 @@ func (h *MigrationHandler) Execute(ctx context.Context, payload []byte) (*task.T
|
||||
return nil, fmt.Errorf("create target storage: %w", err)
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "开始存储迁移: %s -> %s,总对象数: %d", active.Driver, target.Driver, total)
|
||||
task.AppendLog(ctx, "Starting storage migration: %s -> %s, total objects: %d", active.Driver, target.Driver, total)
|
||||
migrated, err := migrateObjects(ctx, sourceBackend, targetBackend, total)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -168,7 +168,7 @@ func (h *MigrationHandler) Execute(ctx context.Context, payload []byte) (*task.T
|
||||
if err := objectstore.SaveActiveConfig(ctx, target); err != nil {
|
||||
return nil, fmt.Errorf("activate target storage: %w", err)
|
||||
}
|
||||
message := fmt.Sprintf("存储迁移完成,共迁移 %d 个对象,活动存储已切换为 %s", migrated, target.Driver)
|
||||
message := fmt.Sprintf("Storage migration completed; migrated %d objects, active storage switched to %s", migrated, target.Driver)
|
||||
task.AppendLog(ctx, "%s", message)
|
||||
return &task.TaskResult{Message: message}, nil
|
||||
}
|
||||
@@ -201,19 +201,19 @@ func migrateObjects(
|
||||
return migrated.Load(), fmt.Errorf("storage migration canceled: %w", err)
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "正在查询待迁移对象批次,当前已完成迁移: %d/%d", migrated.Load(), total)
|
||||
task.AppendLog(ctx, "Querying next migration batch; migrated: %d/%d", migrated.Load(), total)
|
||||
|
||||
objects, err := repository.ListDistinctActiveStorageObjects(ctx, lastFilePath, batchSize)
|
||||
if err != nil {
|
||||
return migrated.Load(), fmt.Errorf("query source objects: %w", err)
|
||||
}
|
||||
if len(objects) == 0 {
|
||||
task.AppendLog(ctx, "所有对象迁移完毕")
|
||||
task.AppendLog(ctx, "All objects have been migrated")
|
||||
break
|
||||
}
|
||||
|
||||
lastFilePath = objects[len(objects)-1].FilePath
|
||||
task.AppendLog(ctx, "获取当前批次迁移对象,批次大小: %d,实际获取对象数: %d", batchSize, len(objects))
|
||||
task.AppendLog(ctx, "Fetched migration batch; requested size: %d, actual objects: %d", batchSize, len(objects))
|
||||
|
||||
var g errgroup.Group
|
||||
g.SetLimit(migrationConcurrency)
|
||||
@@ -233,7 +233,7 @@ func migrateObjects(
|
||||
return migrated.Load(), err
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "当前批次迁移完成。迁移进度: %d/%d", migrated.Load(), total)
|
||||
task.AppendLog(ctx, "Current migration batch completed. Progress: %d/%d", migrated.Load(), total)
|
||||
}
|
||||
return migrated.Load(), nil
|
||||
}
|
||||
@@ -246,11 +246,11 @@ func migrateSingleObject(
|
||||
sha256HexLength int,
|
||||
) error {
|
||||
if shouldSkipMigration(ctx, targetBackend, obj) {
|
||||
task.AppendLog(ctx, "[跳过迁移] 目标存储已存在相同文件: %s", obj.FilePath)
|
||||
task.AppendLog(ctx, "[Migration skipped] Identical file already exists in target storage: %s", obj.FilePath)
|
||||
return nil
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "[迁移开始] 正在从源存储读取文件: %s", obj.FilePath)
|
||||
task.AppendLog(ctx, "[Migration started] Reading file from source storage: %s", obj.FilePath)
|
||||
source, err := sourceBackend.Get(ctx, obj.FilePath)
|
||||
if err != nil {
|
||||
if isNotFoundError(err) {
|
||||
@@ -258,7 +258,7 @@ func migrateSingleObject(
|
||||
}
|
||||
return fmt.Errorf("open source object %q: %w", obj.FilePath, err)
|
||||
}
|
||||
task.AppendLog(ctx, "[传输中] 正在向目标存储上传文件: %s (大小: %d 字节, 类型: %s)", obj.FilePath, obj.FileSize, obj.MimeType)
|
||||
task.AppendLog(ctx, "[Transfer] Uploading file to target storage: %s (size: %d bytes, type: %s)", obj.FilePath, obj.FileSize, obj.MimeType)
|
||||
targetResult, putErr := targetBackend.Put(ctx, obj.FilePath, source.Body, obj.FileSize, obj.MimeType)
|
||||
closeErr := source.Body.Close()
|
||||
if putErr != nil {
|
||||
@@ -269,7 +269,7 @@ func migrateSingleObject(
|
||||
}
|
||||
|
||||
if len(obj.Hash) == sha256HexLength {
|
||||
task.AppendLog(ctx, "[校验中] 正在对目标文件进行数据一致性校验 (SHA-256): %s", targetResult.Key)
|
||||
task.AppendLog(ctx, "[Verification] Checking target file consistency (SHA-256): %s", targetResult.Key)
|
||||
targetObj, getErr := targetBackend.Get(ctx, targetResult.Key)
|
||||
if getErr != nil {
|
||||
return fmt.Errorf("retrieve target object for verification %q: %w", obj.FilePath, getErr)
|
||||
@@ -287,16 +287,16 @@ func migrateSingleObject(
|
||||
if computedHash != obj.Hash {
|
||||
return fmt.Errorf("integrity check failed for %q: got hash %s, want %s", obj.FilePath, computedHash, obj.Hash)
|
||||
}
|
||||
task.AppendLog(ctx, "[校验通过] 文件一致性校验成功: %s", targetResult.Key)
|
||||
task.AppendLog(ctx, "[Verified] File consistency check succeeded: %s", targetResult.Key)
|
||||
}
|
||||
|
||||
if targetResult.Key != obj.FilePath {
|
||||
task.AppendLog(ctx, "[更新数据库] 正在更新文件路径: %s -> %s", obj.FilePath, targetResult.Key)
|
||||
task.AppendLog(ctx, "[Database update] Updating file path: %s -> %s", obj.FilePath, targetResult.Key)
|
||||
if err := repository.UpdateActiveUploadsFilePath(ctx, obj.FilePath, targetResult.Key); err != nil {
|
||||
return fmt.Errorf("update migrated object %q: %w", obj.FilePath, err)
|
||||
}
|
||||
}
|
||||
task.AppendLog(ctx, "[迁移成功] 文件已完成迁移: %s", targetResult.Key)
|
||||
task.AppendLog(ctx, "[Migration succeeded] File migration completed: %s", targetResult.Key)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -321,7 +321,7 @@ func markMissingMigrationObjectDeleted(
|
||||
filePath string,
|
||||
sourceErr error,
|
||||
) error {
|
||||
task.AppendLog(ctx, "警告: 源存储中物理文件不存在,标记为已删除并跳过: %s (错误: %v)", filePath, sourceErr)
|
||||
task.AppendLog(ctx, "Warning: source file is missing; marking it deleted and skipping: %s (error: %v)", filePath, sourceErr)
|
||||
|
||||
affectedUploads, err := repository.MarkActiveUploadsDeletedByFilePath(ctx, filePath)
|
||||
if err != nil {
|
||||
|
||||
@@ -270,7 +270,7 @@ func TestMigrationHandlerExecuteWithRedisLock(t *testing.T) {
|
||||
if err == nil {
|
||||
t.Fatal("Execute() succeeded when lock was held, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "另一个存储迁移任务正在运行中") {
|
||||
if !strings.Contains(err.Error(), "another storage migration task is already running") {
|
||||
t.Errorf("expected lock warning, got: %v", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -31,8 +31,8 @@ var warmImageCacheMu sync.Mutex
|
||||
var WarmImageCacheMeta = task.TaskMeta{
|
||||
Type: TaskTypeWarmImageCache,
|
||||
AsynqTask: WarmImageCacheTask,
|
||||
Name: "预热图片压缩缓存",
|
||||
Description: "串行将文件管理中的图片转换为指定质量的 WebP 并写入永久缓存",
|
||||
Name: "Warm up compressed image cache",
|
||||
Description: "Sequentially convert managed images to WebP at the specified quality and store them in permanent cache",
|
||||
SupportsTime: false,
|
||||
MaxRetry: task.DefaultMaxRetry,
|
||||
Queue: task.QueueDefault,
|
||||
@@ -40,11 +40,11 @@ var WarmImageCacheMeta = task.TaskMeta{
|
||||
Params: []task.TaskParam{
|
||||
{
|
||||
Name: "quality",
|
||||
Label: "图片质量",
|
||||
Label: "Image quality",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
Placeholder: "low / medium / high",
|
||||
Description: "WebP 压缩质量,仅支持 low、medium、high",
|
||||
Description: "WebP compression quality; must be low, medium or high",
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -82,7 +82,7 @@ func (h *WarmImageCacheHandler) ValidatePayload(payload []byte) ([]byte, error)
|
||||
func (h *WarmImageCacheHandler) Execute(ctx context.Context, payload []byte) (*task.TaskResult, error) {
|
||||
normalizedPayload, err := h.ValidatePayload(payload)
|
||||
if err != nil {
|
||||
task.AppendLog(ctx, "图片缓存预热参数无效: %v", err)
|
||||
task.AppendLog(ctx, "Invalid image cache warmup parameters: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ func (h *WarmImageCacheHandler) Execute(ctx context.Context, payload []byte) (*t
|
||||
return nil, fmt.Errorf(shared.ErrParseImageCacheWarmupPayload, err)
|
||||
}
|
||||
|
||||
task.AppendLog(ctx, "等待获取图片缓存预热执行锁,质量: %s", req.Quality)
|
||||
task.AppendLog(ctx, "Waiting for image cache warmup lock, quality: %s", req.Quality)
|
||||
warmImageCacheMu.Lock()
|
||||
defer warmImageCacheMu.Unlock()
|
||||
|
||||
@@ -105,7 +105,7 @@ func (h *WarmImageCacheHandler) Execute(ctx context.Context, payload []byte) (*t
|
||||
var totalGenerated int
|
||||
var totalFailed int
|
||||
|
||||
task.AppendLog(ctx, "开始串行预热图片压缩缓存,质量: %s,每批: %d", req.Quality, batchSize)
|
||||
task.AppendLog(ctx, "Starting sequential compressed image cache warmup, quality: %s, batch size: %d", req.Quality, batchSize)
|
||||
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
@@ -114,7 +114,7 @@ func (h *WarmImageCacheHandler) Execute(ctx context.Context, payload []byte) (*t
|
||||
|
||||
uploads, err := repository.ListActiveImageUploadsAfterID(ctx, lastID, batchSize)
|
||||
if err != nil {
|
||||
task.AppendLog(ctx, "查询图片上传记录失败: %v", err)
|
||||
task.AppendLog(ctx, "Failed to query image upload records: %v", err)
|
||||
return nil, fmt.Errorf(shared.ErrQueryImagesForCacheWarmup, err)
|
||||
}
|
||||
|
||||
@@ -139,7 +139,7 @@ func (h *WarmImageCacheHandler) Execute(ctx context.Context, payload []byte) (*t
|
||||
totalFailed++
|
||||
batchFailed++
|
||||
if totalFailed <= maxFailureLogs {
|
||||
task.AppendLog(ctx, "图片处理失败 [ID:%d]: %v", upload.ID, err)
|
||||
task.AppendLog(ctx, "Failed to process image [ID:%d]: %v", upload.ID, err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -154,7 +154,7 @@ func (h *WarmImageCacheHandler) Execute(ctx context.Context, payload []byte) (*t
|
||||
|
||||
task.AppendLog(
|
||||
ctx,
|
||||
"批次完成,末尾 ID: %d,生成: %d,命中: %d,失败: %d",
|
||||
"Batch completed, last ID: %d, generated: %d, hits: %d, failed: %d",
|
||||
lastID,
|
||||
batchGenerated,
|
||||
batchCached,
|
||||
@@ -163,7 +163,7 @@ func (h *WarmImageCacheHandler) Execute(ctx context.Context, payload []byte) (*t
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf(
|
||||
"图片缓存预热完成,共处理 %d 张,生成 %d 张,命中 %d 张,失败 %d 张",
|
||||
"Image cache warmup completed; processed %d, generated %d, hits %d, failed %d",
|
||||
totalProcessed,
|
||||
totalGenerated,
|
||||
totalCached,
|
||||
|
||||
@@ -141,7 +141,7 @@ func TestSystemCleanupHandler_Execute(t *testing.T) {
|
||||
// 验证结果
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
assert.Contains(t, result.Message, "系统清理完成。成功清理未使用的上传文件 2/2 个;清理历史推送审计日志 1 条;清理任务执行日志 1 条。")
|
||||
assert.Contains(t, result.Message, "System cleanup completed. Removed 2/2 unused uploads, 1 notification audit records and 1 task execution logs.")
|
||||
|
||||
// 验证数据库状态:pending 且超过1小时的应被标记为 deleted
|
||||
var pendingCount int64
|
||||
@@ -216,7 +216,7 @@ func TestSystemCleanupHandler_ExecuteNoFiles(t *testing.T) {
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, result)
|
||||
assert.Contains(t, result.Message, "系统清理完成。成功清理未使用的上传文件 0/0 个;清理历史推送审计日志 0 条;清理任务执行日志 0 条。")
|
||||
assert.Contains(t, result.Message, "System cleanup completed. Removed 0/0 unused uploads, 0 notification audit records and 0 task execution logs.")
|
||||
}
|
||||
|
||||
func TestSystemCleanupHandler_ImplementsTaskHandler(t *testing.T) {
|
||||
@@ -363,7 +363,7 @@ func TestWarmImageCacheHandlerExecute(t *testing.T) {
|
||||
if result == nil {
|
||||
t.Fatal("Execute() result = nil, want non-nil")
|
||||
}
|
||||
if result.Message != "图片缓存预热完成,共处理 2 张,生成 2 张,命中 0 张,失败 0 张" {
|
||||
if result.Message != "Image cache warmup completed; processed 2, generated 2, hits 0, failed 0" {
|
||||
t.Errorf("Execute() message = %q, want generated summary", result.Message)
|
||||
}
|
||||
|
||||
@@ -383,7 +383,7 @@ func TestWarmImageCacheHandlerExecute(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("second Execute(%s) returned error: %v", payload, err)
|
||||
}
|
||||
if secondResult.Message != "图片缓存预热完成,共处理 2 张,生成 0 张,命中 2 张,失败 0 张" {
|
||||
if secondResult.Message != "Image cache warmup completed; processed 2, generated 0, hits 2, failed 0" {
|
||||
t.Errorf("second Execute() message = %q, want cache-hit summary", secondResult.Message)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user