]>
Commit | Line | Data |
---|---|---|
4bbfc6c6 | 1 | import 'multer' |
4bbfc6c6 | 2 | import { sendUpdateActor } from './activitypub/send' |
557b13ae | 3 | import { AVATARS_SIZE, LRU_CACHE, QUEUE_CONCURRENCY } from '../initializers/constants' |
4bbfc6c6 C |
4 | import { updateActorAvatarInstance } from './activitypub' |
5 | import { processImage } from '../helpers/image-utils' | |
4bbfc6c6 C |
6 | import { AccountModel } from '../models/account/account' |
7 | import { VideoChannelModel } from '../models/video/video-channel' | |
8 | import { extname, join } from 'path' | |
4a534352 | 9 | import { retryTransactionWrapper } from '../helpers/database-utils' |
ecf3f060 | 10 | import * as uuidv4 from 'uuid/v4' |
6dd9de95 | 11 | import { CONFIG } from '../initializers/config' |
74dc3bca | 12 | import { sequelizeTypescript } from '../initializers/database' |
557b13ae C |
13 | import * as LRUCache from 'lru-cache' |
14 | import { queue } from 'async' | |
15 | import { downloadImage } from '../helpers/requests' | |
4bbfc6c6 | 16 | |
f201a749 | 17 | async 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 |
41 | type DownloadImageQueueTask = { fileUrl: string, filename: string } |
42 | ||
43 | const 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 | ||
49 | function 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 | |
60 | const avatarPathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.AVATAR_STATIC.MAX_SIZE }) | |
61 | ||
4bbfc6c6 | 62 | export { |
557b13ae C |
63 | avatarPathUnsafeCache, |
64 | updateActorAvatarFile, | |
65 | pushAvatarProcessInQueue | |
4bbfc6c6 | 66 | } |