Update 'src/index.js'

master^2
gitadmin 1 week ago
parent fbb05098ae
commit 08a0bca9a7

@ -969,87 +969,87 @@ const storage = new Storage({
// }); // });
const fastifyMulter = require('fastify-multer'); const fastifyMulter = require('fastify-multer');
const mime = require("mime-types"); const mime = require("mime-types");
const upload = fastifyMulter({ // const upload = fastifyMulter({
dest: 'uploads/', // dest: 'uploads/',
limits: { // limits: {
fieldNameSize: 100, // fieldNameSize: 100,
fieldSize: 1000000, // fieldSize: 1000000,
fields: 10, // fields: 10,
fileSize: 1000000000000000, // fileSize: 1000000000000000,
files: 10, // files: 10,
headerPairs: 2000 // headerPairs: 2000
} // }
}); // });
fastify.register(upload.contentParser); fastify.register(upload.contentParser);
fastify.post('/api/uploads_team_profile/:customerId', { // fastify.post('/api/uploads_team_profile/:customerId', {
preHandler: upload.single('file') // preHandler: upload.single('file')
}, async (request, reply) => { // }, async (request, reply) => {
try { // try {
const { customerId } = request.params; // const { customerId } = request.params;
const file = await request.file; // Uncomment this line // const file = await request.file; // Uncomment this line
const formData = new FormData(); // const formData = new FormData();
formData.append('file', file); // formData.append('file', file);
const bucketName = 'arminta_profile_pictures'; // const bucketName = 'arminta_profile_pictures';
const filePath = `arminta_team_profiles/${file.originalname}`; // const filePath = `arminta_team_profiles/${file.originalname}`;
const fileBuffer = await fs.promises.readFile(file.path); // const fileBuffer = await fs.promises.readFile(file.path);
await storage.bucket(bucketName).file(filePath).save(fileBuffer); // await storage.bucket(bucketName).file(filePath).save(fileBuffer);
// make file public // // make file public
await storage.bucket(bucketName).file(filePath).makePublic(); // await storage.bucket(bucketName).file(filePath).makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; // const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// save in DB // // save in DB
const picture = await TeamMemberProfilePicture.findOneAndUpdate( // const picture = await TeamMemberProfilePicture.findOneAndUpdate(
{ customerId }, // { customerId },
{ picture: publicUrl }, // { picture: publicUrl },
{ new: true, upsert: true } // { new: true, upsert: true }
); // );
return reply.send({ picture: publicUrl }); // return reply.send({ picture: publicUrl });
} catch (err) { // } catch (err) {
request.log.error(err); // request.log.error(err);
return reply.code(500).send({ error: 'Upload failed', details: err.message }); // return reply.code(500).send({ error: 'Upload failed', details: err.message });
} // }
}); // });
fastify.post('/api/uploads_admin_profile/:customerId', { // fastify.post('/api/uploads_admin_profile/:customerId', {
preHandler: upload.single('file') // preHandler: upload.single('file')
}, async (request, reply) => { // }, async (request, reply) => {
try { // try {
const { customerId } = request.params; // const { customerId } = request.params;
const file = await request.file; // Uncomment this line // const file = await request.file; // Uncomment this line
const formData = new FormData(); // const formData = new FormData();
formData.append('file', file); // formData.append('file', file);
const bucketName = 'arminta_profile_pictures'; // const bucketName = 'arminta_profile_pictures';
const filePath = `arminta_team_profiles/${file.originalname}`; // const filePath = `arminta_team_profiles/${file.originalname}`;
const fileBuffer = await fs.promises.readFile(file.path); // const fileBuffer = await fs.promises.readFile(file.path);
await storage.bucket(bucketName).file(filePath).save(fileBuffer); // await storage.bucket(bucketName).file(filePath).save(fileBuffer);
// make file public // // make file public
await storage.bucket(bucketName).file(filePath).makePublic(); // await storage.bucket(bucketName).file(filePath).makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; // const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// save in DB // // save in DB
const picture = await AdminProfilePicture.findOneAndUpdate( // const picture = await AdminProfilePicture.findOneAndUpdate(
{ customerId }, // { customerId },
{ picture: publicUrl }, // { picture: publicUrl },
{ new: true, upsert: true } // { new: true, upsert: true }
); // );
return reply.send({ picture: publicUrl }); // return reply.send({ picture: publicUrl });
} catch (err) { // } catch (err) {
request.log.error(err); // request.log.error(err);
return reply.code(500).send({ error: 'Upload failed', details: err.message }); // return reply.code(500).send({ error: 'Upload failed', details: err.message });
} // }
}); // });
// fastify.post("/api/uploads_installation_profile/:installationId", { // fastify.post("/api/uploads_installation_profile/:installationId", {
// preHandler: upload.single("file"), // your multer/fastify-multipart preHandler // preHandler: upload.single("file"), // your multer/fastify-multipart preHandler
@ -1196,41 +1196,41 @@ fastify.post('/api/uploads_admin_profile/:customerId', {
// return reply.code(500).send({ error: 'Upload failed', details: err.message }); // return reply.code(500).send({ error: 'Upload failed', details: err.message });
// } // }
// }); // });
fastify.post('/api/uploads_company_profile/:customerId', async (request, reply) => { // fastify.post('/api/uploads_company_profile/:customerId', async (request, reply) => {
try { // try {
const customerId = request.params.customerId; // const customerId = request.params.customerId;
const data = await request.file(); // const data = await request.file();
const bucketName = 'arminta_profile_pictures'; // const bucketName = 'arminta_profile_pictures';
const filePath = `arminta_company_profiles/${data.filename}`; // const filePath = `arminta_company_profiles/${data.filename}`;
const file = storage.bucket(bucketName).file(filePath); // const file = storage.bucket(bucketName).file(filePath);
const writeStream = file.createWriteStream(); // const writeStream = file.createWriteStream();
data.file.pipe(writeStream); // data.file.pipe(writeStream);
await new Promise((resolve, reject) => { // await new Promise((resolve, reject) => {
writeStream.on('finish', resolve); // writeStream.on('finish', resolve);
writeStream.on('error', reject); // writeStream.on('error', reject);
}); // });
// Make file public // // Make file public
await file.makePublic(); // await file.makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; // const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// Update DB with async/await // // Update DB with async/await
const picture = await CompanyProfilePicture.findOneAndUpdate( // const picture = await CompanyProfilePicture.findOneAndUpdate(
{ customerId }, // { customerId },
{ picture: publicUrl }, // { picture: publicUrl },
{ new: true, upsert: true } // { new: true, upsert: true }
); // );
reply.send({ picture: publicUrl }); // reply.send({ picture: publicUrl });
} catch (err) { // } catch (err) {
console.error(err); // console.error(err);
reply.code(500).send({ error: 'An error occurred', details: err.message }); // reply.code(500).send({ error: 'An error occurred', details: err.message });
} // }
}); // });
// fastify.post("/api/uploads_installation_TeamMember_profile/:installationId/:teamMemberId", { // fastify.post("/api/uploads_installation_TeamMember_profile/:installationId/:teamMemberId", {
// preHandler: upload.single("file"), // preHandler: upload.single("file"),

Loading…
Cancel
Save