avatar.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import 'multer'
  2. import { sendUpdateActor } from './activitypub/send'
  3. import { AVATARS_SIZE } from '../initializers/constants'
  4. import { updateActorAvatarInstance } from './activitypub'
  5. import { processImage } from '../helpers/image-utils'
  6. import { AccountModel } from '../models/account/account'
  7. import { VideoChannelModel } from '../models/video/video-channel'
  8. import { extname, join } from 'path'
  9. import { retryTransactionWrapper } from '../helpers/database-utils'
  10. import * as uuidv4 from 'uuid/v4'
  11. import { CONFIG } from '../initializers/config'
  12. import { sequelizeTypescript } from '../initializers/database'
  13. async function updateActorAvatarFile (avatarPhysicalFile: Express.Multer.File, accountOrChannel: AccountModel | VideoChannelModel) {
  14. const extension = extname(avatarPhysicalFile.filename)
  15. const avatarName = uuidv4() + extension
  16. const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
  17. await processImage(avatarPhysicalFile.path, destination, AVATARS_SIZE)
  18. return retryTransactionWrapper(() => {
  19. return sequelizeTypescript.transaction(async t => {
  20. const updatedActor = await updateActorAvatarInstance(accountOrChannel.Actor, avatarName, t)
  21. await updatedActor.save({ transaction: t })
  22. await sendUpdateActor(accountOrChannel, t)
  23. return updatedActor.Avatar
  24. })
  25. })
  26. }
  27. export {
  28. updateActorAvatarFile
  29. }