diff options
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/avatar.ts | 34 |
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 @@ | |||
1 | import 'multer' | ||
2 | import * as uuidv4 from 'uuid' | ||
3 | import { sendUpdateActor } from './activitypub/send' | ||
4 | import { AVATARS_SIZE, CONFIG, sequelizeTypescript } from '../initializers' | ||
5 | import { updateActorAvatarInstance } from './activitypub' | ||
6 | import { processImage } from '../helpers/image-utils' | ||
7 | import { ActorModel } from '../models/activitypub/actor' | ||
8 | import { AccountModel } from '../models/account/account' | ||
9 | import { VideoChannelModel } from '../models/video/video-channel' | ||
10 | import { extname, join } from 'path' | ||
11 | |||
12 | async 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 | |||
32 | export { | ||
33 | updateActorAvatarFile | ||
34 | } | ||