mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-06-09 01:20:37 +08:00
refactor: update logging levels and remove unused code
This commit is contained in:
@@ -135,7 +135,7 @@ func saveCmd(ctx *ext.Context, update *ext.Update) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func handleFileMessage(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)
|
supported, err := supportedMediaFilter(update.EffectiveMessage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -226,7 +226,7 @@ func handleFileMessage(ctx *ext.Context, update *ext.Update) error {
|
|||||||
func AddToQueue(ctx *ext.Context, update *ext.Update) error {
|
func AddToQueue(ctx *ext.Context, update *ext.Update) error {
|
||||||
args := strings.Split(string(update.CallbackQuery.Data), " ")
|
args := strings.Split(string(update.CallbackQuery.Data), " ")
|
||||||
messageID, _ := strconv.Atoi(args[1])
|
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)
|
record, err := dao.GetReceivedFileByChatAndMessageID(update.EffectiveChat().GetID(), messageID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.L.Errorf("Failed to get received file: %s", err)
|
logger.L.Errorf("Failed to get received file: %s", err)
|
||||||
|
|||||||
@@ -23,10 +23,6 @@ func initCache() {
|
|||||||
Cache = &CommonCache{cache: freecache.NewCache(10 * 1024 * 1024)}
|
Cache = &CommonCache{cache: freecache.NewCache(10 * 1024 * 1024)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetCache() *CommonCache {
|
|
||||||
return Cache
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *CommonCache) Get(key string, value *types.File) error {
|
func (c *CommonCache) Get(key string, value *types.File) error {
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
defer c.mu.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
initClient()
|
|
||||||
initCache()
|
initCache()
|
||||||
}
|
}
|
||||||
|
|||||||
35
common/os.go
35
common/os.go
@@ -9,20 +9,6 @@ import (
|
|||||||
"github.com/krau/SaveAny-Bot/logger"
|
"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
|
// 删除文件, 并清理空目录. 如果文件不存在则返回 nil
|
||||||
func PurgeFile(path string) error {
|
func PurgeFile(path string) error {
|
||||||
if err := os.Remove(path); err != nil {
|
if err := os.Remove(path); err != nil {
|
||||||
@@ -60,24 +46,3 @@ func RemoveEmptyDirectories(dirPath string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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)
|
|
||||||
}
|
|
||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/celestix/gotgproto/ext"
|
"github.com/celestix/gotgproto/ext"
|
||||||
@@ -70,7 +71,7 @@ func processPendingTask(task *types.Task) error {
|
|||||||
}
|
}
|
||||||
defer readCloser.Close()
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to create file: %w", err)
|
return fmt.Errorf("Failed to create file: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func (w *Webdav) Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *Webdav) Save(ctx context.Context, filePath, storagePath string) error {
|
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 {
|
if err := Client.MkdirAll(filepath.Dir(storagePath), os.ModePerm); err != nil {
|
||||||
logger.L.Errorf("Failed to create directory %s: %v", filepath.Dir(storagePath), err)
|
logger.L.Errorf("Failed to create directory %s: %v", filepath.Dir(storagePath), err)
|
||||||
return errors.New("webdav: failed to create directory")
|
return errors.New("webdav: failed to create directory")
|
||||||
|
|||||||
Reference in New Issue
Block a user