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 {
const customerId = request.params.customerId;
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 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 writeStream = storage.bucket(bucketName).file(filePath).createWriteStream();
const file = storage.bucket(bucketName).file(filePath);
const writeStream = file.createWriteStream();
// 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();
await new Promise((resolve, reject) => {
writeStream.on('finish', resolve);
writeStream.on('error', reject);
});
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// Make file public
await file.makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
TeamMemberProfilePicture.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' });
}
});
// Update DB with async/await
const picture = await TeamMemberProfilePicture.findOneAndUpdate(
{ customerId },
{ picture: publicUrl },
{ new: true, upsert: true }
);
writeStream.on('error', (err) => {
reply.code(500).send({ error: 'Failed to move file' });
});
reply.send({ picture: publicUrl });
} 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) => {
try {
const customerId = request.params.customerId;

Loading…
Cancel
Save