mscdex/busboy

Unusable uploaded files (with the right module)

Koikarp8877 opened this issue · 1 comments

Hello, i'm trying to upload files on my nextjs 14.0.4 app with fastify/busboy, however, while the files upload in the right repo, with the right name and right extension, they are unsuable "Not a JPEG file: starts with 0xef 0xbf", exemple with an jpeg file. I double checked the files are fine. It's my first nextjs app, any help would be greatly aprreciated. Thanks for your time.

Ps : sorry for the previous version, i sent the wrong code.

import { PrismaClient } from '@prisma/client';
const Busboy = require('busboy');
import fs from 'fs';
//import path from 'path';

const prisma = new PrismaClient();

export const createRessource = async (req, res) => {
try {
let bb = Busboy({
headers: req.headers,
limits: { fileSize: 3000000 },
highWaterMark: undefined,
removeContentLengthHeader: true,
removeBodyParsers: true,
});

const formData = {};

bb.on('file', (fieldname, file, filename, encoding, mimetype) => {
console.log(File [${fieldname}]: filename: ${filename}, encoding: ${encoding}, mimetype: ${mimetype});
const uniquefileName = ${new Date().getTime()}.${filename[filename.length - 1]}
//const uniqueFilename = ${fieldname}-${Date.now()}-${paths.basename(filename)};
const paths = 'public/images/${uniquefileName}';
const buffers = [];

file.on('data', (data) => {
buffers.push(data);
console.log(File [${filename}] got ${data.length} bytes);
});

file.on('end', () => {
console.log(File [${filename}] Finished);
const fileData = Buffer.concat(buffers);
console.log(File [${filename}] Buffer Data:, fileData);

fs.writeFileSync(paths, fileData, 'binary');

console.log(`File [${filename}] saved to [${paths}]`);
formData[fieldname] = {
  filename: uniquefileName,
  path: paths,
  mimetype: mimetype,
  buffer: fileData, 
};
const fileContents = fs.readFileSync(paths);
console.log(`File [${filename}] Contents:`, fileContents);

});
});

bb.on('field', (fieldname, val) => {
console.log(Field [${fieldname}]: value: ${val});
formData[fieldname] = val;
});

bb.on('finish', async () => {
console.log('Done parsing form!');
const { titre, contenu, categorieName, visibilite, filefield } = formData;
const fileInfo = formData[filefield];
const currentDate = new Date();
const newRessource = await prisma.t_ressource.create({
data: {
ressource_titre: titre,
ressource_date: currentDate,
ressource_contenu: contenu,
id_categorie: categorieName,
ressource_media: fileInfo,
ressource_visibilite: visibilite,
},
});

console.log('Ressource créée:', newRessource);
console.log('Nom du fichier :', fileInfo);

res.json({
success: true,
message: 'Ressource créée avec succès',
ressource: newRessource,
});
});

bb.on('error', (err) => {
console.error('Erreur lors du traitement du formulaire :', err);
res.status(500).json({
success: false,
message: 'Erreur lors du traitement du formulaire',
});
});

req.once('data', (chunk) => {
bb.write(chunk);
});

bb.end(req.body);

} catch (error) {
console.error('Erreur lors de la création de la ressource:', error);
res.status(500).json({
success: false,
message: 'Erreur lors de la création de la ressource',
});
} finally {
await prisma.$disconnect();
}
};
My Environment :

node version: v20.11.0
os: Linux
Nextjs 14.0.4, prisma.

I saw my error