mirror of
https://github.com/krau/SaveAny-Bot.git
synced 2026-05-11 20:09:53 +08:00
fix(watch): enhance group media handling
This commit is contained in:
@@ -252,6 +252,16 @@ func listenMediaMessageEvent(ch chan userclient.MediaMessageEvent) {
|
|||||||
logger.Errorf("Failed to get storage by user ID %d and name %s: %v", user.ChatID, user.DefaultStorage, err)
|
logger.Errorf("Failed to get storage by user ID %d and name %s: %v", user.ChatID, user.DefaultStorage, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
// Resolve the default directory path from user.DefaultDir
|
||||||
|
var defaultDirPath string
|
||||||
|
if user.DefaultDir != 0 {
|
||||||
|
dir, err := database.GetDirByID(ctx, user.DefaultDir)
|
||||||
|
if err != nil {
|
||||||
|
logger.Warnf("Failed to get default dir for user %d: %v, using root", user.ChatID, err)
|
||||||
|
} else {
|
||||||
|
defaultDirPath = dir.Path
|
||||||
|
}
|
||||||
|
}
|
||||||
switch user.FilenameStrategy {
|
switch user.FilenameStrategy {
|
||||||
case fnamest.Message.String():
|
case fnamest.Message.String():
|
||||||
file.SetName(tgutil.GenFileNameFromMessage(*file.Message()))
|
file.SetName(tgutil.GenFileNameFromMessage(*file.Message()))
|
||||||
@@ -286,14 +296,14 @@ func listenMediaMessageEvent(ch chan userclient.MediaMessageEvent) {
|
|||||||
|
|
||||||
if needAlbumHandling {
|
if needAlbumHandling {
|
||||||
// For media groups with NEW-FOR-ALBUM rule, collect all files of the same group
|
// For media groups with NEW-FOR-ALBUM rule, collect all files of the same group
|
||||||
watchMediaGroupMgr.addFile(event.ChatID, user.ID, file, time.Duration(config.C().Telegram.MediaGroupTimeout)*time.Second, func(files []tfile.TGFileMessage) {
|
watchMediaGroupMgr.addFile(event.ChatID, user.ID, file, time.Duration(max(config.C().Telegram.MediaGroupTimeout, 1))*time.Second, func(files []tfile.TGFileMessage) {
|
||||||
processWatchMediaGroup(ctx, user, stor, "", files)
|
processWatchMediaGroup(ctx, user, stor, defaultDirPath, files)
|
||||||
})
|
})
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process single file or media group without album folder creation
|
// Process single file or media group without album folder creation
|
||||||
var dirPath string
|
dirPath := defaultDirPath
|
||||||
if user.ApplyRule && user.Rules != nil {
|
if user.ApplyRule && user.Rules != nil {
|
||||||
matched, matchedStorageName, matchedDirPath := ruleutil.ApplyRule(ctx, user.Rules, ruleutil.NewInput(file))
|
matched, matchedStorageName, matchedDirPath := ruleutil.ApplyRule(ctx, user.Rules, ruleutil.NewInput(file))
|
||||||
if !matched {
|
if !matched {
|
||||||
@@ -352,6 +362,7 @@ func processWatchMediaGroup(ctx *ext.Context, user *database.User, stor storage.
|
|||||||
type albumFile struct {
|
type albumFile struct {
|
||||||
file tfile.TGFileMessage
|
file tfile.TGFileMessage
|
||||||
storage storage.Storage
|
storage storage.Storage
|
||||||
|
dirPath string
|
||||||
}
|
}
|
||||||
albumFiles := make(map[int64][]albumFile)
|
albumFiles := make(map[int64][]albumFile)
|
||||||
|
|
||||||
@@ -374,9 +385,11 @@ func processWatchMediaGroup(ctx *ext.Context, user *database.User, stor storage.
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if !ruleDirPath.NeedNewForAlbum() {
|
// Use the effective dirPath: if rule returns NEW-FOR-ALBUM sentinel, fall back to the
|
||||||
logger.Warnf("File %s does not need album folder, skipping", file.Name())
|
// base dirPath passed in (which is defaultDirPath from the caller).
|
||||||
continue
|
effectiveDirPath := string(ruleDirPath)
|
||||||
|
if ruleDirPath.NeedNewForAlbum() {
|
||||||
|
effectiveDirPath = dirPath
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := albumFiles[groupId]; !ok {
|
if _, ok := albumFiles[groupId]; !ok {
|
||||||
@@ -385,6 +398,7 @@ func processWatchMediaGroup(ctx *ext.Context, user *database.User, stor storage.
|
|||||||
albumFiles[groupId] = append(albumFiles[groupId], albumFile{
|
albumFiles[groupId] = append(albumFiles[groupId], albumFile{
|
||||||
file: file,
|
file: file,
|
||||||
storage: fileStor,
|
storage: fileStor,
|
||||||
|
dirPath: effectiveDirPath,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,7 +417,7 @@ func processWatchMediaGroup(ctx *ext.Context, user *database.User, stor storage.
|
|||||||
logger.Infof("Creating album folder for group %d: %s with %d files", groupID, albumDir, len(afiles))
|
logger.Infof("Creating album folder for group %d: %s with %d files", groupID, albumDir, len(afiles))
|
||||||
|
|
||||||
for _, af := range afiles {
|
for _, af := range afiles {
|
||||||
afstorPath := path.Join(dirPath, albumDir, af.file.Name())
|
afstorPath := path.Join(af.dirPath, albumDir, af.file.Name())
|
||||||
taskid := xid.New().String()
|
taskid := xid.New().String()
|
||||||
task, err := coretfile.NewTGFileTask(taskid, injectCtx, af.file, albumStor, afstorPath, nil)
|
task, err := coretfile.NewTGFileTask(taskid, injectCtx, af.file, albumStor, afstorPath, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user