1 import { minBy } from 'lodash'
2 import { join } from 'path'
3 import { getImageSize, 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'
20 .then(() => process.exit(0))
26 async function run () {
27 console.log('Generate avatar miniatures from existing avatars.')
29 await initDatabaseModels(true)
30 JobQueue.Instance.init(true)
32 const accounts: AccountModel[] = await AccountModel.findAll({
42 model: VideoChannelModel,
52 for (const account of accounts) {
54 await fillAvatarSizeIfNeeded(account)
55 await generateSmallerAvatarIfNeeded(account)
57 console.error(`Cannot process account avatar ${account.name}`, err)
60 for (const videoChannel of account.VideoChannels) {
62 await fillAvatarSizeIfNeeded(videoChannel)
63 await generateSmallerAvatarIfNeeded(videoChannel)
65 console.error(`Cannot process channel avatar ${videoChannel.name}`, err)
70 console.log('Generation finished!')
73 async function fillAvatarSizeIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) {
74 const avatars = accountOrChannel.Actor.Avatars
76 for (const avatar of avatars) {
77 if (avatar.width && avatar.height) continue
79 console.log('Filling size of avatars of %s.', accountOrChannel.name)
81 const { width, height } = await getImageSize(join(CONFIG.STORAGE.ACTOR_IMAGES, avatar.filename))
83 avatar.height = height
89 async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) {
90 const avatars = accountOrChannel.Actor.Avatars
91 if (avatars.length !== 1) {
95 console.log(`Processing ${accountOrChannel.name}.`)
97 await generateSmallerAvatar(accountOrChannel.Actor)
98 accountOrChannel.Actor = Object.assign(accountOrChannel.Actor, { Server: null })
100 return sendUpdateActor(accountOrChannel, undefined)
103 async function generateSmallerAvatar (actor: MActorDefault) {
104 const bigAvatar = getBiggestActorImage(actor.Avatars)
106 const imageSize = minBy(ACTOR_IMAGES_SIZE[ActorImageType.AVATAR], 'width')
107 const sourceFilename = bigAvatar.filename
109 const newImageName = buildUUID() + getLowercaseExtension(sourceFilename)
110 const source = join(CONFIG.STORAGE.ACTOR_IMAGES, sourceFilename)
111 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, newImageName)
113 await processImage(source, destination, imageSize, true)
115 const actorImageInfo = {
118 height: imageSize.height,
119 width: imageSize.width,
123 await updateActorImages(actor, ActorImageType.AVATAR, [ actorImageInfo ], undefined)