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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 25 additions and 3 deletions

View File

@ -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
}
}