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