diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-03 16:02:29 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-03 16:40:32 +0200 |
commit | 136d7efde798d3dc0ec0dd18aac674365f7d162e (patch) | |
tree | 3a0e2a7a5d04dedf0d8ffda99c2787cecb838891 /server/lib/actor-image.ts | |
parent | 49af5ac8c2653cb0ef23479c9d3256c5b724d49d (diff) | |
download | PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.tar.gz PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.tar.zst PeerTube-136d7efde798d3dc0ec0dd18aac674365f7d162e.zip |
Refactor AP actors
Diffstat (limited to 'server/lib/actor-image.ts')
-rw-r--r-- | server/lib/actor-image.ts | 97 |
1 files changed, 0 insertions, 97 deletions
diff --git a/server/lib/actor-image.ts b/server/lib/actor-image.ts deleted file mode 100644 index f271f0b5b..000000000 --- a/server/lib/actor-image.ts +++ /dev/null | |||
@@ -1,97 +0,0 @@ | |||
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 { 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' | ||
16 | |||
17 | async function updateLocalActorImageFile ( | ||
18 | accountOrChannel: MAccountDefault | MChannelDefault, | ||
19 | imagePhysicalFile: Express.Multer.File, | ||
20 | type: ActorImageType | ||
21 | ) { | ||
22 | const imageSize = type === ActorImageType.AVATAR | ||
23 | ? ACTOR_IMAGES_SIZE.AVATARS | ||
24 | : ACTOR_IMAGES_SIZE.BANNERS | ||
25 | |||
26 | const extension = extname(imagePhysicalFile.filename) | ||
27 | |||
28 | const imageName = uuidv4() + extension | ||
29 | const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName) | ||
30 | await processImage(imagePhysicalFile.path, destination, imageSize) | ||
31 | |||
32 | return retryTransactionWrapper(() => { | ||
33 | return sequelizeTypescript.transaction(async t => { | ||
34 | const actorImageInfo = { | ||
35 | name: imageName, | ||
36 | fileUrl: null, | ||
37 | height: imageSize.height, | ||
38 | width: imageSize.width, | ||
39 | onDisk: true | ||
40 | } | ||
41 | |||
42 | const updatedActor = await updateActorImageInstance(accountOrChannel.Actor, type, actorImageInfo, t) | ||
43 | await updatedActor.save({ transaction: t }) | ||
44 | |||
45 | await sendUpdateActor(accountOrChannel, t) | ||
46 | |||
47 | return type === ActorImageType.AVATAR | ||
48 | ? updatedActor.Avatar | ||
49 | : updatedActor.Banner | ||
50 | }) | ||
51 | }) | ||
52 | } | ||
53 | |||
54 | async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MChannelDefault, type: ActorImageType) { | ||
55 | return retryTransactionWrapper(() => { | ||
56 | return sequelizeTypescript.transaction(async t => { | ||
57 | const updatedActor = await deleteActorImageInstance(accountOrChannel.Actor, type, t) | ||
58 | await updatedActor.save({ transaction: t }) | ||
59 | |||
60 | await sendUpdateActor(accountOrChannel, t) | ||
61 | |||
62 | return updatedActor.Avatar | ||
63 | }) | ||
64 | }) | ||
65 | } | ||
66 | |||
67 | type DownloadImageQueueTask = { fileUrl: string, filename: string, type: ActorImageType } | ||
68 | |||
69 | const downloadImageQueue = queue<DownloadImageQueueTask, Error>((task, cb) => { | ||
70 | const size = task.type === ActorImageType.AVATAR | ||
71 | ? ACTOR_IMAGES_SIZE.AVATARS | ||
72 | : ACTOR_IMAGES_SIZE.BANNERS | ||
73 | |||
74 | downloadImage(task.fileUrl, CONFIG.STORAGE.ACTOR_IMAGES, task.filename, size) | ||
75 | .then(() => cb()) | ||
76 | .catch(err => cb(err)) | ||
77 | }, QUEUE_CONCURRENCY.ACTOR_PROCESS_IMAGE) | ||
78 | |||
79 | function pushActorImageProcessInQueue (task: DownloadImageQueueTask) { | ||
80 | return new Promise<void>((res, rej) => { | ||
81 | downloadImageQueue.push(task, err => { | ||
82 | if (err) return rej(err) | ||
83 | |||
84 | return res() | ||
85 | }) | ||
86 | }) | ||
87 | } | ||
88 | |||
89 | // Unsafe so could returns paths that does not exist anymore | ||
90 | const actorImagePathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.ACTOR_IMAGE_STATIC.MAX_SIZE }) | ||
91 | |||
92 | export { | ||
93 | actorImagePathUnsafeCache, | ||
94 | updateLocalActorImageFile, | ||
95 | deleteLocalActorImageFile, | ||
96 | pushActorImageProcessInQueue | ||
97 | } | ||