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