diff --git a/bot/handlers.go b/bot/handlers.go index 90f52cc..bb9eb4c 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -63,7 +63,7 @@ SaveAny Bot - 转存你的 Telegram 文件 /help - 显示帮助 /silent - 静默模式 /storage - 设置默认存储位置 -/save - 保存文件 +/save [自定义文件名] - 保存文件 静默模式: 开启后 Bot 直接保存到收到的文件到默认位置, 不再询问 ` diff --git a/core/core.go b/core/core.go index 94da471..5adb210 100644 --- a/core/core.go +++ b/core/core.go @@ -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 } diff --git a/storage/local/local.go b/storage/local/local.go index b67b028..b377ee4 100644 --- a/storage/local/local.go +++ b/storage/local/local.go @@ -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) }