2018-06-29 04:29:23 -05:00
|
|
|
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 03:15:50 -05:00
|
|
|
import { retryTransactionWrapper } from '../helpers/database-utils'
|
2018-06-29 04:29:23 -05:00
|
|
|
|
2018-09-20 04:31:48 -05:00
|
|
|
async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, accountOrChannel: AccountModel | VideoChannelModel) {
|
2018-06-29 04:29:23 -05:00
|
|
|
const extension = extname(avatarPhysicalFile.filename)
|
2018-09-20 04:31:48 -05:00
|
|
|
const avatarName = accountOrChannel.Actor.uuid + extension
|
2018-06-29 04:29:23 -05:00
|
|
|
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-06-29 04:29:23 -05:00
|
|
|
|
2018-09-26 03:15:50 -05:00
|
|
|
await sendUpdateActor(accountOrChannel, t)
|
2018-06-29 04:29:23 -05:00
|
|
|
|
2018-09-26 03:15:50 -05:00
|
|
|
return updatedActor.Avatar
|
|
|
|
})
|
2018-06-29 04:29:23 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
updateActorAvatarFile
|
|
|
|
}
|