mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-05-10 17:52:44 +08:00
* 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
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package local
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/charmbracelet/log"
|
|
"github.com/duke-git/lancet/v2/fileutil"
|
|
config "github.com/krau/SaveAny-Bot/config/storage"
|
|
storenum "github.com/krau/SaveAny-Bot/pkg/enums/storage"
|
|
)
|
|
|
|
type Local struct {
|
|
config config.LocalStorageConfig
|
|
logger *log.Logger
|
|
}
|
|
|
|
func (l *Local) Init(ctx context.Context, cfg config.StorageConfig) error {
|
|
localConfig, ok := cfg.(*config.LocalStorageConfig)
|
|
if !ok {
|
|
return fmt.Errorf("failed to cast local config")
|
|
}
|
|
if err := localConfig.Validate(); err != nil {
|
|
return err
|
|
}
|
|
l.config = *localConfig
|
|
err := os.MkdirAll(localConfig.BasePath, os.ModePerm)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create local storage directory: %w", err)
|
|
}
|
|
l.logger = log.FromContext(ctx).WithPrefix(fmt.Sprintf("local[%s]", l.config.Name))
|
|
return nil
|
|
}
|
|
|
|
func (l *Local) Type() storenum.StorageType {
|
|
return storenum.Local
|
|
}
|
|
|
|
func (l *Local) Name() string {
|
|
return l.config.Name
|
|
}
|
|
|
|
func (l *Local) JoinStoragePath(path string) string {
|
|
return filepath.Join(l.config.BasePath, path)
|
|
}
|
|
|
|
func (l *Local) Save(ctx context.Context, r io.Reader, storagePath string) error {
|
|
l.logger.Infof("Saving file to %s", storagePath)
|
|
|
|
ext := filepath.Ext(storagePath)
|
|
base := strings.TrimSuffix(storagePath, ext)
|
|
candidate := storagePath
|
|
for i := 1; l.Exists(ctx, candidate); i++ {
|
|
candidate = fmt.Sprintf("%s_%d%s", base, i, ext)
|
|
}
|
|
|
|
absPath, err := filepath.Abs(candidate)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := fileutil.CreateDir(filepath.Dir(absPath)); err != nil {
|
|
return err
|
|
}
|
|
file, err := os.Create(absPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
_, err = io.Copy(file, r)
|
|
return err
|
|
}
|
|
|
|
func (l *Local) Exists(ctx context.Context, storagePath string) bool {
|
|
absPath, err := filepath.Abs(storagePath)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return fileutil.IsExist(absPath)
|
|
}
|