aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/migrations/peertube-4.0.ts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/migrations/peertube-4.0.ts')
-rw-r--r--scripts/migrations/peertube-4.0.ts104
1 files changed, 0 insertions, 104 deletions
diff --git a/scripts/migrations/peertube-4.0.ts b/scripts/migrations/peertube-4.0.ts
deleted file mode 100644
index b0891c2e6..000000000
--- a/scripts/migrations/peertube-4.0.ts
+++ /dev/null
@@ -1,104 +0,0 @@
1import Bluebird from 'bluebird'
2import { move, readFile, writeFile } from 'fs-extra'
3import { join } from 'path'
4import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
5import { JobQueue } from '@server/lib/job-queue'
6import { generateHLSMasterPlaylistFilename, generateHlsSha256SegmentsFilename, getHlsResolutionPlaylistFilename } from '@server/lib/paths'
7import { VideoPathManager } from '@server/lib/video-path-manager'
8import { VideoModel } from '@server/models/video/video'
9import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist'
10import { initDatabaseModels } from '../../server/initializers/database'
11
12run()
13 .then(() => process.exit(0))
14 .catch(err => {
15 console.error(err)
16 process.exit(-1)
17 })
18
19async function run () {
20 console.log('Migrate old HLS paths to new format.')
21
22 await initDatabaseModels(true)
23
24 JobQueue.Instance.init()
25
26 const ids = await VideoModel.listLocalIds()
27
28 await Bluebird.map(ids, async id => {
29 try {
30 await processVideo(id)
31 } catch (err) {
32 console.error('Cannot process video %s.', { err })
33 }
34 }, { concurrency: 5 })
35
36 console.log('Migration finished!')
37}
38
39async function processVideo (videoId: number) {
40 const video = await VideoModel.loadWithFiles(videoId)
41
42 const hls = video.getHLSPlaylist()
43 if (video.isLive || !hls || hls.playlistFilename !== 'master.m3u8' || hls.VideoFiles.length === 0) {
44 return
45 }
46
47 console.log(`Renaming HLS playlist files of video ${video.name}.`)
48
49 const playlist = await VideoStreamingPlaylistModel.loadHLSPlaylistByVideo(video.id)
50 const hlsDirPath = VideoPathManager.Instance.getFSHLSOutputPath(video)
51
52 const masterPlaylistPath = join(hlsDirPath, playlist.playlistFilename)
53 let masterPlaylistContent = await readFile(masterPlaylistPath, 'utf8')
54
55 for (const videoFile of hls.VideoFiles) {
56 const srcName = `${videoFile.resolution}.m3u8`
57 const dstName = getHlsResolutionPlaylistFilename(videoFile.filename)
58
59 const src = join(hlsDirPath, srcName)
60 const dst = join(hlsDirPath, dstName)
61
62 try {
63 await move(src, dst)
64
65 masterPlaylistContent = masterPlaylistContent.replace(new RegExp('^' + srcName + '$', 'm'), dstName)
66 } catch (err) {
67 console.error('Cannot move video file %s to %s.', src, dst, err)
68 }
69 }
70
71 await writeFile(masterPlaylistPath, masterPlaylistContent)
72
73 if (playlist.segmentsSha256Filename === 'segments-sha256.json') {
74 try {
75 const newName = generateHlsSha256SegmentsFilename(video.isLive)
76
77 const dst = join(hlsDirPath, newName)
78 await move(join(hlsDirPath, playlist.segmentsSha256Filename), dst)
79 playlist.segmentsSha256Filename = newName
80 } catch (err) {
81 console.error(`Cannot rename ${video.name} segments-sha256.json file to a new name`, err)
82 }
83 }
84
85 if (playlist.playlistFilename === 'master.m3u8') {
86 try {
87 const newName = generateHLSMasterPlaylistFilename(video.isLive)
88
89 const dst = join(hlsDirPath, newName)
90 await move(join(hlsDirPath, playlist.playlistFilename), dst)
91 playlist.playlistFilename = newName
92 } catch (err) {
93 console.error(`Cannot rename ${video.name} master.m3u8 file to a new name`, err)
94 }
95 }
96
97 // Everything worked, we can save the playlist now
98 await playlist.save()
99
100 const allVideo = await VideoModel.loadFull(video.id)
101 await federateVideoIfNeeded(allVideo, false)
102
103 console.log(`Successfully moved HLS files of ${video.name}.`)
104}