Update 'src/index.js'

master^2
gitadmin 1 week ago
parent fbb05098ae
commit 08a0bca9a7

@ -969,87 +969,87 @@ const storage = new Storage({
// });
const fastifyMulter = require('fastify-multer');
const mime = require("mime-types");
const upload = fastifyMulter({
dest: 'uploads/',
limits: {
fieldNameSize: 100,
fieldSize: 1000000,
fields: 10,
fileSize: 1000000000000000,
files: 10,
headerPairs: 2000
}
});
// const upload = fastifyMulter({
// dest: 'uploads/',
// limits: {
// fieldNameSize: 100,
// fieldSize: 1000000,
// fields: 10,
// fileSize: 1000000000000000,
// files: 10,
// headerPairs: 2000
// }
// });
fastify.register(upload.contentParser);
fastify.post('/api/uploads_team_profile/:customerId', {
preHandler: upload.single('file')
}, async (request, reply) => {
try {
const { customerId } = request.params;
const file = await request.file; // Uncomment this line
const formData = new FormData();
formData.append('file', file);
const bucketName = 'arminta_profile_pictures';
const filePath = `arminta_team_profiles/${file.originalname}`;
// fastify.post('/api/uploads_team_profile/:customerId', {
// preHandler: upload.single('file')
// }, async (request, reply) => {
// try {
// const { customerId } = request.params;
// const file = await request.file; // Uncomment this line
// const formData = new FormData();
// formData.append('file', file);
// const bucketName = 'arminta_profile_pictures';
// const filePath = `arminta_team_profiles/${file.originalname}`;
const fileBuffer = await fs.promises.readFile(file.path);
// const fileBuffer = await fs.promises.readFile(file.path);
await storage.bucket(bucketName).file(filePath).save(fileBuffer);
// await storage.bucket(bucketName).file(filePath).save(fileBuffer);
// make file public
await storage.bucket(bucketName).file(filePath).makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// // make file public
// await storage.bucket(bucketName).file(filePath).makePublic();
// const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// save in DB
const picture = await TeamMemberProfilePicture.findOneAndUpdate(
{ customerId },
{ picture: publicUrl },
{ new: true, upsert: true }
);
// // save in DB
// const picture = await TeamMemberProfilePicture.findOneAndUpdate(
// { customerId },
// { picture: publicUrl },
// { new: true, upsert: true }
// );
return reply.send({ picture: publicUrl });
// return reply.send({ picture: publicUrl });
} catch (err) {
request.log.error(err);
return reply.code(500).send({ error: 'Upload failed', details: err.message });
}
});
// } catch (err) {
// request.log.error(err);
// return reply.code(500).send({ error: 'Upload failed', details: err.message });
// }
// });
fastify.post('/api/uploads_admin_profile/:customerId', {
preHandler: upload.single('file')
}, async (request, reply) => {
try {
const { customerId } = request.params;
const file = await request.file; // Uncomment this line
const formData = new FormData();
formData.append('file', file);
const bucketName = 'arminta_profile_pictures';
const filePath = `arminta_team_profiles/${file.originalname}`;
// fastify.post('/api/uploads_admin_profile/:customerId', {
// preHandler: upload.single('file')
// }, async (request, reply) => {
// try {
// const { customerId } = request.params;
// const file = await request.file; // Uncomment this line
// const formData = new FormData();
// formData.append('file', file);
// const bucketName = 'arminta_profile_pictures';
// const filePath = `arminta_team_profiles/${file.originalname}`;
const fileBuffer = await fs.promises.readFile(file.path);
// const fileBuffer = await fs.promises.readFile(file.path);
await storage.bucket(bucketName).file(filePath).save(fileBuffer);
// await storage.bucket(bucketName).file(filePath).save(fileBuffer);
// make file public
await storage.bucket(bucketName).file(filePath).makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// // make file public
// await storage.bucket(bucketName).file(filePath).makePublic();
// const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// save in DB
const picture = await AdminProfilePicture.findOneAndUpdate(
{ customerId },
{ picture: publicUrl },
{ new: true, upsert: true }
);
// // save in DB
// const picture = await AdminProfilePicture.findOneAndUpdate(
// { customerId },
// { picture: publicUrl },
// { new: true, upsert: true }
// );
return reply.send({ picture: publicUrl });
// return reply.send({ picture: publicUrl });
} catch (err) {
request.log.error(err);
return reply.code(500).send({ error: 'Upload failed', details: err.message });
}
});
// } catch (err) {
// request.log.error(err);
// return reply.code(500).send({ error: 'Upload failed', details: err.message });
// }
// });
// fastify.post("/api/uploads_installation_profile/:installationId", {
// preHandler: upload.single("file"), // your multer/fastify-multipart preHandler
@ -1196,41 +1196,41 @@ fastify.post('/api/uploads_admin_profile/:customerId', {
// return reply.code(500).send({ error: 'Upload failed', details: err.message });
// }
// });
fastify.post('/api/uploads_company_profile/:customerId', async (request, reply) => {
try {
const customerId = request.params.customerId;
const data = await request.file();
// fastify.post('/api/uploads_company_profile/:customerId', async (request, reply) => {
// try {
// const customerId = request.params.customerId;
// const data = await request.file();
const bucketName = 'arminta_profile_pictures';
const filePath = `arminta_company_profiles/${data.filename}`;
// const bucketName = 'arminta_profile_pictures';
// const filePath = `arminta_company_profiles/${data.filename}`;
const file = storage.bucket(bucketName).file(filePath);
const writeStream = file.createWriteStream();
// const file = storage.bucket(bucketName).file(filePath);
// const writeStream = file.createWriteStream();
data.file.pipe(writeStream);
// data.file.pipe(writeStream);
await new Promise((resolve, reject) => {
writeStream.on('finish', resolve);
writeStream.on('error', reject);
});
// await new Promise((resolve, reject) => {
// writeStream.on('finish', resolve);
// writeStream.on('error', reject);
// });
// Make file public
await file.makePublic();
const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// // Make file public
// await file.makePublic();
// const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// Update DB with async/await
const picture = await CompanyProfilePicture.findOneAndUpdate(
{ customerId },
{ picture: publicUrl },
{ new: true, upsert: true }
);
// // Update DB with async/await
// const picture = await CompanyProfilePicture.findOneAndUpdate(
// { customerId },
// { picture: publicUrl },
// { new: true, upsert: true }
// );
reply.send({ picture: publicUrl });
} catch (err) {
console.error(err);
reply.code(500).send({ error: 'An error occurred', details: err.message });
}
});
// reply.send({ picture: publicUrl });
// } catch (err) {
// console.error(err);
// reply.code(500).send({ error: 'An error occurred', details: err.message });
// }
// });
// fastify.post("/api/uploads_installation_TeamMember_profile/:installationId/:teamMemberId", {
// preHandler: upload.single("file"),

Loading…
Cancel
Save