PeerTube/server/lib/avatar.ts

34 lines
1.3 KiB
TypeScript
Raw Normal View History

import 'multer'
import { sendUpdateActor } from './activitypub/send'
2019-04-11 11:33:44 +02:00
import { AVATARS_SIZE, sequelizeTypescript } from '../initializers'
import { updateActorAvatarInstance } from './activitypub'
import { processImage } from '../helpers/image-utils'
import { AccountModel } from '../models/account/account'
import { VideoChannelModel } from '../models/video/video-channel'
import { extname, join } from 'path'
2018-09-26 10:15:50 +02:00
import { retryTransactionWrapper } from '../helpers/database-utils'
2018-10-08 10:37:08 +02:00
import * as uuidv4 from 'uuid/v4'
2019-04-11 11:33:44 +02:00
import { CONFIG } from '../initializers/config'
2018-09-20 11:31:48 +02:00
async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, accountOrChannel: AccountModel | VideoChannelModel) {
const extension = extname(avatarPhysicalFile.filename)
2018-10-08 10:37:08 +02:00
const avatarName = uuidv4() + extension
const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
await processImage(avatarPhysicalFile, destination, AVATARS_SIZE)
2018-09-26 10:15:50 +02:00
return retryTransactionWrapper(() => {
return sequelizeTypescript.transaction(async t => {
const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarName, t)
await updatedActor.save({ transaction: t })
2018-09-26 10:15:50 +02:00
await sendUpdateActor(accountOrChannel, t)
2018-09-26 10:15:50 +02:00
return updatedActor.Avatar
})
})
}
export {
updateActorAvatarFile
}