diff options
author | Chocobozzz <me@florianbigard.com> | 2019-08-09 11:32:40 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-08-09 11:32:40 +0200 |
commit | 557b13ae24019d9ab214bbea7eaa0f892c8f4b05 (patch) | |
tree | aa32396531acf93e3dfdb29880177813039ed77f /server/lib/avatar.ts | |
parent | c5407d7046168abb4098df1408e7aa84519cb61a (diff) | |
download | PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.tar.gz PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.tar.zst PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.zip |
Lazy load avatars
Diffstat (limited to 'server/lib/avatar.ts')
-rw-r--r-- | server/lib/avatar.ts | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/server/lib/avatar.ts b/server/lib/avatar.ts index 09b4e38ca..1b38e6cb5 100644 --- a/server/lib/avatar.ts +++ b/server/lib/avatar.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import 'multer' | 1 | import 'multer' |
2 | import { sendUpdateActor } from './activitypub/send' | 2 | import { sendUpdateActor } from './activitypub/send' |
3 | import { AVATARS_SIZE } from '../initializers/constants' | 3 | import { AVATARS_SIZE, LRU_CACHE, QUEUE_CONCURRENCY } from '../initializers/constants' |
4 | import { updateActorAvatarInstance } from './activitypub' | 4 | import { updateActorAvatarInstance } from './activitypub' |
5 | import { processImage } from '../helpers/image-utils' | 5 | import { processImage } from '../helpers/image-utils' |
6 | import { AccountModel } from '../models/account/account' | 6 | import { AccountModel } from '../models/account/account' |
@@ -10,6 +10,9 @@ import { retryTransactionWrapper } from '../helpers/database-utils' | |||
10 | import * as uuidv4 from 'uuid/v4' | 10 | import * as uuidv4 from 'uuid/v4' |
11 | import { CONFIG } from '../initializers/config' | 11 | import { CONFIG } from '../initializers/config' |
12 | import { sequelizeTypescript } from '../initializers/database' | 12 | import { sequelizeTypescript } from '../initializers/database' |
13 | import * as LRUCache from 'lru-cache' | ||
14 | import { queue } from 'async' | ||
15 | import { downloadImage } from '../helpers/requests' | ||
13 | 16 | ||
14 | async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, accountOrChannel: AccountModel | VideoChannelModel) { | 17 | async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, accountOrChannel: AccountModel | VideoChannelModel) { |
15 | const extension = extname(avatarPhysicalFile.filename) | 18 | const extension = extname(avatarPhysicalFile.filename) |
@@ -19,7 +22,13 @@ async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, a | |||
19 | 22 | ||
20 | return retryTransactionWrapper(() => { | 23 | return retryTransactionWrapper(() => { |
21 | return sequelizeTypescript.transaction(async t => { | 24 | return sequelizeTypescript.transaction(async t => { |
22 | const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarName, t) | 25 | const avatarInfo = { |
26 | name: avatarName, | ||
27 | fileUrl: null, | ||
28 | onDisk: true | ||
29 | } | ||
30 | |||
31 | const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarInfo, t) | ||
23 | await updatedActor.save({ transaction: t }) | 32 | await updatedActor.save({ transaction: t }) |
24 | 33 | ||
25 | await sendUpdateActor(accountOrChannel, t) | 34 | await sendUpdateActor(accountOrChannel, t) |
@@ -29,6 +38,29 @@ async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, a | |||
29 | }) | 38 | }) |
30 | } | 39 | } |
31 | 40 | ||
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 | |||
32 | export { | 62 | export { |
33 | updateActorAvatarFile | 63 | avatarPathUnsafeCache, |
64 | updateActorAvatarFile, | ||
65 | pushAvatarProcessInQueue | ||
34 | } | 66 | } |