2 import { queue } from 'async'
3 import * as LRUCache from 'lru-cache'
4 import { extname, join } from 'path'
5 import { v4 as uuidv4 } from 'uuid'
6 import { ActorImageType } from '@shared/models'
7 import { retryTransactionWrapper } from '../helpers/database-utils'
8 import { processImage } from '../helpers/image-utils'
9 import { downloadImage } from '../helpers/requests'
10 import { CONFIG } from '../initializers/config'
11 import { ACTOR_IMAGES_SIZE, LRU_CACHE, QUEUE_CONCURRENCY } from '../initializers/constants'
12 import { sequelizeTypescript } from '../initializers/database'
13 import { MAccountDefault, MChannelDefault } from '../types/models'
14 import { deleteActorImageInstance, updateActorImageInstance } from './activitypub/actor'
15 import { sendUpdateActor } from './activitypub/send'
17 async function updateLocalActorImageFile (
18 accountOrChannel: MAccountDefault | MChannelDefault,
19 imagePhysicalFile: Express.Multer.File,
22 const imageSize = type === ActorImageType.AVATAR
23 ? ACTOR_IMAGES_SIZE.AVATARS
24 : ACTOR_IMAGES_SIZE.BANNERS
26 const extension = extname(imagePhysicalFile.filename)
28 const imageName = uuidv4() + extension
29 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName)
30 await processImage(imagePhysicalFile.path, destination, imageSize)
32 return retryTransactionWrapper(() => {
33 return sequelizeTypescript.transaction(async t => {
34 const actorImageInfo = {
41 const updatedActor = await updateActorImageInstance(accountOrChannel.Actor, actorImageInfo, t)
42 await updatedActor.save({ transaction: t })
44 await sendUpdateActor(accountOrChannel, t)
46 return type === ActorImageType.AVATAR
53 async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MChannelDefault, type: ActorImageType) {
54 return retryTransactionWrapper(() => {
55 return sequelizeTypescript.transaction(async t => {
56 const updatedActor = await deleteActorImageInstance(accountOrChannel.Actor, type, t)
57 await updatedActor.save({ transaction: t })
59 await sendUpdateActor(accountOrChannel, t)
61 return updatedActor.Avatar
66 type DownloadImageQueueTask = { fileUrl: string, filename: string, type: ActorImageType }
68 const downloadImageQueue = queue<DownloadImageQueueTask, Error>((task, cb) => {
69 const size = task.type === ActorImageType.AVATAR
70 ? ACTOR_IMAGES_SIZE.AVATARS
71 : ACTOR_IMAGES_SIZE.BANNERS
73 downloadImage(task.fileUrl, CONFIG.STORAGE.ACTOR_IMAGES, task.filename, size)
75 .catch(err => cb(err))
76 }, QUEUE_CONCURRENCY.ACTOR_PROCESS_IMAGE)
78 function pushActorImageProcessInQueue (task: DownloadImageQueueTask) {
79 return new Promise<void>((res, rej) => {
80 downloadImageQueue.push(task, err => {
81 if (err) return rej(err)
88 // Unsafe so could returns paths that does not exist anymore
89 const actorImagePathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.ACTOR_IMAGE_STATIC.MAX_SIZE })
92 actorImagePathUnsafeCache,
93 updateLocalActorImageFile,
94 deleteLocalActorImageFile,
95 pushActorImageProcessInQueue