aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/migrations/peertube-4.0.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2023-07-31 14:34:36 +0200
committerChocobozzz <me@florianbigard.com>2023-08-11 15:02:33 +0200
commit3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch)
treee4510b39bdac9c318fdb4b47018d08f15368b8f0 /scripts/migrations/peertube-4.0.ts
parent04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff)
downloadPeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst
PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge conflicts, but it's a major step forward: * Server can be faster at startup because imports() are async and we can easily lazy import big modules * Angular doesn't seem to support ES import (with .js extension), so we had to correctly organize peertube into a monorepo: * Use yarn workspace feature * Use typescript reference projects for dependencies * Shared projects have been moved into "packages", each one is now a node module (with a dedicated package.json/tsconfig.json) * server/tools have been moved into apps/ and is now a dedicated app bundled and published on NPM so users don't have to build peertube cli tools manually * server/tests have been moved into packages/ so we don't compile them every time we want to run the server * Use isolatedModule option: * Had to move from const enum to const (https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums) * Had to explictely specify "type" imports when used in decorators * Prefer tsx (that uses esbuild under the hood) instead of ts-node to load typescript files (tests with mocha or scripts): * To reduce test complexity as esbuild doesn't support decorator metadata, we only test server files that do not import server models * We still build tests files into js files for a faster CI * Remove unmaintained peertube CLI import script * Removed some barrels to speed up execution (less imports)
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}