PeerTube/server/lib/avatar.ts

33 lines
1.2 KiB
TypeScript
Raw Normal View History

import 'multer'
import { sendUpdateActor } from './activitypub/send'
import { AVATARS_SIZE, CONFIG, 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 11:15:50 +03:00
import { retryTransactionWrapper } from '../helpers/database-utils'
2018-10-08 11:37:08 +03:00
import * as uuidv4 from 'uuid/v4'
2018-09-20 12:31:48 +03:00
async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, accountOrChannel: AccountModel | VideoChannelModel) {
const extension = extname(avatarPhysicalFile.filename)
2018-10-08 11:37:08 +03:00
const avatarName = uuidv4() + extension
const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
await processImage(avatarPhysicalFile, destination, AVATARS_SIZE)
2018-09-26 11:15:50 +03:00
return retryTransactionWrapper(() => {
return sequelizeTypescript.transaction(async t => {
const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarName, t)
await updatedActor.save({ transaction: t })
2018-09-26 11:15:50 +03:00
await sendUpdateActor(accountOrChannel, t)
2018-09-26 11:15:50 +03:00
return updatedActor.Avatar
})
})
}
export {
updateActorAvatarFile
}