feat: add i18n and avatar upload
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import { updateProfileSchema } from '@family-wishlist/shared';
|
||||
import { ConflictError, NotFoundError } from '../../utils/errors.js';
|
||||
import { ConflictError, NotFoundError, ValidationError } from '../../utils/errors.js';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { deleteLocalImageIfAny, saveUploadedAvatar } from '../images/storage.service.js';
|
||||
|
||||
const MAX_AVATAR_BYTES = 2 * 1024 * 1024;
|
||||
|
||||
export default async function profileRoutes(app: FastifyInstance) {
|
||||
app.addHook('preHandler', app.authenticate);
|
||||
@@ -27,4 +30,25 @@ export default async function profileRoutes(app: FastifyInstance) {
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/avatar', async (request) => {
|
||||
const current = await app.prisma.user.findUnique({ where: { id: request.user.id } });
|
||||
if (!current) throw new NotFoundError('Profile');
|
||||
|
||||
const data = await request.file();
|
||||
if (!data) throw new ValidationError('No file uploaded');
|
||||
|
||||
const buffer = await data.toBuffer();
|
||||
if (buffer.byteLength > MAX_AVATAR_BYTES) {
|
||||
throw new ValidationError('Avatar must be 2 MB or less');
|
||||
}
|
||||
|
||||
const { imageUrl } = await saveUploadedAvatar(request.user.id, data.mimetype, buffer);
|
||||
await deleteLocalImageIfAny(current.avatarUrl);
|
||||
|
||||
return app.prisma.user.update({
|
||||
where: { id: request.user.id },
|
||||
data: { avatarUrl: imageUrl },
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user