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'
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 generateSmallerAvatarIfNeeded(account)
56 console.error(`Cannot process account avatar ${account.name}`, err)
59 for (const videoChannel of account.VideoChannels) {
61 await generateSmallerAvatarIfNeeded(videoChannel)
63 console.error(`Cannot process channel avatar ${videoChannel.name}`, err)
68 console.log('Generation finished!')
71 async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) {
72 const avatars = accountOrChannel.Actor.Avatars
73 if (avatars.length !== 1) {
77 console.log(`Processing ${accountOrChannel.name}.`)
79 await generateSmallerAvatar(accountOrChannel.Actor)
80 accountOrChannel.Actor = Object.assign(accountOrChannel.Actor, { Server: null })
82 return sendUpdateActor(accountOrChannel, undefined)
85 async function generateSmallerAvatar (actor: MActorDefault) {
86 const bigAvatar = getBiggestActorImage(actor.Avatars)
88 const imageSize = minBy(ACTOR_IMAGES_SIZE[ActorImageType.AVATAR], 'width')
89 const sourceFilename = bigAvatar.filename
91 const newImageName = buildUUID() + getLowercaseExtension(sourceFilename)
92 const source = join(CONFIG.STORAGE.ACTOR_IMAGES, sourceFilename)
93 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, newImageName)
95 await processImage(source, destination, imageSize, true)
97 const actorImageInfo = {
100 height: imageSize.height,
101 width: imageSize.width,
105 await updateActorImages(actor, ActorImageType.AVATAR, [ actorImageInfo ], undefined)