]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/local-actor.ts
Introduce worker threads to process remote images
[github/Chocobozzz/PeerTube.git] / server / lib / local-actor.ts
CommitLineData
d0800f76 1import { remove } from 'fs-extra'
41fb13c3 2import LRUCache from 'lru-cache'
ea54cd04 3import { join } from 'path'
136d7efd 4import { ActorModel } from '@server/models/actor/actor'
0628157f
C
5import { getLowercaseExtension } from '@shared/core-utils'
6import { buildUUID } from '@shared/extra-utils'
136d7efd 7import { ActivityPubActorType, ActorImageType } from '@shared/models'
f4796856
C
8import { retryTransactionWrapper } from '../helpers/database-utils'
9import { processImage } from '../helpers/image-utils'
6dd9de95 10import { CONFIG } from '../initializers/config'
c53853ca 11import { ACTOR_IMAGES_SIZE, LRU_CACHE, WEBSERVER } from '../initializers/constants'
74dc3bca 12import { sequelizeTypescript } from '../initializers/database'
136d7efd 13import { MAccountDefault, MActor, MChannelDefault } from '../types/models'
d0800f76 14import { deleteActorImages, updateActorImages } from './activitypub/actors'
f4796856 15import { sendUpdateActor } from './activitypub/send'
c53853ca 16import { downloadImageFromWorker } from './worker/parent-process'
4bbfc6c6 17
136d7efd
C
18function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) {
19 return new ActorModel({
20 type,
21 url,
22 preferredUsername,
23 publicKey: null,
24 privateKey: null,
25 followersCount: 0,
26 followingCount: 0,
27 inboxUrl: url + '/inbox',
28 outboxUrl: url + '/outbox',
29 sharedInboxUrl: WEBSERVER.URL + '/inbox',
30 followersUrl: url + '/followers',
31 followingUrl: url + '/following'
32 }) as MActor
33}
34
d0800f76 35async function updateLocalActorImageFiles (
1ea7da81 36 accountOrChannel: MAccountDefault | MChannelDefault,
2cb03dc1
C
37 imagePhysicalFile: Express.Multer.File,
38 type: ActorImageType
453e83ea 39) {
d0800f76 40 const processImageSize = async (imageSize: { width: number, height: number }) => {
41 const extension = getLowercaseExtension(imagePhysicalFile.filename)
42
43 const imageName = buildUUID() + extension
44 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName)
45 await processImage(imagePhysicalFile.path, destination, imageSize, true)
46
47 return {
48 imageName,
49 imageSize
50 }
51 }
52
53 const processedImages = await Promise.all(ACTOR_IMAGES_SIZE[type].map(processImageSize))
54 await remove(imagePhysicalFile.path)
55
56 return retryTransactionWrapper(() => sequelizeTypescript.transaction(async t => {
57 const actorImagesInfo = processedImages.map(({ imageName, imageSize }) => ({
58 name: imageName,
59 fileUrl: null,
60 height: imageSize.height,
61 width: imageSize.width,
62 onDisk: true
63 }))
64
65 const updatedActor = await updateActorImages(accountOrChannel.Actor, type, actorImagesInfo, t)
66 await updatedActor.save({ transaction: t })
67
68 await sendUpdateActor(accountOrChannel, t)
69
70 return type === ActorImageType.AVATAR
71 ? updatedActor.Avatars
72 : updatedActor.Banners
73 }))
4bbfc6c6
C
74}
75
2cb03dc1 76async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MChannelDefault, type: ActorImageType) {
1ea7da81
RK
77 return retryTransactionWrapper(() => {
78 return sequelizeTypescript.transaction(async t => {
d0800f76 79 const updatedActor = await deleteActorImages(accountOrChannel.Actor, type, t)
1ea7da81
RK
80 await updatedActor.save({ transaction: t })
81
82 await sendUpdateActor(accountOrChannel, t)
83
d0800f76 84 return updatedActor.Avatars
1ea7da81
RK
85 })
86 })
87}
88
c53853ca
C
89// ---------------------------------------------------------------------------
90
91function downloadActorImageFromWorker (options: {
d0800f76 92 fileUrl: string
93 filename: string
94 type: ActorImageType
95 size: typeof ACTOR_IMAGES_SIZE[ActorImageType][0]
c53853ca
C
96}) {
97 const downloaderOptions = {
98 url: options.fileUrl,
99 destDir: CONFIG.STORAGE.ACTOR_IMAGES,
100 destName: options.filename,
101 size: options.size
102 }
557b13ae 103
c53853ca 104 return downloadImageFromWorker(downloaderOptions)
557b13ae
C
105}
106
107// Unsafe so could returns paths that does not exist anymore
f4796856 108const actorImagePathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.ACTOR_IMAGE_STATIC.MAX_SIZE })
557b13ae 109
4bbfc6c6 110export {
f4796856 111 actorImagePathUnsafeCache,
d0800f76 112 updateLocalActorImageFiles,
c53853ca 113 downloadActorImageFromWorker,
2cb03dc1 114 deleteLocalActorImageFile,
c53853ca 115 downloadImageFromWorker,
136d7efd 116 buildActorInstance
4bbfc6c6 117}