master
bhaskar 2 years ago
parent 5f085e2946
commit 18c30d28cd

@ -1203,16 +1203,66 @@ exports.getconnectedCustomers = async (req, reply) => {
};
exports.uploadProfilePicture = async (req, reply) => {
// exports.uploadProfilePicture = async (req, reply) => {
// try {
// const supplierId = req.params.supplierId;
// const picture = req.body.picture;
// let profilePicture = await profilePictureSupplier.findOne({ supplierId });
// console.log(profilePicture,"profile===")
// if (!profilePicture) {
// profilePicture = new profilePictureSupplier({
// supplierId,
// picture,
// });
// } else {
// 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');
// 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.body.picture;
const picture = req.file.filename; // Assuming the file field in the request is named 'picture'
let profilePicture = await profilePictureSupplier.findOne({ supplierId });
let profilePicture = await ProfilePictureSupplier.findOne({ supplierId });
console.log(profilePicture,"profile===")
if (!profilePicture) {
profilePicture = new profilePictureSupplier({
profilePicture = new ProfilePictureSupplier({
supplierId,
picture,
});
@ -1222,8 +1272,26 @@ exports.uploadProfilePicture = async (req, reply) => {
await profilePicture.save();
reply.send({ message: 'Profile picture uploaded successfully' });
res.send({ message: 'Profile picture uploaded successfully' });
});
} 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({
method: 'POST',
url: '/api/users/profile-picture-supplier/:supplierId',
schema: {
tags: ['Supplier'],
description: 'Upload a profile picture supplier',
summary: 'Upload a profile picture supplier',
description: 'Upload a profile picture for a supplier',
summary: 'Upload a profile picture for a supplier',
params: {
type: 'object',
properties: {
supplierId: {
type: 'string',
description: 'SupplierId',
description: 'Supplier ID',
},
},
},
consumes: ['multipart/form-data'],
body: {
formData: {
type: 'object',
properties: {
picture: {
type: 'string',
format: 'binary',
description: 'Profile picture file',
},
},
required: ['picture'],
},
response: {
200: {
@ -643,41 +627,112 @@ module.exports = function (fastify, opts, next) {
},
},
},
handler: async (req, reply) => {
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;
let profilePicture = await profilePictureSupplier.findOne({ supplierId });
if (!profilePicture) {
profilePicture = new ProfilePicture({
supplierId,
picture: fs.readFileSync(picturePath),
handler: validationHandler.uploadProfilePicture,
});
} else {
profilePicture.picture = fs.readFileSync(picturePath);
}
await profilePicture.save();
// Delete the temporary uploaded file
fs.unlinkSync(picturePath);
reply.send({ message: 'Profile picture uploaded successfully' });
});
} catch (error) {
reply.status(500).send({ error: error.message });
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({
// 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;
// }
// const supplierId = req.params.supplierId;
// const picturePath = req.file.path;
// let profilePicture = await profilePictureSupplier.findOne({ supplierId });
// if (!profilePicture) {
// profilePicture = new ProfilePicture({
// supplierId,
// picture: fs.readFileSync(picturePath),
// });
// } else {
// profilePicture.picture = fs.readFileSync(picturePath);
// }
// await profilePicture.save();
// // Delete the temporary uploaded file
// fs.unlinkSync(picturePath);
// reply.send({ message: 'Profile picture uploaded successfully' });
// });
// } catch (error) {
// reply.status(500).send({ error: error.message });
// }
// },
// });
next();
}

Loading…
Cancel
Save