mirror of
https://github.com/KeyZox71/knl_meowscendence.git
synced 2025-12-31 21:56:41 +01:00
fixed avatars bloating DB when successive POST occured
This commit is contained in:
@ -229,7 +229,7 @@ export default async function(fastify, options) {
|
||||
return pMatchHistory(request, reply, fastify, getUserInfo, addMatch, incWinsPong, incLossesPong, incWinsTetris, incLossesTetris);
|
||||
});
|
||||
fastify.post('/users/:userId/avatar', { bodyLimit: 5242880, preHandler: [fastify.authenticate] }, async (request, reply) => {
|
||||
return pAvatar(request, reply, fastify, getUserInfo, setAvatarId, postImage);
|
||||
return pAvatar(request, reply, fastify, getUserInfo, getAvatarId, setAvatarId, deleteAvatarId, postImage, deleteImage);
|
||||
});
|
||||
fastify.post('/ping', { preHandler: [fastify.authenticate] }, async (request, reply) => {
|
||||
return pPing(request, reply, fastify, setActivityTime);
|
||||
|
||||
@ -5,7 +5,7 @@ import sharp from 'sharp';
|
||||
* @param {import('fastify').FastifyReply} reply
|
||||
* @param {import('fastify').FastifyInstance} fastify
|
||||
*/
|
||||
export async function pAvatar(request, reply, fastify, getUserInfo, setAvatarId, postImage) {
|
||||
export async function pAvatar(request, reply, fastify, getUserInfo, getAvatarId, setAvatarId, deleteAvatarId, postImage, deleteImage) {
|
||||
try {
|
||||
const userId = request.params.userId;
|
||||
if (request.user !== userId && request.user !== 'admin') {
|
||||
@ -35,6 +35,12 @@ export async function pAvatar(request, reply, fastify, getUserInfo, setAvatarId,
|
||||
const fileName = `avatar_${userId}.webp`;
|
||||
const imageId = postImage.run(fileName, mimeType, webpBuffer);
|
||||
|
||||
const oldImageId = getAvatarId.get(userId);
|
||||
if (oldImageId.avatarId !== -1) {
|
||||
deleteImage.run(oldImageId.avatarId);
|
||||
deleteAvatarId.run(userId);
|
||||
}
|
||||
|
||||
setAvatarId.run(imageId.lastInsertRowid, userId);
|
||||
|
||||
return reply.code(200).send({ msg: "Avatar uploaded successfully" });
|
||||
|
||||
Reference in New Issue
Block a user