Migrate vtt contents when hash changes (#2554)

This commit is contained in:
WithoutPants
2022-05-04 09:29:20 +10:00
committed by GitHub
parent 1a6f5619ae
commit 36aa51a187

View File

@@ -1,6 +1,8 @@
package scene package scene
import ( import (
"bytes"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@@ -35,13 +37,14 @@ func MigrateHash(p *paths.Paths, oldHash string, newHash string) {
newPath = scenePaths.GetTranscodePath(newHash) newPath = scenePaths.GetTranscodePath(newHash)
migrateSceneFiles(oldPath, newPath) migrateSceneFiles(oldPath, newPath)
oldPath = scenePaths.GetSpriteVttFilePath(oldHash) oldVttPath := scenePaths.GetSpriteVttFilePath(oldHash)
newPath = scenePaths.GetSpriteVttFilePath(newHash) newVttPath := scenePaths.GetSpriteVttFilePath(newHash)
migrateSceneFiles(oldPath, newPath) migrateSceneFiles(oldVttPath, newVttPath)
oldPath = scenePaths.GetSpriteImageFilePath(oldHash) oldPath = scenePaths.GetSpriteImageFilePath(oldHash)
newPath = scenePaths.GetSpriteImageFilePath(newHash) newPath = scenePaths.GetSpriteImageFilePath(newHash)
migrateSceneFiles(oldPath, newPath) migrateSceneFiles(oldPath, newPath)
migrateVttFile(newVttPath, oldPath, newPath)
oldPath = scenePaths.GetInteractiveHeatmapPath(oldHash) oldPath = scenePaths.GetInteractiveHeatmapPath(oldHash)
newPath = scenePaths.GetInteractiveHeatmapPath(newHash) 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
}
}