]>
Commit | Line | Data |
---|---|---|
1 | import { remove } from 'fs-extra' | |
2 | import LRUCache from 'lru-cache' | |
3 | import { join } from 'path' | |
4 | import { ActorModel } from '@server/models/actor/actor' | |
5 | import { getLowercaseExtension } from '@shared/core-utils' | |
6 | import { buildUUID } from '@shared/extra-utils' | |
7 | import { ActivityPubActorType, ActorImageType } from '@shared/models' | |
8 | import { retryTransactionWrapper } from '../helpers/database-utils' | |
9 | import { processImage } from '../helpers/image-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 } from './worker/parent-process' | |
17 | ||
18 | function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) { | |
19 | return new ActorModel({ | |
20 | type, | |
21 | url, | |
22 | preferredUsername, | |
23 | publicKey: null, | |
24 | privateKey: null, | |
25 | followersCount: 0, | |
26 | followingCount: 0, | |
27 | inboxUrl: url + '/inbox', | |
28 | outboxUrl: url + '/outbox', | |
29 | sharedInboxUrl: WEBSERVER.URL + '/inbox', | |
30 | followersUrl: url + '/followers', | |
31 | followingUrl: url + '/following' | |
32 | }) as MActor | |
33 | } | |
34 | ||
35 | async function updateLocalActorImageFiles ( | |
36 | accountOrChannel: MAccountDefault | MChannelDefault, | |
37 | imagePhysicalFile: Express.Multer.File, | |
38 | type: ActorImageType | |
39 | ) { | |
40 | const processImageSize = async (imageSize: { width: number, height: number }) => { | |
41 | const extension = getLowercaseExtension(imagePhysicalFile.filename) | |
42 | ||
43 | const imageName = buildUUID() + extension | |
44 | const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName) | |
45 | await processImage(imagePhysicalFile.path, destination, imageSize, true) | |
46 | ||
47 | return { | |
48 | imageName, | |
49 | imageSize | |
50 | } | |
51 | } | |
52 | ||
53 | const processedImages = await Promise.all(ACTOR_IMAGES_SIZE[type].map(processImageSize)) | |
54 | await remove(imagePhysicalFile.path) | |
55 | ||
56 | return retryTransactionWrapper(() => sequelizeTypescript.transaction(async t => { | |
57 | const actorImagesInfo = processedImages.map(({ imageName, imageSize }) => ({ | |
58 | name: imageName, | |
59 | fileUrl: null, | |
60 | height: imageSize.height, | |
61 | width: imageSize.width, | |
62 | onDisk: true | |
63 | })) | |
64 | ||
65 | const updatedActor = await updateActorImages(accountOrChannel.Actor, type, actorImagesInfo, t) | |
66 | await updatedActor.save({ transaction: t }) | |
67 | ||
68 | await sendUpdateActor(accountOrChannel, t) | |
69 | ||
70 | return type === ActorImageType.AVATAR | |
71 | ? updatedActor.Avatars | |
72 | : updatedActor.Banners | |
73 | })) | |
74 | } | |
75 | ||
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 }) | |
81 | ||
82 | await sendUpdateActor(accountOrChannel, t) | |
83 | ||
84 | return updatedActor.Avatars | |
85 | }) | |
86 | }) | |
87 | } | |
88 | ||
89 | // --------------------------------------------------------------------------- | |
90 | ||
91 | function downloadActorImageFromWorker (options: { | |
92 | fileUrl: string | |
93 | filename: string | |
94 | type: ActorImageType | |
95 | size: typeof ACTOR_IMAGES_SIZE[ActorImageType][0] | |
96 | }) { | |
97 | const downloaderOptions = { | |
98 | url: options.fileUrl, | |
99 | destDir: CONFIG.STORAGE.ACTOR_IMAGES, | |
100 | destName: options.filename, | |
101 | size: options.size | |
102 | } | |
103 | ||
104 | return downloadImageFromWorker(downloaderOptions) | |
105 | } | |
106 | ||
107 | // Unsafe so could returns paths that does not exist anymore | |
108 | const actorImagePathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.ACTOR_IMAGE_STATIC.MAX_SIZE }) | |
109 | ||
110 | export { | |
111 | actorImagePathUnsafeCache, | |
112 | updateLocalActorImageFiles, | |
113 | downloadActorImageFromWorker, | |
114 | deleteLocalActorImageFile, | |
115 | downloadImageFromWorker, | |
116 | buildActorInstance | |
117 | } |