mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-05-11 22:39:41 +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:
@@ -9,7 +9,7 @@ import (
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/krau/SaveAny-Bot/types"
|
||||
"github.com/krau/SaveAny-Bot/pkg/enums/key"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
@@ -54,7 +54,7 @@ func (c *Client) doRequest(ctx context.Context, method WebdavMethod, url string,
|
||||
req.Header.Set("Depth", "1")
|
||||
}
|
||||
if method == WebdavMethodPut && ctx != nil {
|
||||
if length := ctx.Value(types.ContextKeyContentLength); length != nil {
|
||||
if length := ctx.Value(key.ContextKeyContentLength); length != nil {
|
||||
if l, ok := length.(int64); ok {
|
||||
req.ContentLength = l
|
||||
}
|
||||
|
||||
@@ -6,19 +6,21 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/krau/SaveAny-Bot/common"
|
||||
"github.com/charmbracelet/log"
|
||||
config "github.com/krau/SaveAny-Bot/config/storage"
|
||||
"github.com/krau/SaveAny-Bot/types"
|
||||
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
|
||||
)
|
||||
|
||||
type Webdav struct {
|
||||
config config.WebdavStorageConfig
|
||||
client *Client
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
func (w *Webdav) Init(cfg config.StorageConfig) error {
|
||||
func (w *Webdav) Init(ctx context.Context, cfg config.StorageConfig) error {
|
||||
webdavConfig, ok := cfg.(*config.WebdavStorageConfig)
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to cast webdav config")
|
||||
@@ -27,42 +29,51 @@ func (w *Webdav) Init(cfg config.StorageConfig) error {
|
||||
return err
|
||||
}
|
||||
w.config = *webdavConfig
|
||||
w.logger = log.FromContext(ctx).WithPrefix(fmt.Sprintf("webdav[%s]", w.config.Name))
|
||||
w.client = NewClient(w.config.URL, w.config.Username, w.config.Password, &http.Client{
|
||||
Timeout: time.Hour * 12,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Webdav) Type() types.StorageType {
|
||||
return types.StorageTypeWebdav
|
||||
func (w *Webdav) Type() storenum.StorageType {
|
||||
return storenum.Webdav
|
||||
}
|
||||
|
||||
func (w *Webdav) Name() string {
|
||||
return w.config.Name
|
||||
}
|
||||
|
||||
func (w *Webdav) JoinStoragePath(task types.Task) string {
|
||||
return path.Join(w.config.BasePath, task.StoragePath)
|
||||
func (w *Webdav) JoinStoragePath(p string) string {
|
||||
return path.Join(w.config.BasePath, p)
|
||||
}
|
||||
|
||||
func (w *Webdav) Save(ctx context.Context, r io.Reader, storagePath string) error {
|
||||
common.Log.Infof("Saving file to %s", storagePath)
|
||||
if err := w.client.MkDir(ctx, path.Dir(storagePath)); err != nil {
|
||||
common.Log.Errorf("Failed to create directory %s: %v", path.Dir(storagePath), err)
|
||||
w.logger.Infof("Saving file to %s", storagePath)
|
||||
|
||||
ext := path.Ext(storagePath)
|
||||
base := strings.TrimSuffix(storagePath, ext)
|
||||
candidate := storagePath
|
||||
for i := 1; w.Exists(ctx, candidate); i++ {
|
||||
candidate = fmt.Sprintf("%s_%d%s", base, i, ext)
|
||||
}
|
||||
|
||||
if err := w.client.MkDir(ctx, path.Dir(candidate)); err != nil {
|
||||
w.logger.Errorf("Failed to create directory %s: %v", path.Dir(candidate), err)
|
||||
return ErrFailedToCreateDirectory
|
||||
}
|
||||
if err := w.client.WriteFile(ctx, storagePath, r); err != nil {
|
||||
common.Log.Errorf("Failed to write file %s: %v", storagePath, err)
|
||||
if err := w.client.WriteFile(ctx, candidate, r); err != nil {
|
||||
w.logger.Errorf("Failed to write file %s: %v", candidate, err)
|
||||
return ErrFailedToWriteFile
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w *Webdav) Exists(ctx context.Context, storagePath string) bool {
|
||||
common.Log.Debugf("Checking if file exists at %s", storagePath)
|
||||
w.logger.Debugf("Checking if file exists at %s", storagePath)
|
||||
exists, err := w.client.Exists(ctx, storagePath)
|
||||
if err != nil {
|
||||
common.Log.Errorf("Failed to check if file exists at %s: %v", storagePath, err)
|
||||
w.logger.Errorf("Failed to check if file exists at %s: %v", storagePath, err)
|
||||
return false
|
||||
}
|
||||
return exists
|
||||
|
||||
Reference in New Issue
Block a user