|
|
@ -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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|