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

Loading…
Cancel
Save