From 09e2b2bd4e516d52e65d66d8ce2e04100da93b59 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Mon, 2 Mar 2026 15:45:37 +1100 Subject: [PATCH] Wrap CleanCaptions with database. Refactor AssociateCaptions. --- internal/manager/task_scan.go | 4 +++- pkg/file/video/caption.go | 32 ++++++++++++++++++-------------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/internal/manager/task_scan.go b/internal/manager/task_scan.go index 77a492134..5d1f063c2 100644 --- a/internal/manager/task_scan.go +++ b/internal/manager/task_scan.go @@ -363,7 +363,9 @@ func (j *ScanJob) handleFile(ctx context.Context, f file.ScannedFile, progress * if videoFile != nil { txnMgr := j.scanner.Repository.TxnManager fileRepo := j.scanner.Repository.File - if err := video.CleanCaptions(ctx, videoFile, txnMgr, fileRepo); err != nil { + if err := txn.WithDatabase(ctx, txnMgr, func(ctx context.Context) error { + return video.CleanCaptions(ctx, videoFile, txnMgr, fileRepo) + }); err != nil { logger.Errorf("Error cleaning captions: %v", err) } } diff --git a/pkg/file/video/caption.go b/pkg/file/video/caption.go index a9e216acd..46317d90c 100644 --- a/pkg/file/video/caption.go +++ b/pkg/file/video/caption.go @@ -129,21 +129,25 @@ func AssociateCaptions(ctx context.Context, captionPath string, txnMgr txn.Manag matched = true captions, er := w.GetCaptions(ctx, fileID) - if er == nil { - fileExt := filepath.Ext(captionPath) - ext := fileExt[1:] - if !IsLangInCaptions(captionLang, ext, captions) { // only update captions if language code is not present - newCaption := &models.VideoCaption{ - LanguageCode: captionLang, - Filename: filepath.Base(captionPath), - CaptionType: ext, - } - captions = append(captions, newCaption) - er = w.UpdateCaptions(ctx, fileID, captions) - if er == nil { - logger.Debugf("Updated captions for file %s. Added %s", path, captionLang) - } + if er != nil { + return fmt.Errorf("getting captions for file %s: %w", path, er) + } + + fileExt := filepath.Ext(captionPath) + ext := fileExt[1:] + if !IsLangInCaptions(captionLang, ext, captions) { // only update captions if language code is not present + newCaption := &models.VideoCaption{ + LanguageCode: captionLang, + Filename: filepath.Base(captionPath), + CaptionType: ext, } + captions = append(captions, newCaption) + er = w.UpdateCaptions(ctx, fileID, captions) + if er != nil { + return fmt.Errorf("updating captions for file %s: %w", path, er) + } + + logger.Debugf("Updated captions for file %s. Added %s", path, captionLang) } } return err