aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/avatar.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-04-06 11:35:56 +0200
committerChocobozzz <chocobozzz@cpy.re>2021-04-08 10:07:53 +0200
commitf479685678406a5df864d89615b33d29085ebfc6 (patch)
tree8de15e90cd8d97d8810715df8585c61f48d5282a /server/lib/avatar.ts
parent968aaed2066873fc1c39f95168284122d9d15e21 (diff)
downloadPeerTube-f479685678406a5df864d89615b33d29085ebfc6.tar.gz
PeerTube-f479685678406a5df864d89615b33d29085ebfc6.tar.zst
PeerTube-f479685678406a5df864d89615b33d29085ebfc6.zip
Agnostic actor image storage
Diffstat (limited to 'server/lib/avatar.ts')
-rw-r--r--server/lib/avatar.ts85
1 files changed, 0 insertions, 85 deletions
diff --git a/server/lib/avatar.ts b/server/lib/avatar.ts
deleted file mode 100644
index 86f1e7bdb..000000000
--- a/server/lib/avatar.ts
+++ /dev/null
@@ -1,85 +0,0 @@
1import 'multer'
2import { sendUpdateActor } from './activitypub/send'
3import { AVATARS_SIZE, LRU_CACHE, QUEUE_CONCURRENCY } from '../initializers/constants'
4import { updateActorAvatarInstance, deleteActorAvatarInstance } from './activitypub/actor'
5import { processImage } from '../helpers/image-utils'
6import { extname, join } from 'path'
7import { retryTransactionWrapper } from '../helpers/database-utils'
8import { v4 as uuidv4 } from 'uuid'
9import { CONFIG } from '../initializers/config'
10import { sequelizeTypescript } from '../initializers/database'
11import * as LRUCache from 'lru-cache'
12import { queue } from 'async'
13import { downloadImage } from '../helpers/requests'
14import { MAccountDefault, MChannelDefault } from '../types/models'
15
16async function updateLocalActorAvatarFile (
17 accountOrChannel: MAccountDefault | MChannelDefault,
18 avatarPhysicalFile: Express.Multer.File
19) {
20 const extension = extname(avatarPhysicalFile.filename)
21
22 const avatarName = uuidv4() + extension
23 const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
24 await processImage(avatarPhysicalFile.path, destination, AVATARS_SIZE)
25
26 return retryTransactionWrapper(() => {
27 return sequelizeTypescript.transaction(async t => {
28 const avatarInfo = {
29 name: avatarName,
30 fileUrl: null,
31 onDisk: true
32 }
33
34 const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarInfo, t)
35 await updatedActor.save({ transaction: t })
36
37 await sendUpdateActor(accountOrChannel, t)
38
39 return updatedActor.Avatar
40 })
41 })
42}
43
44async function deleteLocalActorAvatarFile (
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
59type DownloadImageQueueTask = { fileUrl: string, filename: string }
60
61const downloadImageQueue = queue<DownloadImageQueueTask, Error>((task, cb) => {
62 downloadImage(task.fileUrl, CONFIG.STORAGE.AVATARS_DIR, task.filename, AVATARS_SIZE)
63 .then(() => cb())
64 .catch(err => cb(err))
65}, QUEUE_CONCURRENCY.AVATAR_PROCESS_IMAGE)
66
67function pushAvatarProcessInQueue (task: DownloadImageQueueTask) {
68 return new Promise<void>((res, rej) => {
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
78const avatarPathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.AVATAR_STATIC.MAX_SIZE })
79
80export {
81 avatarPathUnsafeCache,
82 updateLocalActorAvatarFile,
83 deleteLocalActorAvatarFile,
84 pushAvatarProcessInQueue
85}