master
bhaskar 2 years ago
parent 5f085e2946
commit 18c30d28cd

@ -1203,27 +1203,95 @@ exports.getconnectedCustomers = async (req, reply) => {
}; };
exports.uploadProfilePicture = async (req, reply) => { // exports.uploadProfilePicture = async (req, reply) => {
try { // try {
const supplierId = req.params.supplierId; // const supplierId = req.params.supplierId;
const picture = req.body.picture; // const picture = req.body.picture;
let profilePicture = await profilePictureSupplier.findOne({ supplierId }); // let profilePicture = await profilePictureSupplier.findOne({ supplierId });
console.log(profilePicture,"profile===") // console.log(profilePicture,"profile===")
if (!profilePicture) { // if (!profilePicture) {
profilePicture = new profilePictureSupplier({ // profilePicture = new profilePictureSupplier({
supplierId, // supplierId,
picture, // picture,
}); // });
} else { // } else {
profilePicture.picture = picture; // profilePicture.picture = picture;
} // }
// await profilePicture.save();
// reply.send({ message: 'Profile picture uploaded successfully' });
// } catch (error) {
// reply.status(500).send({ error: error.message });
// }
// };
const multer = require('multer');
const fs = require('fs');
// Multer storage configuration
const storage = multer.diskStorage({
destination: function (req, file, cb) {
// Specify the destination folder for storing uploaded files
cb(null, './uploads');
},
filename: function (req, file, cb) {
// Generate a unique filename for the uploaded file
cb(null, file.originalname);
},
});
// Multer upload configuration
const upload = multer({ storage: storage }).single('picture');
await profilePicture.save(); // Handler for uploading profile picture
exports.uploadProfilePicture = async (req, res) => {
try {
upload(req, res, async (err) => {
if (err) {
return res.status(400).send({ error: 'Failed to upload profile picture' });
}
const supplierId = req.params.supplierId;
const picture = req.file.filename; // Assuming the file field in the request is named 'picture'
let profilePicture = await ProfilePictureSupplier.findOne({ supplierId });
if (!profilePicture) {
profilePicture = new ProfilePictureSupplier({
supplierId,
picture,
});
} else {
profilePicture.picture = picture;
}
await profilePicture.save();
reply.send({ message: 'Profile picture uploaded successfully' }); res.send({ message: 'Profile picture uploaded successfully' });
});
} catch (error) { } catch (error) {
reply.status(500).send({ error: error.message }); res.status(500).send({ error: error.message });
} }
}; };
// Route for fetching profile picture data
fastify.get('/api/users/profile-picture-supplier/:supplierId', async (req, res) => {
try {
const supplierId = req.params.supplierId;
const profilePicture = await ProfilePictureSupplier.findOne({ supplierId });
if (!profilePicture) {
return res.status(404).send({ error: 'Profile picture not found' });
}
// Send the profile picture data as a response
res.send({ picture: profilePicture.picture });
} catch (error) {
res.status(500).send({ error: error.message });
}
});

