2 import { sendUpdateActor } from './activitypub/send'
3 import { AVATARS_SIZE, LRU_CACHE, QUEUE_CONCURRENCY } from '../initializers/constants'
4 import { updateActorAvatarInstance } from './activitypub'
5 import { processImage } from '../helpers/image-utils'
6 import { AccountModel } from '../models/account/account'
7 import { VideoChannelModel } from '../models/video/video-channel'
8 import { extname, join } from 'path'
9 import { retryTransactionWrapper } from '../helpers/database-utils'
10 import * as uuidv4 from 'uuid/v4'
11 import { CONFIG } from '../initializers/config'
12 import { sequelizeTypescript } from '../initializers/database'
13 import * as LRUCache from 'lru-cache'
14 import { queue } from 'async'
15 import { downloadImage } from '../helpers/requests'
17 async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, accountOrChannel: AccountModel | VideoChannelModel) {
18 const extension = extname(avatarPhysicalFile.filename)
19 const avatarName = uuidv4() + extension
20 const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
21 await processImage(avatarPhysicalFile.path, destination, AVATARS_SIZE)
23 return retryTransactionWrapper(() => {
24 return sequelizeTypescript.transaction(async t => {
31 const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarInfo, t)
32 await updatedActor.save({ transaction: t })
34 await sendUpdateActor(accountOrChannel, t)
36 return updatedActor.Avatar
41 type DownloadImageQueueTask = { fileUrl: string, filename: string }
43 const downloadImageQueue = queue<DownloadImageQueueTask, Error>((task, cb) => {
44 downloadImage(task.fileUrl, CONFIG.STORAGE.AVATARS_DIR, task.filename, AVATARS_SIZE)
46 .catch(err => cb(err))
47 }, QUEUE_CONCURRENCY.AVATAR_PROCESS_IMAGE)
49 function pushAvatarProcessInQueue (task: DownloadImageQueueTask) {
50 return new Promise((res, rej) => {
51 downloadImageQueue.push(task, err => {
52 if (err) return rej(err)
59 // Unsafe so could returns paths that does not exist anymore
60 const avatarPathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.AVATAR_STATIC.MAX_SIZE })
63 avatarPathUnsafeCache,
64 updateActorAvatarFile,
65 pushAvatarProcessInQueue