* 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
95 lines
2.0 KiB
Go
95 lines
2.0 KiB
Go
package batchtftask
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"path/filepath"
|
|
"sync/atomic"
|
|
|
|
"github.com/krau/SaveAny-Bot/common/tdler"
|
|
"github.com/krau/SaveAny-Bot/config"
|
|
"github.com/krau/SaveAny-Bot/pkg/tfile"
|
|
"github.com/krau/SaveAny-Bot/storage"
|
|
"github.com/rs/xid"
|
|
)
|
|
|
|
type TaskElement struct {
|
|
ID string
|
|
Storage storage.Storage
|
|
Path string
|
|
File tfile.TGFile
|
|
localPath string
|
|
stream bool
|
|
}
|
|
|
|
type Task struct {
|
|
ID string
|
|
Ctx context.Context
|
|
Elems []TaskElement
|
|
Progress ProgressTracker
|
|
IgnoreErrors bool // if true, errors during processing will be ignored
|
|
downloaded atomic.Int64
|
|
client tdler.Client
|
|
totalSize int64
|
|
processing map[string]TaskElementInfo
|
|
failed map[string]error // errors for each element
|
|
}
|
|
|
|
func NewTaskElement(
|
|
stor storage.Storage,
|
|
path string,
|
|
file tfile.TGFile,
|
|
) (*TaskElement, error) {
|
|
id := xid.New().String()
|
|
_, ok := stor.(storage.StorageCannotStream)
|
|
if !config.Cfg.Stream || ok {
|
|
cachePath, err := filepath.Abs(filepath.Join(config.Cfg.Temp.BasePath, fmt.Sprintf("%s_%s", id, file.Name())))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get absolute path for cache: %w", err)
|
|
}
|
|
return &TaskElement{
|
|
ID: id,
|
|
Storage: stor,
|
|
Path: path,
|
|
File: file,
|
|
localPath: cachePath,
|
|
}, nil
|
|
}
|
|
return &TaskElement{
|
|
ID: id,
|
|
Storage: stor,
|
|
Path: path,
|
|
File: file,
|
|
stream: true,
|
|
}, nil
|
|
}
|
|
|
|
func NewBatchTGFileTask(
|
|
id string,
|
|
ctx context.Context,
|
|
files []TaskElement,
|
|
client tdler.Client,
|
|
progress ProgressTracker,
|
|
ignoreErrors bool,
|
|
) *Task {
|
|
task := &Task{
|
|
ID: id,
|
|
Ctx: ctx,
|
|
client: client,
|
|
Elems: files,
|
|
Progress: progress,
|
|
downloaded: atomic.Int64{},
|
|
totalSize: func() int64 {
|
|
var total int64
|
|
for _, elem := range files {
|
|
total += elem.File.Size()
|
|
}
|
|
return total
|
|
}(),
|
|
processing: make(map[string]TaskElementInfo),
|
|
IgnoreErrors: ignoreErrors,
|
|
failed: make(map[string]error),
|
|
}
|
|
return task
|
|
}
|