Add storage rule support to API task creation

Co-authored-by: krau <71133316+krau@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-19 07:17:35 +00:00
parent 6896bdc852
commit e1b4087801

View File

@@ -18,6 +18,7 @@ import (
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gotd/td/tg" "github.com/gotd/td/tg"
"github.com/krau/SaveAny-Bot/client/bot" "github.com/krau/SaveAny-Bot/client/bot"
"github.com/krau/SaveAny-Bot/client/bot/handlers/utils/ruleutil"
"github.com/krau/SaveAny-Bot/common/utils/tgutil" "github.com/krau/SaveAny-Bot/common/utils/tgutil"
"github.com/krau/SaveAny-Bot/config" "github.com/krau/SaveAny-Bot/config"
"github.com/krau/SaveAny-Bot/core" "github.com/krau/SaveAny-Bot/core"
@@ -226,19 +227,46 @@ func handleCreateTask(w http.ResponseWriter, r *http.Request) {
// Create tasks for all files // Create tasks for all files
taskIDs := make([]string, 0, len(files)) taskIDs := make([]string, 0, len(files))
dirPath := req.DirPath baseDirPath := req.DirPath
if dirPath == "" { if baseDirPath == "" {
dirPath = "/" baseDirPath = "/"
} }
// Create context with bot extension // Create context with bot extension
injectCtx := tgutil.ExtWithContext(r.Context(), botCtx) injectCtx := tgutil.ExtWithContext(r.Context(), botCtx)
// Apply storage rules if enabled for the user
useRule := userDB.ApplyRule && userDB.Rules != nil
for _, tgFile := range files { for _, tgFile := range files {
storagePath := stor.JoinStoragePath(path.Join(dirPath, tgFile.Name())) // Determine storage and directory path for this specific file
fileStor := stor
dirPath := baseDirPath
// Apply rules if enabled
if useRule {
matched, matchedStorName, matchedDirPath := ruleutil.ApplyRule(injectCtx, userDB.Rules, ruleutil.NewInput(tgFile))
if matched {
// Rule matched, apply overrides
if matchedDirPath != "" && matchedDirPath != "{{album}}" {
dirPath = matchedDirPath.String()
}
if matchedStorName.Usable() {
var err error
fileStor, err = storage.GetStorageByUserIDAndName(injectCtx, userDB.ChatID, matchedStorName.String())
if err != nil {
logger.Errorf("Failed to get storage from rule: %v", err)
// Fall back to original storage
fileStor = stor
}
}
}
}
storagePath := fileStor.JoinStoragePath(path.Join(dirPath, tgFile.Name()))
taskID := xid.New().String() taskID := xid.New().String()
task, err := tftask.NewTGFileTask(taskID, injectCtx, tgFile, stor, storagePath, &apiProgressTracker{ task, err := tftask.NewTGFileTask(taskID, injectCtx, tgFile, fileStor, storagePath, &apiProgressTracker{
taskID: taskID, taskID: taskID,
}) })
if err != nil { if err != nil {