aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/avatar.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/server/lib/avatar.ts b/server/lib/avatar.ts
new file mode 100644
index 000000000..7fdef008c
--- /dev/null
+++ b/server/lib/avatar.ts
@@ -0,0 +1,34 @@
1import 'multer'
2import * as uuidv4 from 'uuid'
3import { sendUpdateActor } from './activitypub/send'
4import { AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../initializers'
5import { updateActorAvatarInstance } from './activitypub'
6import { processImage } from '../helpers/image-utils'
7import { ActorModel } from '../models/activitypub/actor'
8import { AccountModel } from '../models/account/account'
9import { VideoChannelModel } from '../models/video/video-channel'
10import { extname, join } from 'path'
11
12async function updateActorAvatarFile (
13 avatarPhysicalFile: Express.Multer.File,
14 actor: ActorModel,
15 accountOrChannel: AccountModel | VideoChannelModel
16) {
17 const extension = extname(avatarPhysicalFile.filename)
18 const avatarName = uuidv4() + extension
19 const destination = join(CONFIG.STORAGE.AVATARS_DIR, avatarName)
20 await processImage(avatarPhysicalFile, destination, AVATARS_SIZE)
21
22 return sequelizeTypescript.transaction(async t => {
23 const updatedActor = await updateActorAvatarInstance(actor, avatarName, t)
24 await updatedActor.save({ transaction: t })
25
26 await sendUpdateActor(accountOrChannel, t)
27
28 return updatedActor.Avatar
29 })
30}
31
32export {
33 updateActorAvatarFile
34}