diff --git a/bot/handlers.go b/bot/handlers.go index 6b8f2f1..ab741df 100644 --- a/bot/handlers.go +++ b/bot/handlers.go @@ -135,7 +135,7 @@ func saveCmd(ctx *ext.Context, update *ext.Update) error { } func handleFileMessage(ctx *ext.Context, update *ext.Update) error { - logger.L.Debug("Got media: ", update.EffectiveMessage.Media.TypeName()) + logger.L.Trace("Got media: ", update.EffectiveMessage.Media.TypeName()) supported, err := supportedMediaFilter(update.EffectiveMessage) if err != nil { return err @@ -226,7 +226,7 @@ func handleFileMessage(ctx *ext.Context, update *ext.Update) error { func AddToQueue(ctx *ext.Context, update *ext.Update) error { args := strings.Split(string(update.CallbackQuery.Data), " ") messageID, _ := strconv.Atoi(args[1]) - logger.L.Debugf("Got add to queue: chatID: %d, messageID: %d, storage: %s", update.EffectiveChat().GetID(), messageID, args[2]) + logger.L.Trace("Got add to queue: chatID: %d, messageID: %d, storage: %s", update.EffectiveChat().GetID(), messageID, args[2]) record, err := dao.GetReceivedFileByChatAndMessageID(update.EffectiveChat().GetID(), messageID) if err != nil { logger.L.Errorf("Failed to get received file: %s", err) diff --git a/common/cache.go b/common/cache.go index 7916a37..5a9ffe7 100644 --- a/common/cache.go +++ b/common/cache.go @@ -23,10 +23,6 @@ func initCache() { Cache = &CommonCache{cache: freecache.NewCache(10 * 1024 * 1024)} } -func GetCache() *CommonCache { - return Cache -} - func (c *CommonCache) Get(key string, value *types.File) error { c.mu.RLock() defer c.mu.RUnlock() diff --git a/common/common.go b/common/common.go index 3813ecc..f15d177 100644 --- a/common/common.go +++ b/common/common.go @@ -1,6 +1,5 @@ package common func Init() { - initClient() initCache() } diff --git a/common/os.go b/common/os.go index c97d55f..255458e 100644 --- a/common/os.go +++ b/common/os.go @@ -9,20 +9,6 @@ import ( "github.com/krau/SaveAny-Bot/logger" ) -// 创建文件, 自动创建目录 -func MkFile(path string, data []byte) error { - err := os.MkdirAll(filepath.Dir(path), os.ModePerm) - if err != nil { - return err - } - return os.WriteFile(path, data, os.ModePerm) -} - -func FileExists(path string) bool { - _, err := os.Stat(path) - return err == nil -} - // 删除文件, 并清理空目录. 如果文件不存在则返回 nil func PurgeFile(path string) error { if err := os.Remove(path); err != nil { @@ -60,24 +46,3 @@ func RemoveEmptyDirectories(dirPath string) error { } return nil } - -// 在指定时间后删除和清理文件 (定时器) -func PurgeFileAfter(path string, td time.Duration) { - _, err := os.Stat(path) - if err != nil { - logger.L.Errorf("Failed to create timer for %s: %s", path, err) - return - } - logger.L.Debugf("Purge file after %s: %s", td, path) - time.AfterFunc(td, func() { - PurgeFile(path) - }) -} - -func MkCache(path string, data []byte, td time.Duration) { - if err := MkFile(path, data); err != nil { - logger.L.Errorf("failed to save cache file: %s", err) - } else { - go PurgeFileAfter(path, td) - } -} diff --git a/common/req.go b/common/req.go deleted file mode 100644 index e0a5926..0000000 --- a/common/req.go +++ /dev/null @@ -1,19 +0,0 @@ -package common - -import ( - "path/filepath" - "time" - - "github.com/imroc/req/v3" - "github.com/krau/SaveAny-Bot/config" -) - -var ReqClient *req.Client - -func initClient() { - ReqClient = req.NewClient().SetOutputDirectory(config.Cfg.Temp.BasePath).SetTimeout(86400 * time.Second) -} - -func GetCacheFilePath(filename string) string { - return filepath.Join(config.Cfg.Temp.BasePath, filename) -} diff --git a/core/core.go b/core/core.go index d213b0b..497450f 100644 --- a/core/core.go +++ b/core/core.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "os" + "path/filepath" "time" "github.com/celestix/gotgproto/ext" @@ -70,7 +71,7 @@ func processPendingTask(task *types.Task) error { } defer readCloser.Close() - dest, err := os.Create(common.GetCacheFilePath(task.FileName())) + dest, err := os.Create(filepath.Join(config.Cfg.Temp.BasePath, task.File.FileName)) if err != nil { return fmt.Errorf("Failed to create file: %w", err) } diff --git a/storage/webdav/webdav.go b/storage/webdav/webdav.go index add6514..e7dc7a1 100644 --- a/storage/webdav/webdav.go +++ b/storage/webdav/webdav.go @@ -32,7 +32,7 @@ func (w *Webdav) Init() { } func (w *Webdav) Save(ctx context.Context, filePath, storagePath string) error { - storagePath = filepath.Join(basePath, storagePath) + storagePath = basePath + "/" + storagePath if err := Client.MkdirAll(filepath.Dir(storagePath), os.ModePerm); err != nil { logger.L.Errorf("Failed to create directory %s: %v", filepath.Dir(storagePath), err) return errors.New("webdav: failed to create directory")