team member profile picture

master^2
Bhaskar 8 months ago
parent 83af35801b
commit 9a7fb64d02

@ -741,57 +741,44 @@ fastify.register(require('fastify-multipart'));
// } // }
// }); // });
fastify.post('/api/uploads_team_member/:customerId', async (request, reply) => { fastify.post('/api/uploads_team_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();
// Generate a unique file name
const fileName = `${data.filename}`;
// Define the destination bucket and file path
const bucketName = 'arminta_profile_pictures'; const bucketName = 'arminta_profile_pictures';
const filePath = `arminta_team_member_profiles/${fileName}`; const filePath = `arminta_team_profiles/${data.filename}`;
// Create a write stream to the destination file in the bucket const file = storage.bucket(bucketName).file(filePath);
const writeStream = storage.bucket(bucketName).file(filePath).createWriteStream(); const writeStream = file.createWriteStream();
// Pipe the file data to the write stream
data.file.pipe(writeStream); data.file.pipe(writeStream);
writeStream.on('finish', async () => { await new Promise((resolve, reject) => {
try { writeStream.on('finish', resolve);
// Make the uploaded file publicly accessible writeStream.on('error', reject);
await storage.bucket(bucketName).file(filePath).makePublic(); });
// Make file public
await file.makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`; const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
TeamMemberProfilePicture.findOneAndUpdate( // Update DB with async/await
const picture = await TeamMemberProfilePicture.findOneAndUpdate(
{ customerId }, { customerId },
{ picture: publicUrl }, { picture: publicUrl },
{ new: true, upsert: true }, { 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.send({ picture: publicUrl });
reply.code(500).send({ error: 'Failed to move file' });
});
} catch (err) { } catch (err) {
reply.code(500).send({ error: 'An error occurred' }); console.error(err);
reply.code(500).send({ error: 'An error occurred', 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;

Loading…
Cancel
Save