diff options
Diffstat (limited to 'server/lib/local-actor.ts')
-rw-r--r-- | server/lib/local-actor.ts | 46 |
1 files changed, 7 insertions, 39 deletions
diff --git a/server/lib/local-actor.ts b/server/lib/local-actor.ts index 16dc265a3..611e6d0af 100644 --- a/server/lib/local-actor.ts +++ b/server/lib/local-actor.ts | |||
@@ -1,5 +1,4 @@ | |||
1 | import { remove } from 'fs-extra' | 1 | import { remove } from 'fs-extra' |
2 | import { LRUCache } from 'lru-cache' | ||
3 | import { join } from 'path' | 2 | import { join } from 'path' |
4 | import { Transaction } from 'sequelize/types' | 3 | import { Transaction } from 'sequelize/types' |
5 | import { ActorModel } from '@server/models/actor/actor' | 4 | import { ActorModel } from '@server/models/actor/actor' |
@@ -8,14 +7,14 @@ 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 { CONFIG } from '../initializers/config' | 9 | import { CONFIG } from '../initializers/config' |
11 | import { ACTOR_IMAGES_SIZE, LRU_CACHE, WEBSERVER } from '../initializers/constants' | 10 | import { ACTOR_IMAGES_SIZE, WEBSERVER } from '../initializers/constants' |
12 | import { sequelizeTypescript } from '../initializers/database' | 11 | import { sequelizeTypescript } from '../initializers/database' |
13 | import { MAccountDefault, MActor, MChannelDefault } from '../types/models' | 12 | import { MAccountDefault, MActor, MChannelDefault } from '../types/models' |
14 | import { deleteActorImages, updateActorImages } from './activitypub/actors' | 13 | import { deleteActorImages, updateActorImages } from './activitypub/actors' |
15 | import { sendUpdateActor } from './activitypub/send' | 14 | import { sendUpdateActor } from './activitypub/send' |
16 | import { downloadImageFromWorker, processImageFromWorker } from './worker/parent-process' | 15 | import { processImageFromWorker } from './worker/parent-process' |
17 | 16 | ||
18 | function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) { | 17 | export function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) { |
19 | return new ActorModel({ | 18 | return new ActorModel({ |
20 | type, | 19 | type, |
21 | url, | 20 | url, |
@@ -32,7 +31,7 @@ function buildActorInstance (type: ActivityPubActorType, url: string, preferredU | |||
32 | }) as MActor | 31 | }) as MActor |
33 | } | 32 | } |
34 | 33 | ||
35 | async function updateLocalActorImageFiles ( | 34 | export async function updateLocalActorImageFiles ( |
36 | accountOrChannel: MAccountDefault | MChannelDefault, | 35 | accountOrChannel: MAccountDefault | MChannelDefault, |
37 | imagePhysicalFile: Express.Multer.File, | 36 | imagePhysicalFile: Express.Multer.File, |
38 | type: ActorImageType | 37 | type: ActorImageType |
@@ -41,7 +40,7 @@ async function updateLocalActorImageFiles ( | |||
41 | const extension = getLowercaseExtension(imagePhysicalFile.filename) | 40 | const extension = getLowercaseExtension(imagePhysicalFile.filename) |
42 | 41 | ||
43 | const imageName = buildUUID() + extension | 42 | const imageName = buildUUID() + extension |
44 | const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName) | 43 | const destination = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, imageName) |
45 | await processImageFromWorker({ path: imagePhysicalFile.path, destination, newSize: imageSize, keepOriginal: true }) | 44 | await processImageFromWorker({ path: imagePhysicalFile.path, destination, newSize: imageSize, keepOriginal: true }) |
46 | 45 | ||
47 | return { | 46 | return { |
@@ -73,7 +72,7 @@ async function updateLocalActorImageFiles ( | |||
73 | })) | 72 | })) |
74 | } | 73 | } |
75 | 74 | ||
76 | async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MChannelDefault, type: ActorImageType) { | 75 | export async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MChannelDefault, type: ActorImageType) { |
77 | return retryTransactionWrapper(() => { | 76 | return retryTransactionWrapper(() => { |
78 | return sequelizeTypescript.transaction(async t => { | 77 | return sequelizeTypescript.transaction(async t => { |
79 | const updatedActor = await deleteActorImages(accountOrChannel.Actor, type, t) | 78 | const updatedActor = await deleteActorImages(accountOrChannel.Actor, type, t) |
@@ -88,7 +87,7 @@ async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MC | |||
88 | 87 | ||
89 | // --------------------------------------------------------------------------- | 88 | // --------------------------------------------------------------------------- |
90 | 89 | ||
91 | async function findAvailableLocalActorName (baseActorName: string, transaction?: Transaction) { | 90 | export async function findAvailableLocalActorName (baseActorName: string, transaction?: Transaction) { |
92 | let actor = await ActorModel.loadLocalByName(baseActorName, transaction) | 91 | let actor = await ActorModel.loadLocalByName(baseActorName, transaction) |
93 | if (!actor) return baseActorName | 92 | if (!actor) return baseActorName |
94 | 93 | ||
@@ -101,34 +100,3 @@ async function findAvailableLocalActorName (baseActorName: string, transaction?: | |||
101 | 100 | ||
102 | throw new Error('Cannot find available actor local name (too much iterations).') | 101 | throw new Error('Cannot find available actor local name (too much iterations).') |
103 | } | 102 | } |
104 | |||
105 | // --------------------------------------------------------------------------- | ||
106 | |||
107 | function downloadActorImageFromWorker (options: { | ||
108 | fileUrl: string | ||
109 | filename: string | ||
110 | type: ActorImageType | ||
111 | size: typeof ACTOR_IMAGES_SIZE[ActorImageType][0] | ||
112 | }) { | ||
113 | const downloaderOptions = { | ||
114 | url: options.fileUrl, | ||
115 | destDir: CONFIG.STORAGE.ACTOR_IMAGES, | ||
116 | destName: options.filename, | ||
117 | size: options.size | ||
118 | } | ||
119 | |||
120 | return downloadImageFromWorker(downloaderOptions) | ||
121 | } | ||
122 | |||
123 | // Unsafe so could returns paths that does not exist anymore | ||
124 | const actorImagePathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.ACTOR_IMAGE_STATIC.MAX_SIZE }) | ||
125 | |||
126 | export { | ||
127 | actorImagePathUnsafeCache, | ||
128 | updateLocalActorImageFiles, | ||
129 | findAvailableLocalActorName, | ||
130 | downloadActorImageFromWorker, | ||
131 | deleteLocalActorImageFile, | ||
132 | downloadImageFromWorker, | ||
133 | buildActorInstance | ||
134 | } | ||