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