2018-06-29 11:29:23 +02:00
|
|
|
import 'multer'
|
|
|
|
import { sendUpdateActor } from './activitypub/send'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { AVATARS_SIZE } from '../initializers/constants'
|
2018-06-29 11:29:23 +02:00
|
|
|
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'
|
2019-04-11 14:26:41 +02:00
|
|
|
import { sequelizeTypescript } from '../initializers/database'
|
2018-06-29 11:29:23 +02:00
|
|
|
|
2018-09-20 11:31:48 +02:00
|
|
|
async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, accountOrChannel: AccountModel | VideoChannelModel) {
|
2018-06-29 11:29:23 +02:00
|
|
|
const extension = extname(avatarPhysicalFile.filename)
|
2018-10-08 10:37:08 +02:00
|
|
|
const avatarName = uuidv4() + extension
|
2018-06-29 11:29:23 +02:00
|
|
|
const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
|
2019-04-24 09:56:25 +02:00
|
|
|
await processImage(avatarPhysicalFile.path, destination, AVATARS_SIZE)
|
2018-06-29 11:29:23 +02:00
|
|
|
|
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-06-29 11:29:23 +02:00
|
|
|
|
2018-09-26 10:15:50 +02:00
|
|
|
await sendUpdateActor(accountOrChannel, t)
|
2018-06-29 11:29:23 +02:00
|
|
|
|
2018-09-26 10:15:50 +02:00
|
|
|
return updatedActor.Avatar
|
|
|
|
})
|
2018-06-29 11:29:23 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
updateActorAvatarFile
|
|
|
|
}
|