* refactor: a big refactor. wip * refactor: port handle file * refactor: place all handlers * fix: task info nil pointer * feat: enhance task progress tracking and context management * feat: cancel task * feat: stream mode * feat: silent mode * feat: dir cmd * refactor: remove unused old file * feat: rule cmd * feat: handle silent mode * feat: batch task * fix: batch task progress and temp file cleanup * refactor: update file creation and cleanup methods for better resource management * feat: add save command with silent mode handling * feat: message link * feat: update message prompts to include file count in storage selection * feat: slient save links * refactor: reduce dup code * feat: rule type * feat: chose dir * feat: refactor file handling and storage rules, improve error handling and logging * feat: rule mode * feat: telegraph pics * fix: tphpics nil pointer and inaccurate dirpath * feat: silent save telegraph * feat: add suffix to avoid file overwrite * feat: new storage telegram * chore: tidy go mod
101 lines
2.4 KiB
Go
101 lines
2.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"slices"
|
|
|
|
"github.com/charmbracelet/log"
|
|
"github.com/krau/SaveAny-Bot/client/bot"
|
|
userclient "github.com/krau/SaveAny-Bot/client/user"
|
|
"github.com/krau/SaveAny-Bot/common/i18n"
|
|
"github.com/krau/SaveAny-Bot/common/i18n/i18nk"
|
|
"github.com/krau/SaveAny-Bot/common/utils/fsutil"
|
|
"github.com/krau/SaveAny-Bot/config"
|
|
"github.com/krau/SaveAny-Bot/core"
|
|
"github.com/krau/SaveAny-Bot/database"
|
|
"github.com/krau/SaveAny-Bot/storage"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func Run(cmd *cobra.Command, _ []string) {
|
|
ctx := cmd.Context()
|
|
logger := log.NewWithOptions(os.Stdout, log.Options{
|
|
Level: log.DebugLevel,
|
|
ReportTimestamp: true,
|
|
TimeFormat: time.TimeOnly,
|
|
ReportCaller: true,
|
|
})
|
|
ctx = log.WithContext(ctx, logger)
|
|
|
|
initAll(ctx)
|
|
core.Run(ctx)
|
|
|
|
<-ctx.Done()
|
|
logger.Info(i18n.T(i18nk.Exiting))
|
|
defer logger.Info(i18n.T(i18nk.Bye))
|
|
cleanCache()
|
|
}
|
|
|
|
func initAll(ctx context.Context) {
|
|
if err := config.Init(ctx); err != nil {
|
|
fmt.Println("Failed to load config:", err)
|
|
os.Exit(1)
|
|
}
|
|
logger := log.FromContext(ctx)
|
|
i18n.Init(config.Cfg.Lang)
|
|
logger.Info(i18n.T(i18nk.Initing))
|
|
if config.Cfg.Telegram.Userbot.Enable {
|
|
uc, err := userclient.Login(ctx)
|
|
if err != nil {
|
|
logger.Fatalf("User client login failed: %s", err)
|
|
}
|
|
logger.Infof("User client logged in as %s", uc.Self.FirstName)
|
|
}
|
|
database.Init(ctx)
|
|
storage.LoadStorages(ctx)
|
|
|
|
bot.Init(ctx)
|
|
}
|
|
|
|
func cleanCache() {
|
|
if config.Cfg.NoCleanCache {
|
|
return
|
|
}
|
|
if config.Cfg.Temp.BasePath != "" && !config.Cfg.Stream {
|
|
if slices.Contains([]string{"/", ".", "\\", ".."}, filepath.Clean(config.Cfg.Temp.BasePath)) {
|
|
log.Error(i18n.T(i18nk.InvalidCacheDir, map[string]any{
|
|
"Path": config.Cfg.Temp.BasePath,
|
|
}))
|
|
return
|
|
}
|
|
currentDir, err := os.Getwd()
|
|
if err != nil {
|
|
log.Error(i18n.T(i18nk.GetWorkdirFailed, map[string]any{
|
|
"Error": err,
|
|
}))
|
|
return
|
|
}
|
|
cachePath := filepath.Join(currentDir, config.Cfg.Temp.BasePath)
|
|
cachePath, err = filepath.Abs(cachePath)
|
|
if err != nil {
|
|
log.Error(i18n.T(i18nk.GetCacheAbsPathFailed, map[string]any{
|
|
"Error": err,
|
|
}))
|
|
return
|
|
}
|
|
log.Info(i18n.T(i18nk.CleaningCache, map[string]any{
|
|
"Path": cachePath,
|
|
}))
|
|
if err := fsutil.RemoveAllInDir(cachePath); err != nil {
|
|
log.Error(i18n.T(i18nk.CleanCacheFailed, map[string]any{
|
|
"Error": err,
|
|
}))
|
|
}
|
|
}
|
|
}
|