]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/avatar.ts
Cleaner warning of IP address leaking on embedded videos (#2034)
[github/Chocobozzz/PeerTube.git] / server / lib / avatar.ts
CommitLineData
4bbfc6c6 1import 'multer'
4bbfc6c6 2import { sendUpdateActor } from './activitypub/send'
557b13ae 3import { AVATARS_SIZE, LRU_CACHE, QUEUE_CONCURRENCY } from '../initializers/constants'
4bbfc6c6
C
4import { updateActorAvatarInstance } from './activitypub'
5import { processImage } from '../helpers/image-utils'
4bbfc6c6
C
6import { AccountModel } from '../models/account/account'
7import { VideoChannelModel } from '../models/video/video-channel'
8import { extname, join } from 'path'
4a534352 9import { retryTransactionWrapper } from '../helpers/database-utils'
ecf3f060 10import * as uuidv4 from 'uuid/v4'
6dd9de95 11import { CONFIG } from '../initializers/config'
74dc3bca 12import { sequelizeTypescript } from '../initializers/database'
557b13ae
C
13import * as LRUCache from 'lru-cache'
14import { queue } from 'async'
15import { downloadImage } from '../helpers/requests'
4bbfc6c6 16
f201a749 17async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, accountOrChannel: AccountModel | VideoChannelModel) {
4bbfc6c6 18 const extension = extname(avatarPhysicalFile.filename)
ecf3f060 19 const avatarName = uuidv4() + extension
4bbfc6c6 20 const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
2fb5b3a5 21 await processImage(avatarPhysicalFile.path, destination, AVATARS_SIZE)
4bbfc6c6 22
4a534352
C
23 return retryTransactionWrapper(() => {
24 return sequelizeTypescript.transaction(async t => {
557b13ae
C
25 const avatarInfo = {
26 name: avatarName,
27 fileUrl: null,
28 onDisk: true
29 }
30
31 const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarInfo, t)
4a534352 32 await updatedActor.save({ transaction: t })
4bbfc6c6 33
4a534352 34 await sendUpdateActor(accountOrChannel, t)
4bbfc6c6 35
4a534352
C
36 return updatedActor.Avatar
37 })
4bbfc6c6
C
38 })
39}
40
557b13ae
C
41type DownloadImageQueueTask = { fileUrl: string, filename: string }
42
43const downloadImageQueue = queue<DownloadImageQueueTask, Error>((task, cb) => {
44 downloadImage(task.fileUrl, CONFIG.STORAGE.AVATARS_DIR, task.filename, AVATARS_SIZE)
45 .then(() => cb())
46 .catch(err => cb(err))
47}, QUEUE_CONCURRENCY.AVATAR_PROCESS_IMAGE)
48
49function pushAvatarProcessInQueue (task: DownloadImageQueueTask) {
50 return new Promise((res, rej) => {
51 downloadImageQueue.push(task, err => {
52 if (err) return rej(err)
53
54 return res()
55 })
56 })
57}
58
59// Unsafe so could returns paths that does not exist anymore
60const avatarPathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.AVATAR_STATIC.MAX_SIZE })
61
4bbfc6c6 62export {
557b13ae
C
63 avatarPathUnsafeCache,
64 updateActorAvatarFile,
65 pushAvatarProcessInQueue
4bbfc6c6 66}