]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/migrations/peertube-4.2.ts
Translated using Weblate (Finnish)
[github/Chocobozzz/PeerTube.git] / scripts / migrations / peertube-4.2.ts
CommitLineData
d0800f76 1import { minBy } from 'lodash'
2import { join } from 'path'
7bde6250 3import { getImageSize, processImage } from '@server/helpers/image-utils'
d0800f76 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 {
7bde6250 54 await fillAvatarSizeIfNeeded(account)
d0800f76 55 await generateSmallerAvatarIfNeeded(account)
56 } catch (err) {
57 console.error(`Cannot process account avatar ${account.name}`, err)
58 }
59
60 for (const videoChannel of account.VideoChannels) {
61 try {
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
7bde6250
C
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, avatar.filename))
81 avatar.width = width
82 avatar.height = height
83
84 await avatar.save()
85 }
86}
87
d0800f76 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, sourceFilename)
110 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, newImageName)
111
112 await processImage(source, destination, imageSize, 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}