mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-06-07 16:39:55 +08:00
feat: rename file only when storagePath exists
This commit is contained in:
@@ -27,13 +27,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func processPendingTask(task *types.Task) error {
|
func processPendingTask(task *types.Task) error {
|
||||||
common.Log.Debugf("Start processing task: %s", task.String())
|
common.Log.Infof("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)
|
|
||||||
}
|
|
||||||
|
|
||||||
if task.FileName() == "" {
|
if task.FileName() == "" {
|
||||||
task.File.FileName = fmt.Sprintf("%d_%d_%s", task.FileChatID, task.FileMessageID, task.File.Hash())
|
task.File.FileName = fmt.Sprintf("%d_%d_%s", task.FileChatID, task.FileMessageID, task.File.Hash())
|
||||||
|
|||||||
31
core/rule.go
31
core/rule.go
@@ -26,23 +26,30 @@ func getStorageAndPathForTask(task *types.Task) (storage.Storage, string, error)
|
|||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
storagePath := taskStorage.JoinStoragePath(*task)
|
storagePath := taskStorage.JoinStoragePath(*task)
|
||||||
if !user.ApplyRule || user.Rules == nil {
|
|
||||||
return taskStorage, storagePath, nil
|
|
||||||
}
|
|
||||||
var ruleTaskStorage storage.Storage
|
var ruleTaskStorage storage.Storage
|
||||||
var ruleStoragePath string
|
var ruleStoragePath string
|
||||||
for _, rule := range user.Rules {
|
if user.ApplyRule && user.Rules != nil {
|
||||||
matchStorage, matchStoragePath := applyRule(&rule, *task)
|
for _, rule := range user.Rules {
|
||||||
if matchStorage != nil && matchStoragePath != "" {
|
matchStorage, matchStoragePath := applyRule(&rule, *task)
|
||||||
ruleTaskStorage = matchStorage
|
if matchStorage != nil && matchStoragePath != "" {
|
||||||
ruleStoragePath = 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) {
|
func applyRule(rule *dao.Rule, task types.Task) (storage.Storage, string) {
|
||||||
|
|||||||
@@ -147,3 +147,8 @@ func (a *Alist) NotSupportStream() string {
|
|||||||
func (a *Alist) JoinStoragePath(task types.Task) string {
|
func (a *Alist) JoinStoragePath(task types.Task) string {
|
||||||
return path.Join(a.config.BasePath, task.StoragePath)
|
return path.Join(a.config.BasePath, task.StoragePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *Alist) Exists(ctx context.Context, storagePath string) bool {
|
||||||
|
// TODO: Implement it.
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -63,3 +63,11 @@ func (l *Local) Save(ctx context.Context, r io.Reader, storagePath string) error
|
|||||||
_, err = io.Copy(file, r)
|
_, err = io.Copy(file, r)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *Local) Exists(ctx context.Context, storagePath string) bool {
|
||||||
|
absPath, err := filepath.Abs(storagePath)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return fileutil.IsExist(absPath)
|
||||||
|
}
|
||||||
|
|||||||
@@ -70,3 +70,17 @@ func (m *Minio) Save(ctx context.Context, r io.Reader, storagePath string) error
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Minio) Exists(ctx context.Context, storagePath string) bool {
|
||||||
|
common.Log.Debugf("Checking if file exists at %s", storagePath)
|
||||||
|
// TODO: test it.
|
||||||
|
_, err := m.client.StatObject(ctx, m.config.BucketName, storagePath, minio.StatObjectOptions{})
|
||||||
|
if err != nil {
|
||||||
|
if minio.ToErrorResponse(err).Code == "NoSuchKey" {
|
||||||
|
return false // File does not exist
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ type Storage interface {
|
|||||||
Name() string
|
Name() string
|
||||||
JoinStoragePath(task types.Task) string
|
JoinStoragePath(task types.Task) string
|
||||||
Save(ctx context.Context, reader io.Reader, storagePath string) error
|
Save(ctx context.Context, reader io.Reader, storagePath string) error
|
||||||
|
Exists(ctx context.Context, storagePath string) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type StorageNotSupportStream interface {
|
type StorageNotSupportStream interface {
|
||||||
|
|||||||
@@ -5,4 +5,5 @@ import "errors"
|
|||||||
var (
|
var (
|
||||||
ErrFailedToCreateDirectory = errors.New("webdav: failed to create directory")
|
ErrFailedToCreateDirectory = errors.New("webdav: failed to create directory")
|
||||||
ErrFailedToWriteFile = errors.New("webdav: failed to write file")
|
ErrFailedToWriteFile = errors.New("webdav: failed to write file")
|
||||||
|
ErrFailedToCheckFileExists = errors.New("webdav: failed to check if file exists")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -57,3 +57,13 @@ func (w *Webdav) Save(ctx context.Context, r io.Reader, storagePath string) erro
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Webdav) Exists(ctx context.Context, storagePath string) bool {
|
||||||
|
common.Log.Debugf("Checking if file exists at %s", storagePath)
|
||||||
|
exists, err := w.client.Exists(ctx, storagePath)
|
||||||
|
if err != nil {
|
||||||
|
common.Log.Errorf("Failed to check if file exists at %s: %v", storagePath, err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return exists
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user