From 36aa51a1878880d47d86d4dd3875de4b35053567 Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Wed, 4 May 2022 09:29:20 +1000 Subject: [PATCH] Migrate vtt contents when hash changes (#2554) --- pkg/scene/migrate_hash.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/pkg/scene/migrate_hash.go b/pkg/scene/migrate_hash.go index 0a45f1456..e63654bf7 100644 --- a/pkg/scene/migrate_hash.go +++ b/pkg/scene/migrate_hash.go @@ -1,6 +1,8 @@ package scene import ( + "bytes" + "io/ioutil" "os" "path/filepath" @@ -35,13 +37,14 @@ func MigrateHash(p *paths.Paths, oldHash string, newHash string) { newPath = scenePaths.GetTranscodePath(newHash) migrateSceneFiles(oldPath, newPath) - oldPath = scenePaths.GetSpriteVttFilePath(oldHash) - newPath = scenePaths.GetSpriteVttFilePath(newHash) - migrateSceneFiles(oldPath, newPath) + oldVttPath := scenePaths.GetSpriteVttFilePath(oldHash) + newVttPath := scenePaths.GetSpriteVttFilePath(newHash) + migrateSceneFiles(oldVttPath, newVttPath) oldPath = scenePaths.GetSpriteImageFilePath(oldHash) newPath = scenePaths.GetSpriteImageFilePath(newHash) migrateSceneFiles(oldPath, newPath) + migrateVttFile(newVttPath, oldPath, newPath) oldPath = scenePaths.GetInteractiveHeatmapPath(oldHash) newPath = scenePaths.GetInteractiveHeatmapPath(newHash) @@ -62,3 +65,22 @@ func migrateSceneFiles(oldName, newName string) { } } } + +// #2481: migrate vtt file contents in addition to renaming +func migrateVttFile(vttPath, oldSpritePath, newSpritePath string) { + contents, err := ioutil.ReadFile(vttPath) + if err != nil { + logger.Errorf("Error reading %s for vtt migration: %v", vttPath, err) + return + } + + oldSpriteBasename := filepath.Base(oldSpritePath) + newSpriteBasename := filepath.Base(newSpritePath) + + contents = bytes.ReplaceAll(contents, []byte(oldSpriteBasename), []byte(newSpriteBasename)) + + if err := ioutil.WriteFile(vttPath, contents, 0644); err != nil { + logger.Errorf("Error writing %s for vtt migration: %v", vttPath, err) + return + } +}