mirror of
https://github.com/KeyZox71/knl_meowscendence.git
synced 2025-12-31 21:56:41 +01:00
「🔨」 fix(avatar): fix this shit
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
import fastifyJWT from '@fastify/jwt';
|
||||
import fastifyCookie from '@fastify/cookie';
|
||||
import Database from 'better-sqlite3';
|
||||
import multipart from '@fastify/multipart';
|
||||
|
||||
import { gUsers } from './gUsers.js';
|
||||
import { gUser } from './gUser.js';
|
||||
@ -151,7 +150,13 @@ export default async function(fastify, options) {
|
||||
},
|
||||
});
|
||||
fastify.register(fastifyCookie);
|
||||
fastify.register(multipart, { limits: { fileSize: 2 * 1024 * 1024 + 1 } });
|
||||
|
||||
fastify.addContentTypeParser(
|
||||
['image/jpeg', 'image/png', 'image/gif', 'image/webp'],
|
||||
{ parseAs: 'buffer' },
|
||||
async (request, payload) => payload
|
||||
);
|
||||
|
||||
|
||||
fastify.decorate('authenticate', async function(request, reply) {
|
||||
try {
|
||||
|
||||
@ -1,37 +1,40 @@
|
||||
import sharp from 'sharp';
|
||||
|
||||
/**
|
||||
* @param {import('fastify').FastifyRequest} request
|
||||
* @param {import('fastify').FastifyReply} reply
|
||||
* @param {import('fastify').FastifyInstance} fastify
|
||||
*/
|
||||
export async function pAvatar(request, reply, fastify, getUserInfo, setAvatarId, postImage) {
|
||||
try {
|
||||
const userId = request.params.userId;
|
||||
if (!getUserInfo.get(userId)) {
|
||||
return reply.cose(404).send({ error: "User does not exist" });
|
||||
return reply.code(404).send({ error: "User does not exist" });
|
||||
}
|
||||
const parts = request.parts();
|
||||
for await (const part of parts) {
|
||||
if (part.file) {
|
||||
let size = 0;
|
||||
const chunks = [];
|
||||
for await (const chunk of part.file) {
|
||||
size += chunk.length;
|
||||
chunks.push(chunk);
|
||||
}
|
||||
if (size === 5 * 1024 * 1024 + 1) {
|
||||
return reply.code(400).send({ error: "File too large" });
|
||||
}
|
||||
const buffer = Buffer.concat(chunks);
|
||||
if (!part.filename || part.filename.trim() === '') {
|
||||
return reply.code(400).send({ error: "Missing filename" });
|
||||
}
|
||||
if (!part.mimetype || part.mimetype.trim() === '') {
|
||||
return reply.code(400).send({ error: "Missing mimetype" });
|
||||
}
|
||||
const webpBuffer = await sharp(buffer).toFormat('webp').toBuffer();
|
||||
const imageId = postImage.run(part.filename, part.mimetype, webpBuffer);
|
||||
setAvatarId.run(imageId.lastInsertRowid, userId);
|
||||
return reply.code(200).send({ msg: "Avatar uploaded successfully" });
|
||||
}
|
||||
|
||||
// Read the raw body as a Buffer
|
||||
const buffer = request.body;
|
||||
|
||||
if (!buffer) {
|
||||
return reply.code(400).send({ error: "No file uploaded" });
|
||||
}
|
||||
return reply.code(400).send({ error: "No avatar uploaded" });
|
||||
|
||||
// Check file size (5MB limit)
|
||||
if (buffer.length > 5 * 1024 * 1024) {
|
||||
return reply.code(400).send({ error: "File too large" });
|
||||
}
|
||||
|
||||
// Convert to WebP
|
||||
const webpBuffer = await sharp(buffer).toFormat('webp').toBuffer();
|
||||
|
||||
// Save the image and update the user's avatar
|
||||
const mimeType = request.headers['content-type'];
|
||||
const fileName = `avatar_${userId}.webp`;
|
||||
const imageId = postImage.run(fileName, mimeType, webpBuffer);
|
||||
|
||||
setAvatarId.run(imageId.lastInsertRowid, userId);
|
||||
|
||||
return reply.code(200).send({ msg: "Avatar uploaded successfully" });
|
||||
} catch (err) {
|
||||
fastify.log.error(err);
|
||||
return reply.code(500).send({ error: "Internal server error" });
|
||||
|
||||
Reference in New Issue
Block a user