feat: rename file only when storagePath exists

This commit is contained in:
krau
2025-06-11 09:54:08 +08:00
parent 9d3a3a8dcd
commit e85d3c9441
8 changed files with 59 additions and 19 deletions

View File

@@ -27,13 +27,7 @@ import (
)
func processPendingTask(task *types.Task) error {
common.Log.Debugf("Start processing task: %s", task.String())
if task.FileName() != "" && !task.IsTelegraph && task.File.FileSize != 0 && task.FileDBID != 0 {
ext := path.Ext(task.FileName())
name := task.FileName()[:len(task.FileName())-len(ext)]
task.File.FileName = fmt.Sprintf("%s_%d%s", name, task.FileDBID, ext)
}
common.Log.Infof("Start processing task: %s", task.String())
if task.FileName() == "" {
task.File.FileName = fmt.Sprintf("%d_%d_%s", task.FileChatID, task.FileMessageID, task.File.Hash())

View File

@@ -26,23 +26,30 @@ func getStorageAndPathForTask(task *types.Task) (storage.Storage, string, error)
return nil, "", err
}
storagePath := taskStorage.JoinStoragePath(*task)
if !user.ApplyRule || user.Rules == nil {
return taskStorage, storagePath, nil
}
var ruleTaskStorage storage.Storage
var ruleStoragePath string
for _, rule := range user.Rules {
matchStorage, matchStoragePath := applyRule(&rule, *task)
if matchStorage != nil && matchStoragePath != "" {
ruleTaskStorage = matchStorage
ruleStoragePath = matchStoragePath
if user.ApplyRule && user.Rules != nil {
for _, rule := range user.Rules {
matchStorage, matchStoragePath := applyRule(&rule, *task)
if matchStorage != nil && matchStoragePath != "" {
ruleTaskStorage = matchStorage
ruleStoragePath = matchStoragePath
common.Log.Debugf("Rule matched: %s, %s", ruleTaskStorage.Name(), ruleStoragePath)
return ruleTaskStorage, ruleStoragePath, nil
}
}
}
if ruleStoragePath == "" || ruleTaskStorage == nil {
return taskStorage, storagePath, nil
if taskStorage.Exists(task.Ctx, storagePath) {
ext := path.Ext(task.FileName())
name := task.FileName()[:len(task.FileName())-len(ext)]
task.File.FileName = fmt.Sprintf("%s_%d%s", name, task.FileDBID, ext)
task.StoragePath = task.File.FileName
storagePath = taskStorage.JoinStoragePath(*task)
}
common.Log.Debugf("Rule matched: %s, %s", ruleTaskStorage.Name(), ruleStoragePath)
return ruleTaskStorage, ruleStoragePath, nil
return taskStorage, storagePath, nil
}
func applyRule(rule *dao.Rule, task types.Task) (storage.Storage, string) {