aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/migrations/peertube-4.2.ts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/migrations/peertube-4.2.ts')
-rw-r--r--scripts/migrations/peertube-4.2.ts106
1 files changed, 106 insertions, 0 deletions
diff --git a/scripts/migrations/peertube-4.2.ts b/scripts/migrations/peertube-4.2.ts
new file mode 100644
index 000000000..045c3e511
--- /dev/null
+++ b/scripts/migrations/peertube-4.2.ts
@@ -0,0 +1,106 @@
1import { minBy } from 'lodash'
2import { join } from 'path'
3import { processImage } from '@server/helpers/image-utils'
4import { CONFIG } from '@server/initializers/config'
5import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
6import { updateActorImages } from '@server/lib/activitypub/actors'
7import { sendUpdateActor } from '@server/lib/activitypub/send'
8import { getBiggestActorImage } from '@server/lib/actor-image'
9import { JobQueue } from '@server/lib/job-queue'
10import { AccountModel } from '@server/models/account/account'
11import { ActorModel } from '@server/models/actor/actor'
12import { VideoChannelModel } from '@server/models/video/video-channel'
13import { MAccountDefault, MActorDefault, MChannelDefault } from '@server/types/models'
14import { getLowercaseExtension } from '@shared/core-utils'
15import { buildUUID } from '@shared/extra-utils'
16import { ActorImageType } from '@shared/models'
17import { initDatabaseModels } from '../../server/initializers/database'
18
19run()
20 .then(() => process.exit(0))
21 .catch(err => {
22 console.error(err)
23 process.exit(-1)
24 })
25
26async function run () {
27 console.log('Generate avatar miniatures from existing avatars.')
28
29 await initDatabaseModels(true)
30 JobQueue.Instance.init(true)
31
32 const accounts: AccountModel[] = await AccountModel.findAll({
33 include: [
34 {
35 model: ActorModel,
36 required: true,
37 where: {
38 serverId: null
39 }
40 },
41 {
42 model: VideoChannelModel,
43 include: [
44 {
45 model: AccountModel
46 }
47 ]
48 }
49 ]
50 })
51
52 for (const account of accounts) {
53 try {
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 generateSmallerAvatarIfNeeded(videoChannel)
62 } catch (err) {
63 console.error(`Cannot process channel avatar ${videoChannel.name}`, err)
64 }
65 }
66 }
67
68 console.log('Generation finished!')
69}
70
71async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) {
72 const avatars = accountOrChannel.Actor.Avatars
73 if (avatars.length !== 1) {
74 return
75 }
76
77 console.log(`Processing ${accountOrChannel.name}.`)
78
79 await generateSmallerAvatar(accountOrChannel.Actor)
80 accountOrChannel.Actor = Object.assign(accountOrChannel.Actor, { Server: null })
81
82 return sendUpdateActor(accountOrChannel, undefined)
83}
84
85async function generateSmallerAvatar (actor: MActorDefault) {
86 const bigAvatar = getBiggestActorImage(actor.Avatars)
87
88 const imageSize = minBy(ACTOR_IMAGES_SIZE[ActorImageType.AVATAR], 'width')
89 const sourceFilename = bigAvatar.filename
90
91 const newImageName = buildUUID() + getLowercaseExtension(sourceFilename)
92 const source = join(CONFIG.STORAGE.ACTOR_IMAGES, sourceFilename)
93 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, newImageName)
94
95 await processImage(source, destination, imageSize, true)
96
97 const actorImageInfo = {
98 name: newImageName,
99 fileUrl: null,
100 height: imageSize.height,
101 width: imageSize.width,
102 onDisk: true
103 }
104
105 await updateActorImages(actor, ActorImageType.AVATAR, [ actorImageInfo ], undefined)
106}