mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-06-29 19:21:25 +08:00
refactor: refactor task logic for better scalability (#76)
* 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
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// cmd/gen_i18n/main.go
|
||||
// cmd/geni18n/main.go
|
||||
package main
|
||||
|
||||
import (
|
||||
@@ -14,8 +14,8 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
dir := flag.String("dir", "./i18n/locale", "Locales directory path")
|
||||
out := flag.String("out", "i18n/i18nk/keys.go", "Output file path")
|
||||
dir := flag.String("dir", "./common/i18n/locale", "Locales directory path")
|
||||
out := flag.String("out", "common/i18n/i18nk/keys.go", "Output file path")
|
||||
pkg := flag.String("pkg", "i18nk", "Package name for generated file")
|
||||
flag.Parse()
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -12,8 +13,8 @@ var rootCmd = &cobra.Command{
|
||||
Run: Run,
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
func Execute(ctx context.Context) {
|
||||
if err := rootCmd.ExecuteContext(ctx); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
100
cmd/run.go
100
cmd/run.go
@@ -4,46 +4,78 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"slices"
|
||||
|
||||
"github.com/krau/SaveAny-Bot/bot"
|
||||
"github.com/krau/SaveAny-Bot/common"
|
||||
"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/dao"
|
||||
"github.com/krau/SaveAny-Bot/i18n"
|
||||
"github.com/krau/SaveAny-Bot/i18n/i18nk"
|
||||
"github.com/krau/SaveAny-Bot/database"
|
||||
"github.com/krau/SaveAny-Bot/storage"
|
||||
"github.com/krau/SaveAny-Bot/userclient"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func Run(_ *cobra.Command, _ []string) {
|
||||
InitAll()
|
||||
core.Run()
|
||||
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)
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
sig := <-quit
|
||||
common.Log.Info(sig, i18n.T(i18nk.Exiting))
|
||||
defer common.Log.Info(i18n.T(i18nk.Bye))
|
||||
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)) {
|
||||
common.Log.Error(i18n.T(i18nk.InvalidCacheDir, map[string]any{
|
||||
log.Error(i18n.T(i18nk.InvalidCacheDir, map[string]any{
|
||||
"Path": config.Cfg.Temp.BasePath,
|
||||
}))
|
||||
return
|
||||
}
|
||||
currentDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
common.Log.Error(i18n.T(i18nk.GetWorkdirFailed, map[string]any{
|
||||
log.Error(i18n.T(i18nk.GetWorkdirFailed, map[string]any{
|
||||
"Error": err,
|
||||
}))
|
||||
return
|
||||
@@ -51,42 +83,18 @@ func Run(_ *cobra.Command, _ []string) {
|
||||
cachePath := filepath.Join(currentDir, config.Cfg.Temp.BasePath)
|
||||
cachePath, err = filepath.Abs(cachePath)
|
||||
if err != nil {
|
||||
common.Log.Error(i18n.T(i18nk.GetCacheAbsPathFailed, map[string]any{
|
||||
log.Error(i18n.T(i18nk.GetCacheAbsPathFailed, map[string]any{
|
||||
"Error": err,
|
||||
}))
|
||||
return
|
||||
}
|
||||
common.Log.Info(i18n.T(i18nk.CleaningCache, map[string]any{
|
||||
log.Info(i18n.T(i18nk.CleaningCache, map[string]any{
|
||||
"Path": cachePath,
|
||||
}))
|
||||
if err := common.RemoveAllInDir(cachePath); err != nil {
|
||||
common.Log.Error(i18n.T(i18nk.CleanCacheFailed, map[string]any{
|
||||
if err := fsutil.RemoveAllInDir(cachePath); err != nil {
|
||||
log.Error(i18n.T(i18nk.CleanCacheFailed, map[string]any{
|
||||
"Error": err,
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func InitAll() {
|
||||
if err := config.Init(); err != nil {
|
||||
fmt.Println("Failed to load config:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
common.InitLogger()
|
||||
i18n.Init(config.Cfg.Lang)
|
||||
common.Log.Info(i18n.T(i18nk.Initing))
|
||||
dao.Init()
|
||||
storage.LoadStorages()
|
||||
common.Init()
|
||||
if config.Cfg.Telegram.Userbot.Enable {
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer cancel()
|
||||
uc, err := userclient.Login(ctx)
|
||||
if err != nil {
|
||||
common.Log.Errorf("User client login failed: %s", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
common.Log.Infof("User client logged in as %s", uc.Self.FirstName)
|
||||
}
|
||||
bot.Init()
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/krau/SaveAny-Bot/common"
|
||||
"github.com/krau/SaveAny-Bot/pkg/consts"
|
||||
"github.com/rhysd/go-github-selfupdate/selfupdate"
|
||||
|
||||
"github.com/blang/semver"
|
||||
@@ -16,7 +16,7 @@ var VersionCmd = &cobra.Command{
|
||||
Aliases: []string{"v"},
|
||||
Short: "Print the version number of saveany-bot",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Printf("saveany-bot version: %s %s/%s\nBuildTime: %s, Commit: %s\n", common.Version, runtime.GOOS, runtime.GOARCH, common.BuildTime, common.GitCommit)
|
||||
fmt.Printf("saveany-bot version: %s %s/%s\nBuildTime: %s, Commit: %s\n", consts.Version, runtime.GOOS, runtime.GOARCH, consts.BuildTime, consts.GitCommit)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -25,14 +25,14 @@ var upgradeCmd = &cobra.Command{
|
||||
Aliases: []string{"up"},
|
||||
Short: "Upgrade saveany-bot to the latest version",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
v := semver.MustParse(common.Version)
|
||||
v := semver.MustParse(consts.Version)
|
||||
latest, err := selfupdate.UpdateSelf(v, "krau/SaveAny-Bot")
|
||||
if err != nil {
|
||||
fmt.Println("Binary update failed:", err)
|
||||
return
|
||||
}
|
||||
if latest.Version.Equals(v) {
|
||||
fmt.Println("Current binary is the latest version", common.Version)
|
||||
fmt.Println("Current binary is the latest version", consts.Version)
|
||||
} else {
|
||||
fmt.Println("Successfully updated to version", latest.Version)
|
||||
fmt.Println("Release note:\n", latest.ReleaseNotes)
|
||||
|
||||
Reference in New Issue
Block a user