]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/avatar.ts
Add tmp and redundancy directories
[github/Chocobozzz/PeerTube.git] / server / lib / avatar.ts
... / ...
CommitLineData
1import 'multer'
2import { sendUpdateActor } from './activitypub/send'
3import { AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../initializers'
4import { updateActorAvatarInstance } from './activitypub'
5import { processImage } from '../helpers/image-utils'
6import { AccountModel } from '../models/account/account'
7import { VideoChannelModel } from '../models/video/video-channel'
8import { extname, join } from 'path'
9import { retryTransactionWrapper } from '../helpers/database-utils'
10import * as uuidv4 from 'uuid/v4'
11
12async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, accountOrChannel: AccountModel | VideoChannelModel) {
13 const extension = extname(avatarPhysicalFile.filename)
14 const avatarName = uuidv4() + extension
15 const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
16 await processImage(avatarPhysicalFile, destination, AVATARS_SIZE)
17
18 return retryTransactionWrapper(() => {
19 return sequelizeTypescript.transaction(async t => {
20 const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarName, t)
21 await updatedActor.save({ transaction: t })
22
23 await sendUpdateActor(accountOrChannel, t)
24
25 return updatedActor.Avatar
26 })
27 })
28}
29
30export {
31 updateActorAvatarFile
32}