diff options
Diffstat (limited to 'scripts/migrations/peertube-4.2.ts')
-rw-r--r-- | scripts/migrations/peertube-4.2.ts | 106 |
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 @@ | |||
1 | import { minBy } from 'lodash' | ||
2 | import { join } from 'path' | ||
3 | import { processImage } from '@server/helpers/image-utils' | ||
4 | import { CONFIG } from '@server/initializers/config' | ||
5 | import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants' | ||
6 | import { updateActorImages } from '@server/lib/activitypub/actors' | ||
7 | import { sendUpdateActor } from '@server/lib/activitypub/send' | ||
8 | import { getBiggestActorImage } from '@server/lib/actor-image' | ||
9 | import { JobQueue } from '@server/lib/job-queue' | ||
10 | import { AccountModel } from '@server/models/account/account' | ||
11 | import { ActorModel } from '@server/models/actor/actor' | ||
12 | import { VideoChannelModel } from '@server/models/video/video-channel' | ||
13 | import { MAccountDefault, MActorDefault, MChannelDefault } from '@server/types/models' | ||
14 | import { getLowercaseExtension } from '@shared/core-utils' | ||
15 | import { buildUUID } from '@shared/extra-utils' | ||
16 | import { ActorImageType } from '@shared/models' | ||
17 | import { initDatabaseModels } from '../../server/initializers/database' | ||
18 | |||
19 | run() | ||
20 | .then(() => process.exit(0)) | ||
21 | .catch(err => { | ||
22 | console.error(err) | ||
23 | process.exit(-1) | ||
24 | }) | ||
25 | |||
26 | async 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 | |||
71 | async 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 | |||
85 | async 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 | } | ||