Update 'src/index.js'

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

@ -981,7 +981,7 @@ const mime = require("mime-types");
// } // }
// }); // });
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')
@ -1383,59 +1383,59 @@ fastify.post('/api/uploads/:supplierId', async (request, reply) => {
fastify.post('/api/uploads-user/:customerId', async (request, reply) => { // fastify.post('/api/uploads-user/: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();
// Generate a unique file name // // Generate a unique file name
const fileName = `${data.filename}`; // const fileName = `${data.filename}`;
// Define the destination bucket and file path // // Define the destination bucket and file path
const bucketName = 'arminta_profile_pictures'; // const bucketName = 'arminta_profile_pictures';
const filePath = `arminta_user_profiles/${fileName}`; // const filePath = `arminta_user_profiles/${fileName}`;
// Create a write stream to the destination file in the bucket // // Create a write stream to the destination file in the bucket
const writeStream = storage.bucket(bucketName).file(filePath).createWriteStream(); // const writeStream = storage.bucket(bucketName).file(filePath).createWriteStream();
// Pipe the file data to the write stream // // Pipe the file data to the write stream
data.file.pipe(writeStream); // data.file.pipe(writeStream);
writeStream.on('finish', async () => { // writeStream.on('finish', async () => {
try { // try {
// Make the uploaded file publicly accessible // // Make the uploaded file publicly accessible
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}`;
ProfilePicture.findOneAndUpdate( // ProfilePicture.findOneAndUpdate(
{ customerId }, // { customerId },
{ picture: publicUrl }, // { picture: publicUrl },
{ new: true, upsert: true }, // { new: true, upsert: true },
(error, picture) => { // (error, picture) => {
if (error) { // if (error) {
reply.code(500).send({ error: 'Failed to update database' }); // reply.code(500).send({ error: 'Failed to update database' });
} else { // } else {
// Return the public URL // // Return the public URL
reply.send({ picture: publicUrl }); // reply.send({ picture: publicUrl });
} // }
} // }
); // );
} catch (error) { // } catch (error) {
reply.code(500).send({ error: 'Failed to make file public' }); // reply.code(500).send({ error: 'Failed to make file public' });
} // }
}); // });
writeStream.on('error', (err) => { // writeStream.on('error', (err) => {
reply.code(500).send({ error: 'Failed to move file' }); // reply.code(500).send({ error: 'Failed to move file' });
}); // });
} catch (err) { // } catch (err) {
reply.code(500).send({ error: 'An error occurred' }); // reply.code(500).send({ error: 'An error occurred' });
} // }
}); // });
// fastify.post("/api/uploads-electricty-work/:customerId/:installationId", async (request, reply) => { // fastify.post("/api/uploads-electricty-work/:customerId/:installationId", async (request, reply) => {

Loading…
Cancel
Save