Wrap CleanCaptions with database. Refactor AssociateCaptions.

This commit is contained in:
WithoutPants 2026-03-02 15:45:37 +11:00
parent bc75d47f15
commit 09e2b2bd4e
2 changed files with 21 additions and 15 deletions

View file

@ -363,7 +363,9 @@ func (j *ScanJob) handleFile(ctx context.Context, f file.ScannedFile, progress *
if videoFile != nil { if videoFile != nil {
txnMgr := j.scanner.Repository.TxnManager txnMgr := j.scanner.Repository.TxnManager
fileRepo := j.scanner.Repository.File 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) logger.Errorf("Error cleaning captions: %v", err)
} }
} }

View file

@ -129,21 +129,25 @@ func AssociateCaptions(ctx context.Context, captionPath string, txnMgr txn.Manag
matched = true matched = true
captions, er := w.GetCaptions(ctx, fileID) captions, er := w.GetCaptions(ctx, fileID)
if er == nil { if er != nil {
fileExt := filepath.Ext(captionPath) return fmt.Errorf("getting captions for file %s: %w", path, er)
ext := fileExt[1:] }
if !IsLangInCaptions(captionLang, ext, captions) { // only update captions if language code is not present
newCaption := &models.VideoCaption{ fileExt := filepath.Ext(captionPath)
LanguageCode: captionLang, ext := fileExt[1:]
Filename: filepath.Base(captionPath), if !IsLangInCaptions(captionLang, ext, captions) { // only update captions if language code is not present
CaptionType: ext, newCaption := &models.VideoCaption{
} LanguageCode: captionLang,
captions = append(captions, newCaption) Filename: filepath.Base(captionPath),
er = w.UpdateCaptions(ctx, fileID, captions) CaptionType: ext,
if er == nil {
logger.Debugf("Updated captions for file %s. Added %s", path, captionLang)
}
} }
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 return err