PeerTube/server/lib/avatar.ts

35 lines
1.3 KiB
TypeScript
Raw Normal View History

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