1 import { remove } from 'fs-extra'
2 import { LRUCache } from 'lru-cache'
3 import { join } from 'path'
4 import { Transaction } from 'sequelize/types'
5 import { ActorModel } from '@server/models/actor/actor'
6 import { getLowercaseExtension } from '@shared/core-utils'
7 import { buildUUID } from '@shared/extra-utils'
8 import { ActivityPubActorType, ActorImageType } from '@shared/models'
9 import { retryTransactionWrapper } from '../helpers/database-utils'
10 import { CONFIG } from '../initializers/config'
11 import { ACTOR_IMAGES_SIZE, LRU_CACHE, WEBSERVER } from '../initializers/constants'
12 import { sequelizeTypescript } from '../initializers/database'
13 import { MAccountDefault, MActor, MChannelDefault } from '../types/models'
14 import { deleteActorImages, updateActorImages } from './activitypub/actors'
15 import { sendUpdateActor } from './activitypub/send'
16 import { downloadImageFromWorker, processImageFromWorker } from './worker/parent-process'
18 function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) {
19 return new ActorModel({
27 inboxUrl: url + '/inbox',
28 outboxUrl: url + '/outbox',
29 sharedInboxUrl: WEBSERVER.URL + '/inbox',
30 followersUrl: url + '/followers',
31 followingUrl: url + '/following'
35 async function updateLocalActorImageFiles (
36 accountOrChannel: MAccountDefault | MChannelDefault,
37 imagePhysicalFile: Express.Multer.File,
40 const processImageSize = async (imageSize: { width: number, height: number }) => {
41 const extension = getLowercaseExtension(imagePhysicalFile.filename)
43 const imageName = buildUUID() + extension
44 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName)
45 await processImageFromWorker({ path: imagePhysicalFile.path, destination, newSize: imageSize, keepOriginal: true })
53 const processedImages = await Promise.all(ACTOR_IMAGES_SIZE[type].map(processImageSize))
54 await remove(imagePhysicalFile.path)
56 return retryTransactionWrapper(() => sequelizeTypescript.transaction(async t => {
57 const actorImagesInfo = processedImages.map(({ imageName, imageSize }) => ({
60 height: imageSize.height,
61 width: imageSize.width,
65 const updatedActor = await updateActorImages(accountOrChannel.Actor, type, actorImagesInfo, t)
66 await updatedActor.save({ transaction: t })
68 await sendUpdateActor(accountOrChannel, t)
70 return type === ActorImageType.AVATAR
71 ? updatedActor.Avatars
72 : updatedActor.Banners
76 async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MChannelDefault, type: ActorImageType) {
77 return retryTransactionWrapper(() => {
78 return sequelizeTypescript.transaction(async t => {
79 const updatedActor = await deleteActorImages(accountOrChannel.Actor, type, t)
80 await updatedActor.save({ transaction: t })
82 await sendUpdateActor(accountOrChannel, t)
84 return updatedActor.Avatars
89 // ---------------------------------------------------------------------------
91 async function findAvailableLocalActorName (baseActorName: string, transaction?: Transaction) {
92 let actor = await ActorModel.loadLocalByName(baseActorName, transaction)
93 if (!actor) return baseActorName
95 for (let i = 1; i < 30; i++) {
96 const name = `${baseActorName}-${i}`
98 actor = await ActorModel.loadLocalByName(name, transaction)
99 if (!actor) return name
102 throw new Error('Cannot find available actor local name (too much iterations).')
105 // ---------------------------------------------------------------------------
107 function downloadActorImageFromWorker (options: {
111 size: typeof ACTOR_IMAGES_SIZE[ActorImageType][0]
113 const downloaderOptions = {
114 url: options.fileUrl,
115 destDir: CONFIG.STORAGE.ACTOR_IMAGES,
116 destName: options.filename,
120 return downloadImageFromWorker(downloaderOptions)
123 // Unsafe so could returns paths that does not exist anymore
124 const actorImagePathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.ACTOR_IMAGE_STATIC.MAX_SIZE })
127 actorImagePathUnsafeCache,
128 updateLocalActorImageFiles,
129 findAvailableLocalActorName,
130 downloadActorImageFromWorker,
131 deleteLocalActorImageFile,
132 downloadImageFromWorker,