feat: support custom storage path in filename

This commit is contained in:
krau
2025-02-12 12:24:11 +08:00
parent 930e838b2e
commit 0fb5634874
3 changed files with 19 additions and 4 deletions

View File

@@ -63,7 +63,7 @@ SaveAny Bot - 转存你的 Telegram 文件
/help - 显示帮助
/silent - 静默模式
/storage - 设置默认存储位置
/save - 保存文件
/save [自定义文件名] - 保存文件
静默模式: 开启后 Bot 直接保存到收到的文件到默认位置, 不再询问
`

View File

@@ -10,6 +10,7 @@ import (
"time"
"github.com/celestix/gotgproto/ext"
"github.com/duke-git/lancet/v2/fileutil"
"github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/bot"
"github.com/krau/SaveAny-Bot/config"
@@ -20,7 +21,14 @@ import (
func processPendingTask(task *types.Task) error {
logger.L.Debugf("Start processing task: %s", task.String())
os.MkdirAll(config.Cfg.Temp.BasePath, os.ModePerm)
destPath := filepath.Join(config.Cfg.Temp.BasePath, task.FileName())
absDestPath, err := filepath.Abs(destPath)
if err != nil {
return fmt.Errorf("Failed to get absolute path: %w", err)
}
if err := fileutil.CreateDir(filepath.Dir(absDestPath)); err != nil {
return fmt.Errorf("Failed to create directory: %w", err)
}
ctx := task.Ctx.(*ext.Context)
ctx.EditMessage(task.ChatID, &tg.MessagesEditMessageRequest{
@@ -28,7 +36,6 @@ func processPendingTask(task *types.Task) error {
ID: task.ReplyMessageID,
})
destPath := filepath.Join(config.Cfg.Temp.BasePath, task.File.FileName)
if task.StoragePath == "" {
task.StoragePath = task.File.FileName
}

View File

@@ -21,5 +21,13 @@ func (l *Local) Init() {
}
func (l *Local) Save(ctx context.Context, filePath, storagePath string) error {
return fileutil.CopyFile(filePath, filepath.Join(config.Cfg.Storage.Local.BasePath, storagePath))
storagePath = filepath.Join(config.Cfg.Storage.Local.BasePath, storagePath)
absPath, err := filepath.Abs(storagePath)
if err != nil {
return err
}
if err := fileutil.CreateDir(filepath.Dir(absPath)); err != nil {
return err
}
return fileutil.CopyFile(filePath, storagePath)
}