diff options
Diffstat (limited to 'server/lib/actor-image.ts')
-rw-r--r-- | server/lib/actor-image.ts | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/server/lib/actor-image.ts b/server/lib/actor-image.ts new file mode 100644 index 000000000..ca7f9658d --- /dev/null +++ b/server/lib/actor-image.ts | |||
@@ -0,0 +1,85 @@ | |||
1 | import 'multer' | ||
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 { retryTransactionWrapper } from '../helpers/database-utils' | ||
7 | import { processImage } from '../helpers/image-utils' | ||
8 | import { downloadImage } from '../helpers/requests' | ||
9 | import { CONFIG } from '../initializers/config' | ||
10 | import { AVATARS_SIZE, LRU_CACHE, QUEUE_CONCURRENCY } from '../initializers/constants' | ||
11 | import { sequelizeTypescript } from '../initializers/database' | ||
12 | import { MAccountDefault, MChannelDefault } from '../types/models' | ||
13 | import { deleteActorAvatarInstance, updateActorAvatarInstance } from './activitypub/actor' | ||
14 | import { sendUpdateActor } from './activitypub/send' | ||
15 | |||
16 | async function updateLocalActorAvatarFile ( | ||
17 | accountOrChannel: MAccountDefault | MChannelDefault, | ||
18 | avatarPhysicalFile: Express.Multer.File | ||
19 | ) { | ||
20 | const extension = extname(avatarPhysicalFile.filename) | ||
21 | |||
22 | const avatarName = uuidv4() + extension | ||
23 | const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, avatarName) | ||
24 | await processImage(avatarPhysicalFile.path, destination, AVATARS_SIZE) | ||
25 | |||
26 | return retryTransactionWrapper(() => { | ||
27 | return sequelizeTypescript.transaction(async t => { | ||
28 | const avatarInfo = { | ||
29 | name: avatarName, | ||
30 | fileUrl: null, | ||
31 | onDisk: true | ||
32 | } | ||
33 | |||
34 | const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarInfo, t) | ||
35 | await updatedActor.save({ transaction: t }) | ||
36 | |||
37 | await sendUpdateActor(accountOrChannel, t) | ||
38 | |||
39 | return updatedActor.Avatar | ||
40 | }) | ||
41 | }) | ||
42 | } | ||
43 | |||
44 | async function deleteLocalActorAvatarFile ( | ||
45 | accountOrChannel: MAccountDefault | MChannelDefault | ||
46 | ) { | ||
47 | return retryTransactionWrapper(() => { | ||
48 | return sequelizeTypescript.transaction(async t => { | ||
49 | const updatedActor = await deleteActorAvatarInstance(accountOrChannel.Actor, t) | ||
50 | await updatedActor.save({ transaction: t }) | ||
51 | |||
52 | await sendUpdateActor(accountOrChannel, t) | ||
53 | |||
54 | return updatedActor.Avatar | ||
55 | }) | ||
56 | }) | ||
57 | } | ||
58 | |||
59 | type DownloadImageQueueTask = { fileUrl: string, filename: string } | ||
60 | |||
61 | const downloadImageQueue = queue<DownloadImageQueueTask, Error>((task, cb) => { | ||
62 | downloadImage(task.fileUrl, CONFIG.STORAGE.ACTOR_IMAGES, task.filename, AVATARS_SIZE) | ||
63 | .then(() => cb()) | ||
64 | .catch(err => cb(err)) | ||
65 | }, QUEUE_CONCURRENCY.ACTOR_PROCESS_IMAGE) | ||
66 | |||
67 | function pushActorImageProcessInQueue (task: DownloadImageQueueTask) { | ||
68 | return new Promise<void>((res, rej) => { | ||
69 | downloadImageQueue.push(task, err => { | ||
70 | if (err) return rej(err) | ||
71 | |||
72 | return res() | ||
73 | }) | ||
74 | }) | ||
75 | } | ||
76 | |||
77 | // Unsafe so could returns paths that does not exist anymore | ||
78 | const actorImagePathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.ACTOR_IMAGE_STATIC.MAX_SIZE }) | ||
79 | |||
80 | export { | ||
81 | actorImagePathUnsafeCache, | ||
82 | updateLocalActorAvatarFile, | ||
83 | deleteLocalActorAvatarFile, | ||
84 | pushActorImageProcessInQueue | ||
85 | } | ||