@ -576,48 +576,32 @@ module.exports = function (fastify, opts, next) {
// }); // });
const multer = require('multer');
const fs = require('fs');
const storage = multer.diskStorage({
destination: function (req, file, cb) {
if (!fs.existsSync(__dirname + '/temp')) {
fs.mkdirSync(__dirname + '/temp');
}
cb(null, './temp');
},
filename: function (req, file, cb) {
cb(null, file.originalname + '-' + Date.now() + '.' + file.mimetype.split('/')[1]);
},
});
const upload = multer({ storage: storage }).single('picture');
fastify.route({ fastify.route({
method: 'POST', method: 'POST',
url: '/api/users/profile-picture-supplier/:supplierId', url: '/api/users/profile-picture-supplier/:supplierId',
schema: { schema: {
tags: ['Supplier'], tags: ['Supplier'],
description: 'Upload a profile picture supplier', description: 'Upload a profile picture for a supplier',
summary: 'Upload a profile picture supplier', summary: 'Upload a profile picture for a supplier',
params: { params: {
type: 'object', type: 'object',
properties: { properties: {
supplierId: { supplierId: {
type: 'string', type: 'string',
description: 'SupplierId', description: 'Supplier ID',
}, },
}, },
}, },
consumes: ['multipart/form-data'],
body: { body: {
formData: { type: 'object',
properties: {
picture: { picture: {
type: 'string', type: 'string',
format: 'binary',
description: 'Profile picture file', description: 'Profile picture file',
}, },
}, },
required: ['picture'],
}, },
response: { response: {
200: { 200: {
@ -643,40 +627,111 @@ module.exports = function (fastify, opts, next) {
}, },
}, },
}, },
handler: async (req, reply) => { handler: validationHandler.uploadProfilePicture,
try { });
upload(req, reply, async (err) => {
if (err) {
reply.status(400).send({ error: 'Failed to upload profile picture' });
return;
}
const supplierId = req.params.supplierId;
const picturePath = req.file.path;
const multer = require('multer');
const fs = require('fs');
let profilePicture = await profilePictureSupplier.findOne({ supplierId }); const storage = multer.diskStorage({
destination: function (req, file, cb) {
if (!fs.existsSync(__dirname + '/temp')) {
fs.mkdirSync(__dirname + '/temp');
}
cb(null, './temp');
},
filename: function (req, file, cb) {
cb(null, file.originalname + '-' + Date.now() + '.' + file.mimetype.split('/')[1]);
},
});
const upload = multer({ storage: storage }).single('picture');
// fastify.route({
// method: 'POST',
// url: '/api/users/profile-picture-supplier/:supplierId',
// schema: {
// tags: ['Supplier'],
// description: 'Upload a profile picture supplier',
// summary: 'Upload a profile picture supplier',
// params: {
// type: 'object',
// properties: {
// supplierId: {
// type: 'string',
// description: 'SupplierId',
// },
// },
// },
// body: {
// formData: {
// picture: {
// type: 'string',
// format: 'binary',
// description: 'Profile picture file',
// },
// },
// },
// response: {
// 200: {
// description: 'Profile picture uploaded successfully',
// type: 'object',
// properties: {
// message: { type: 'string' },
// },
// },
// 400: {
// description: 'Failed to upload profile picture',
// type: 'object',
// properties: {
// error: { type: 'string' },
// },
// },
// 500: {
// description: 'Internal server error',
// type: 'object',
// properties: {
// error: { type: 'string' },
// },
// },
// },
// },
// handler: async (req, reply) => {
// try {
// upload(req, reply, async (err) => {
// if (err) {
// reply.status(400).send({ error: 'Failed to upload profile picture' });
// return;
// }
if (!profilePicture) { // const supplierId = req.params.supplierId;
profilePicture = new ProfilePicture({ // const picturePath = req.file.path;
supplierId,
picture: fs.readFileSync(picturePath),
});
} else {
profilePicture.picture = fs.readFileSync(picturePath);
}
await profilePicture.save(); // let profilePicture = await profilePictureSupplier.findOne({ supplierId });
// Delete the temporary uploaded file // if (!profilePicture) {
fs.unlinkSync(picturePath); // profilePicture = new ProfilePicture({
// supplierId,
// picture: fs.readFileSync(picturePath),
// });
// } else {
// profilePicture.picture = fs.readFileSync(picturePath);
// }
reply.send({ message: 'Profile picture uploaded successfully' }); // await profilePicture.save();
});
} catch (error) { // // Delete the temporary uploaded file
reply.status(500).send({ error: error.message }); // fs.unlinkSync(picturePath);
}
}, // reply.send({ message: 'Profile picture uploaded successfully' });
}); // });
// } catch (error) {
// reply.status(500).send({ error: error.message });
// }
// },
// });
next(); next();
} }

Loading…
Cancel
Save