aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/scripts/migrations
diff options
context:
space:
mode:
Diffstat (limited to 'server/scripts/migrations')
-rw-r--r--server/scripts/migrations/peertube-4.0.ts110
-rw-r--r--server/scripts/migrations/peertube-4.2.ts123
-rw-r--r--server/scripts/migrations/peertube-5.0.ts71
3 files changed, 304 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 @@
1import Bluebird from 'bluebird'
2import { move } from 'fs-extra/esm'
3import { readFile, writeFile } from 'fs/promises'
4import { join } from 'path'
5import { initDatabaseModels } from '@server/initializers/database.js'
6import { federateVideoIfNeeded } from '@server/lib/activitypub/videos/index.js'
7import { JobQueue } from '@server/lib/job-queue/index.js'
8import {
9 generateHLSMasterPlaylistFilename,
10 generateHlsSha256SegmentsFilename,
11 getHlsResolutionPlaylistFilename
12} from '@server/lib/paths.js'
13import { VideoPathManager } from '@server/lib/video-path-manager.js'
14import { VideoStreamingPlaylistModel } from '@server/models/video/video-streaming-playlist.js'
15import { VideoModel } from '@server/models/video/video.js'
16
17run()
18 .then(() => process.exit(0))
19 .catch(err => {
20 console.error(err)
21 process.exit(-1)
22
23 })
24
25async 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
45async 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}
diff --git a/server/scripts/migrations/peertube-4.2.ts b/server/scripts/migrations/peertube-4.2.ts
new file mode 100644
index 000000000..6c89ee39e
--- /dev/null
+++ b/server/scripts/migrations/peertube-4.2.ts
@@ -0,0 +1,123 @@
1import { ActorImageType } from '@peertube/peertube-models'
2import { buildUUID, getLowercaseExtension } from '@peertube/peertube-node-utils'
3import { getImageSize, processImage } from '@server/helpers/image-utils.js'
4import { CONFIG } from '@server/initializers/config.js'
5import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants.js'
6import { initDatabaseModels } from '@server/initializers/database.js'
7import { updateActorImages } from '@server/lib/activitypub/actors/index.js'
8import { sendUpdateActor } from '@server/lib/activitypub/send/index.js'
9import { getBiggestActorImage } from '@server/lib/actor-image.js'
10import { JobQueue } from '@server/lib/job-queue/index.js'
11import { AccountModel } from '@server/models/account/account.js'
12import { ActorModel } from '@server/models/actor/actor.js'
13import { VideoChannelModel } from '@server/models/video/video-channel.js'
14import { MAccountDefault, MActorDefault, MChannelDefault } from '@server/types/models/index.js'
15import minBy from 'lodash-es/minBy.js'
16import { join } from 'path'
17
18run()
19 .then(() => process.exit(0))
20 .catch(err => {
21 console.error(err)
22 process.exit(-1)
23 })
24
25async function run () {
26 console.log('Generate avatar miniatures from existing avatars.')
27
28 await initDatabaseModels(true)
29 JobQueue.Instance.init()
30
31 const accounts: AccountModel[] = await AccountModel.findAll({
32 include: [
33 {
34 model: ActorModel,
35 required: true,
36 where: {
37 serverId: null
38 }
39 },
40 {
41 model: VideoChannelModel,
42 include: [
43 {
44 model: AccountModel
45 }
46 ]
47 }
48 ]
49 })
50
51 for (const account of accounts) {
52 try {
53 await fillAvatarSizeIfNeeded(account)
54 await generateSmallerAvatarIfNeeded(account)
55 } catch (err) {
56 console.error(`Cannot process account avatar ${account.name}`, err)
57 }
58
59 for (const videoChannel of account.VideoChannels) {
60 try {
61 await fillAvatarSizeIfNeeded(videoChannel)
62 await generateSmallerAvatarIfNeeded(videoChannel)
63 } catch (err) {
64 console.error(`Cannot process channel avatar ${videoChannel.name}`, err)
65 }
66 }
67 }
68
69 console.log('Generation finished!')
70}
71
72async function fillAvatarSizeIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) {
73 const avatars = accountOrChannel.Actor.Avatars
74
75 for (const avatar of avatars) {
76 if (avatar.width && avatar.height) continue
77
78 console.log('Filling size of avatars of %s.', accountOrChannel.name)
79
80 const { width, height } = await getImageSize(join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, avatar.filename))
81 avatar.width = width
82 avatar.height = height
83
84 await avatar.save()
85 }
86}
87
88async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) {
89 const avatars = accountOrChannel.Actor.Avatars
90 if (avatars.length !== 1) {
91 return
92 }
93
94 console.log(`Processing ${accountOrChannel.name}.`)
95
96 await generateSmallerAvatar(accountOrChannel.Actor)
97 accountOrChannel.Actor = Object.assign(accountOrChannel.Actor, { Server: null })
98
99 return sendUpdateActor(accountOrChannel, undefined)
100}
101
102async function generateSmallerAvatar (actor: MActorDefault) {
103 const bigAvatar = getBiggestActorImage(actor.Avatars)
104
105 const imageSize = minBy(ACTOR_IMAGES_SIZE[ActorImageType.AVATAR], 'width')
106 const sourceFilename = bigAvatar.filename
107
108 const newImageName = buildUUID() + getLowercaseExtension(sourceFilename)
109 const source = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, sourceFilename)
110 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, newImageName)
111
112 await processImage({ path: source, destination, newSize: imageSize, keepOriginal: true })
113
114 const actorImageInfo = {
115 name: newImageName,
116 fileUrl: null,
117 height: imageSize.height,
118 width: imageSize.width,
119 onDisk: true
120 }
121
122 await updateActorImages(actor, ActorImageType.AVATAR, [ actorImageInfo ], undefined)
123}
diff --git a/server/scripts/migrations/peertube-5.0.ts b/server/scripts/migrations/peertube-5.0.ts
new file mode 100644
index 000000000..6139abd08
--- /dev/null
+++ b/server/scripts/migrations/peertube-5.0.ts
@@ -0,0 +1,71 @@
1import { ensureDir } from 'fs-extra/esm'
2import { Op } from 'sequelize'
3import { updateTorrentMetadata } from '@server/helpers/webtorrent.js'
4import { DIRECTORIES } from '@server/initializers/constants.js'
5import { moveFilesIfPrivacyChanged } from '@server/lib/video-privacy.js'
6import { VideoModel } from '@server/models/video/video.js'
7import { MVideoFullLight } from '@server/types/models/index.js'
8import { VideoPrivacy } from '@peertube/peertube-models'
9import { initDatabaseModels } from '@server/initializers/database.js'
10
11run()
12 .then(() => process.exit(0))
13 .catch(err => {
14 console.error(err)
15 process.exit(-1)
16 })
17
18async function run () {
19 console.log('Moving private video files in dedicated folders.')
20
21 await ensureDir(DIRECTORIES.HLS_STREAMING_PLAYLIST.PRIVATE)
22 await ensureDir(DIRECTORIES.VIDEOS.PRIVATE)
23
24 await initDatabaseModels(true)
25
26 const videos = await VideoModel.unscoped().findAll({
27 attributes: [ 'uuid' ],
28 where: {
29 privacy: {
30 [Op.in]: [ VideoPrivacy.PRIVATE, VideoPrivacy.INTERNAL ]
31 }
32 }
33 })
34
35 for (const { uuid } of videos) {
36 try {
37 console.log('Moving files of video %s.', uuid)
38
39 const video = await VideoModel.loadFull(uuid)
40
41 try {
42 await moveFilesIfPrivacyChanged(video, VideoPrivacy.PUBLIC)
43 } catch (err) {
44 console.error('Cannot move files of video %s.', uuid, err)
45 }
46
47 try {
48 await updateTorrents(video)
49 } catch (err) {
50 console.error('Cannot regenerate torrents of video %s.', uuid, err)
51 }
52 } catch (err) {
53 console.error('Cannot process video %s.', uuid, err)
54 }
55 }
56}
57
58async function updateTorrents (video: MVideoFullLight) {
59 for (const file of video.VideoFiles) {
60 await updateTorrentMetadata(video, file)
61
62 await file.save()
63 }
64
65 const playlist = video.getHLSPlaylist()
66 for (const file of (playlist?.VideoFiles || [])) {
67 await updateTorrentMetadata(playlist, file)
68
69 await file.save()
70 }
71}