mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-07-20 12:11:29 +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,6 +1,7 @@
|
||||
package alist
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -8,11 +9,13 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"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"
|
||||
"github.com/krau/SaveAny-Bot/pkg/enums/key"
|
||||
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
|
||||
)
|
||||
|
||||
type Alist struct {
|
||||
@@ -21,9 +24,10 @@ type Alist struct {
|
||||
baseURL string
|
||||
loginInfo *loginRequest
|
||||
config config.AlistStorageConfig
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
func (a *Alist) Init(cfg config.StorageConfig) error {
|
||||
func (a *Alist) Init(ctx context.Context, cfg config.StorageConfig) error {
|
||||
alistConfig, ok := cfg.(*config.AlistStorageConfig)
|
||||
if !ok {
|
||||
return fmt.Errorf("failed to cast alist config")
|
||||
@@ -32,45 +36,46 @@ func (a *Alist) Init(cfg config.StorageConfig) error {
|
||||
return err
|
||||
}
|
||||
a.config = *alistConfig
|
||||
|
||||
a.baseURL = alistConfig.URL
|
||||
a.client = getHttpClient()
|
||||
a.logger = log.FromContext(ctx).WithPrefix(fmt.Sprintf("alist[%s]", alistConfig.Name))
|
||||
|
||||
if alistConfig.Token != "" {
|
||||
a.token = alistConfig.Token
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, a.baseURL+"/api/me", nil)
|
||||
if err != nil {
|
||||
common.Log.Fatalf("Failed to create request: %v", err)
|
||||
a.logger.Fatalf("Failed to create request: %v", err)
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Authorization", a.token)
|
||||
|
||||
resp, err := a.client.Do(req)
|
||||
if err != nil {
|
||||
common.Log.Fatalf("Failed to send request: %v", err)
|
||||
a.logger.Fatalf("Failed to send request: %v", err)
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
common.Log.Fatalf("Failed to get alist user info: %s", resp.Status)
|
||||
a.logger.Fatalf("Failed to get alist user info: %s", resp.Status)
|
||||
return err
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
common.Log.Fatalf("Failed to read response body: %v", err)
|
||||
a.logger.Fatalf("Failed to read response body: %v", err)
|
||||
return err
|
||||
}
|
||||
var meResp meResponse
|
||||
if err := json.Unmarshal(body, &meResp); err != nil {
|
||||
common.Log.Fatalf("Failed to unmarshal me response: %v", err)
|
||||
a.logger.Fatalf("Failed to unmarshal me response: %v", err)
|
||||
return err
|
||||
}
|
||||
if meResp.Code != http.StatusOK {
|
||||
common.Log.Fatalf("Failed to get alist user info: %s", meResp.Message)
|
||||
a.logger.Fatalf("Failed to get alist user info: %s", meResp.Message)
|
||||
return err
|
||||
}
|
||||
common.Log.Debugf("Logged in Alist as %s", meResp.Data.Username)
|
||||
a.logger.Debugf("Logged in Alist as %s", meResp.Data.Username)
|
||||
return nil
|
||||
}
|
||||
a.loginInfo = &loginRequest{
|
||||
@@ -78,18 +83,18 @@ func (a *Alist) Init(cfg config.StorageConfig) error {
|
||||
Password: alistConfig.Password,
|
||||
}
|
||||
|
||||
if err := a.getToken(); err != nil {
|
||||
common.Log.Fatalf("Failed to login to Alist: %v", err)
|
||||
if err := a.getToken(ctx); err != nil {
|
||||
a.logger.Fatalf("Failed to login to Alist: %v", err)
|
||||
return err
|
||||
}
|
||||
common.Log.Debug("Logged in to Alist")
|
||||
a.logger.Debug("Logged in to Alist")
|
||||
|
||||
go a.refreshToken(*alistConfig)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Alist) Type() types.StorageType {
|
||||
return types.StorageTypeAlist
|
||||
func (a *Alist) Type() storenum.StorageType {
|
||||
return storenum.Alist
|
||||
}
|
||||
|
||||
func (a *Alist) Name() string {
|
||||
@@ -97,16 +102,23 @@ func (a *Alist) Name() string {
|
||||
}
|
||||
|
||||
func (a *Alist) Save(ctx context.Context, reader io.Reader, storagePath string) error {
|
||||
common.Log.Infof("Saving file to %s", storagePath)
|
||||
a.logger.Infof("Saving file to %s", storagePath)
|
||||
|
||||
ext := path.Ext(storagePath)
|
||||
base := strings.TrimSuffix(storagePath, ext)
|
||||
candidate := storagePath
|
||||
for i := 1; a.Exists(ctx, candidate); i++ {
|
||||
candidate = fmt.Sprintf("%s_%d%s", base, i, ext)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPut, a.baseURL+"/api/fs/put", reader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create request: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", a.token)
|
||||
req.Header.Set("File-Path", url.PathEscape(storagePath))
|
||||
req.Header.Set("File-Path", url.PathEscape(candidate))
|
||||
req.Header.Set("Content-Type", "application/octet-stream")
|
||||
if length := ctx.Value(types.ContextKeyContentLength); length != nil {
|
||||
if length := ctx.Value(key.ContextKeyContentLength); length != nil {
|
||||
length, ok := length.(int64)
|
||||
if ok {
|
||||
req.ContentLength = length
|
||||
@@ -140,15 +152,66 @@ func (a *Alist) Save(ctx context.Context, reader io.Reader, storagePath string)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Alist) NotSupportStream() string {
|
||||
return "Alist does not support chunked transfer encoding"
|
||||
}
|
||||
|
||||
func (a *Alist) JoinStoragePath(task types.Task) string {
|
||||
return path.Join(a.config.BasePath, task.StoragePath)
|
||||
func (a *Alist) JoinStoragePath(p string) string {
|
||||
return path.Join(a.config.BasePath, p)
|
||||
}
|
||||
|
||||
func (a *Alist) Exists(ctx context.Context, storagePath string) bool {
|
||||
// TODO: Implement it.
|
||||
return false
|
||||
// POST /api/fs/get
|
||||
/*
|
||||
body:
|
||||
{
|
||||
"path": "/t",
|
||||
"password": "",
|
||||
"page": 1,
|
||||
"per_page": 0,
|
||||
"refresh": false
|
||||
}
|
||||
*/
|
||||
body := map[string]any{
|
||||
"path": storagePath,
|
||||
"password": "",
|
||||
}
|
||||
bodyBytes, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
a.logger.Errorf("Failed to marshal request body: %v", err)
|
||||
return false
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, a.baseURL+"/api/fs/get", bytes.NewBuffer(bodyBytes))
|
||||
if err != nil {
|
||||
a.logger.Errorf("Failed to create request: %v", err)
|
||||
return false
|
||||
}
|
||||
req.Header.Set("Authorization", a.token)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := a.client.Do(req)
|
||||
if err != nil {
|
||||
a.logger.Errorf("Failed to send request: %v", err)
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return false
|
||||
}
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
a.logger.Errorf("Failed to read response body: %v", err)
|
||||
return false
|
||||
}
|
||||
var fsGetResp fsGetResponse
|
||||
if err := json.Unmarshal(data, &fsGetResp); err != nil {
|
||||
a.logger.Errorf("Failed to unmarshal fs get response: %v", err)
|
||||
return false
|
||||
}
|
||||
if fsGetResp.Code != http.StatusOK {
|
||||
a.logger.Errorf("Failed to get file info from Alist: %d, %s", fsGetResp.Code, fsGetResp.Message)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// Impl StorageCannotStream interface
|
||||
func (a *Alist) CannotStream() string {
|
||||
return "Alist does not support chunked transfer encoding"
|
||||
}
|
||||
|
||||
@@ -2,17 +2,17 @@ package alist
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/krau/SaveAny-Bot/common"
|
||||
config "github.com/krau/SaveAny-Bot/config/storage"
|
||||
)
|
||||
|
||||
func (a *Alist) getToken() error {
|
||||
func (a *Alist) getToken(ctx context.Context) error {
|
||||
loginBody, err := json.Marshal(a.loginInfo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal login request: %w", err)
|
||||
@@ -51,15 +51,15 @@ func (a *Alist) getToken() error {
|
||||
func (a *Alist) refreshToken(cfg config.AlistStorageConfig) {
|
||||
tokenExp := cfg.TokenExp
|
||||
if tokenExp <= 0 {
|
||||
common.Log.Warn("Invalid token expiration time, using default value")
|
||||
a.logger.Warn("Invalid token expiration time, using default value")
|
||||
tokenExp = 3600
|
||||
}
|
||||
for {
|
||||
time.Sleep(time.Duration(tokenExp) * time.Second)
|
||||
if err := a.getToken(); err != nil {
|
||||
common.Log.Errorf("Failed to refresh jwt token: %v", err)
|
||||
if err := a.getToken(context.Background()); err != nil {
|
||||
a.logger.Errorf("Failed to refresh jwt token: %v", err)
|
||||
continue
|
||||
}
|
||||
common.Log.Info("Refreshed Alist jwt token")
|
||||
a.logger.Info("Refreshed Alist jwt token")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,3 +42,8 @@ type putResponse struct {
|
||||
} `json:"task"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type fsGetResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user