diff options
Diffstat (limited to 'server/lib/local-actor.ts')
-rw-r--r-- | server/lib/local-actor.ts | 38 |
1 files changed, 16 insertions, 22 deletions
diff --git a/server/lib/local-actor.ts b/server/lib/local-actor.ts index 01046d017..1d9be76e2 100644 --- a/server/lib/local-actor.ts +++ b/server/lib/local-actor.ts | |||
@@ -1,4 +1,3 @@ | |||
1 | import { queue } from 'async' | ||
2 | import { remove } from 'fs-extra' | 1 | import { remove } from 'fs-extra' |
3 | import LRUCache from 'lru-cache' | 2 | import LRUCache from 'lru-cache' |
4 | import { join } from 'path' | 3 | import { join } from 'path' |
@@ -7,14 +6,13 @@ import { getLowercaseExtension } from '@shared/core-utils' | |||
7 | import { buildUUID } from '@shared/extra-utils' | 6 | import { buildUUID } from '@shared/extra-utils' |
8 | import { ActivityPubActorType, ActorImageType } from '@shared/models' | 7 | import { ActivityPubActorType, ActorImageType } from '@shared/models' |
9 | import { retryTransactionWrapper } from '../helpers/database-utils' | 8 | import { retryTransactionWrapper } from '../helpers/database-utils' |
10 | import { processImage } from '../helpers/image-utils' | ||
11 | import { downloadImage } from '../helpers/requests' | ||
12 | import { CONFIG } from '../initializers/config' | 9 | import { CONFIG } from '../initializers/config' |
13 | import { ACTOR_IMAGES_SIZE, LRU_CACHE, QUEUE_CONCURRENCY, WEBSERVER } from '../initializers/constants' | 10 | import { ACTOR_IMAGES_SIZE, LRU_CACHE, WEBSERVER } from '../initializers/constants' |
14 | import { sequelizeTypescript } from '../initializers/database' | 11 | import { sequelizeTypescript } from '../initializers/database' |
15 | import { MAccountDefault, MActor, MChannelDefault } from '../types/models' | 12 | import { MAccountDefault, MActor, MChannelDefault } from '../types/models' |
16 | import { deleteActorImages, updateActorImages } from './activitypub/actors' | 13 | import { deleteActorImages, updateActorImages } from './activitypub/actors' |
17 | import { sendUpdateActor } from './activitypub/send' | 14 | import { sendUpdateActor } from './activitypub/send' |
15 | import { downloadImageFromWorker, processImageFromWorker } from './worker/parent-process' | ||
18 | 16 | ||
19 | function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) { | 17 | function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) { |
20 | return new ActorModel({ | 18 | return new ActorModel({ |
@@ -43,7 +41,7 @@ async function updateLocalActorImageFiles ( | |||
43 | 41 | ||
44 | const imageName = buildUUID() + extension | 42 | const imageName = buildUUID() + extension |
45 | const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName) | 43 | const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName) |
46 | await processImage(imagePhysicalFile.path, destination, imageSize, true) | 44 | await processImageFromWorker({ path: imagePhysicalFile.path, destination, newSize: imageSize, keepOriginal: true }) |
47 | 45 | ||
48 | return { | 46 | return { |
49 | imageName, | 47 | imageName, |
@@ -87,27 +85,22 @@ async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MC | |||
87 | }) | 85 | }) |
88 | } | 86 | } |
89 | 87 | ||
90 | type DownloadImageQueueTask = { | 88 | // --------------------------------------------------------------------------- |
89 | |||
90 | function downloadActorImageFromWorker (options: { | ||
91 | fileUrl: string | 91 | fileUrl: string |
92 | filename: string | 92 | filename: string |
93 | type: ActorImageType | 93 | type: ActorImageType |
94 | size: typeof ACTOR_IMAGES_SIZE[ActorImageType][0] | 94 | size: typeof ACTOR_IMAGES_SIZE[ActorImageType][0] |
95 | } | 95 | }) { |
96 | 96 | const downloaderOptions = { | |
97 | const downloadImageQueue = queue<DownloadImageQueueTask, Error>((task, cb) => { | 97 | url: options.fileUrl, |
98 | downloadImage(task.fileUrl, CONFIG.STORAGE.ACTOR_IMAGES, task.filename, task.size) | 98 | destDir: CONFIG.STORAGE.ACTOR_IMAGES, |
99 | .then(() => cb()) | 99 | destName: options.filename, |
100 | .catch(err => cb(err)) | 100 | size: options.size |
101 | }, QUEUE_CONCURRENCY.ACTOR_PROCESS_IMAGE) | 101 | } |
102 | |||
103 | function pushActorImageProcessInQueue (task: DownloadImageQueueTask) { | ||
104 | return new Promise<void>((res, rej) => { | ||
105 | downloadImageQueue.push(task, err => { | ||
106 | if (err) return rej(err) | ||
107 | 102 | ||
108 | return res() | 103 | return downloadImageFromWorker(downloaderOptions) |
109 | }) | ||
110 | }) | ||
111 | } | 104 | } |
112 | 105 | ||
113 | // Unsafe so could returns paths that does not exist anymore | 106 | // Unsafe so could returns paths that does not exist anymore |
@@ -116,7 +109,8 @@ const actorImagePathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE. | |||
116 | export { | 109 | export { |
117 | actorImagePathUnsafeCache, | 110 | actorImagePathUnsafeCache, |
118 | updateLocalActorImageFiles, | 111 | updateLocalActorImageFiles, |
112 | downloadActorImageFromWorker, | ||
119 | deleteLocalActorImageFile, | 113 | deleteLocalActorImageFile, |
120 | pushActorImageProcessInQueue, | 114 | downloadImageFromWorker, |
121 | buildActorInstance | 115 | buildActorInstance |
122 | } | 116 | } |