Skip to content

Commit

Permalink
refactor: update logging levels and remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
krau committed Nov 9, 2024
1 parent 71e5007 commit 454d69c
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 63 deletions.
4 changes: 2 additions & 2 deletions bot/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions common/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion common/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package common

func Init() {
initClient()
initCache()
}
35 changes: 0 additions & 35 deletions common/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
19 changes: 0 additions & 19 deletions common/req.go

This file was deleted.

3 changes: 2 additions & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"time"

"github.com/celestix/gotgproto/ext"
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion storage/webdav/webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 454d69c

Please sign in to comment.