]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/actor-image.ts
Agnostic actor image storage
[github/Chocobozzz/PeerTube.git] / server / lib / actor-image.ts
CommitLineData
4bbfc6c6 1import 'multer'
f4796856
C
2import { queue } from 'async'
3import * as LRUCache from 'lru-cache'
4bbfc6c6 4import { extname, join } from 'path'
bdd428a6 5import { v4 as uuidv4 } from 'uuid'
f4796856
C
6import { retryTransactionWrapper } from '../helpers/database-utils'
7import { processImage } from '../helpers/image-utils'
8import { downloadImage } from '../helpers/requests'
6dd9de95 9import { CONFIG } from '../initializers/config'
f4796856 10import { AVATARS_SIZE, LRU_CACHE, QUEUE_CONCURRENCY } from '../initializers/constants'
74dc3bca 11import { sequelizeTypescript } from '../initializers/database'
26d6bf65 12import { MAccountDefault, MChannelDefault } from '../types/models'
f4796856
C
13import { deleteActorAvatarInstance, updateActorAvatarInstance } from './activitypub/actor'
14import { sendUpdateActor } from './activitypub/send'
4bbfc6c6 15
e08ff02a 16async function updateLocalActorAvatarFile (
1ea7da81
RK
17 accountOrChannel: MAccountDefault | MChannelDefault,
18 avatarPhysicalFile: Express.Multer.File
453e83ea 19) {
4bbfc6c6 20 const extension = extname(avatarPhysicalFile.filename)
e08ff02a 21
ecf3f060 22 const avatarName = uuidv4() + extension
f4796856 23 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, avatarName)
2fb5b3a5 24 await processImage(avatarPhysicalFile.path, destination, AVATARS_SIZE)
4bbfc6c6 25
4a534352
C
26 return retryTransactionWrapper(() => {
27 return sequelizeTypescript.transaction(async t => {
557b13ae
C
28 const avatarInfo = {
29 name: avatarName,
30 fileUrl: null,
31 onDisk: true
32 }
33
34 const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarInfo, t)
4a534352 35 await updatedActor.save({ transaction: t })
4bbfc6c6 36
4a534352 37 await sendUpdateActor(accountOrChannel, t)
4bbfc6c6 38
4a534352
C
39 return updatedActor.Avatar
40 })
4bbfc6c6
C
41 })
42}
43
e08ff02a 44async function deleteLocalActorAvatarFile (
1ea7da81
RK
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
557b13ae
C
59type DownloadImageQueueTask = { fileUrl: string, filename: string }
60
61const downloadImageQueue = queue<DownloadImageQueueTask, Error>((task, cb) => {
f4796856 62 downloadImage(task.fileUrl, CONFIG.STORAGE.ACTOR_IMAGES, task.filename, AVATARS_SIZE)
557b13ae
C
63 .then(() => cb())
64 .catch(err => cb(err))
f4796856 65}, QUEUE_CONCURRENCY.ACTOR_PROCESS_IMAGE)
557b13ae 66
f4796856 67function pushActorImageProcessInQueue (task: DownloadImageQueueTask) {
ba5a8d89 68 return new Promise<void>((res, rej) => {
557b13ae
C
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
f4796856 78const actorImagePathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.ACTOR_IMAGE_STATIC.MAX_SIZE })
557b13ae 79
4bbfc6c6 80export {
f4796856 81 actorImagePathUnsafeCache,
e08ff02a
C
82 updateLocalActorAvatarFile,
83 deleteLocalActorAvatarFile,
f4796856 84 pushActorImageProcessInQueue
4bbfc6c6 85}