video upload

master^2
Bhaskar 3 weeks ago
parent 835e37302f
commit 06a933c92d

@ -1364,13 +1364,88 @@ fastify.post(
// } // }
// }); // });
// fastify.post(
// "/api/uploads-manualTestVideo-work/:customerId/:installationId",
// { preHandler: upload.any() },
// async (request, reply) => {
// try {
// const { customerId, installationId } = request.params;
// const files = request.files;
// if (!files || files.length === 0) {
// return reply.code(400).send({ error: "No files uploaded" });
// }
// const bucketName = "arminta_profile_pictures";
// const publicUrls = [];
// for (const file of files) {
// const uniqueFileName = `${Date.now()}-${Math.random()
// .toString(36)
// .substring(7)}-${file.originalname}`;
// const filePath = `manual_test_video/${uniqueFileName}`;
// console.log(`Uploading video: ${file.originalname} → ${filePath}`);
// const gcsFile = storage.bucket(bucketName).file(filePath);
// // Use a proper write stream with full metadata
// const stream = gcsFile.createWriteStream({
// metadata: {
// contentType: file.mimetype || "video/mp4", // fallback to mp4
// contentDisposition: "inline", // ensures browser plays instead of downloads
// },
// resumable: false,
// });
// stream.write(file.buffer);
// stream.end();
// await new Promise((resolve, reject) => {
// stream.on("finish", async () => {
// try {
// await gcsFile.makePublic();
// const publicUrl = `https://storage.googleapis.com/${bucketName}/${filePath}`;
// publicUrls.push(publicUrl);
// console.log(`✅ Uploaded: ${publicUrl}`);
// resolve();
// } catch (err) {
// reject(err);
// }
// });
// stream.on("error", (err) => {
// console.error("Upload Error:", err);
// reject(err);
// });
// });
// }
// const updatedRecord = await ManualTestVideo.findOneAndUpdate(
// { customerId, installationId },
// { $push: { videoUrl: { $each: publicUrls.map((url) => ({ url })) } } }, // use videoUrl field
// { new: true, upsert: true }
// );
// return reply.send({
// success: true,
// videos: publicUrls,
// details: updatedRecord,
// });
// } catch (err) {
// console.error("Upload Error:", err);
// return reply.code(500).send({ error: "Upload failed", details: err.message });
// }
// }
// );
fastify.post( fastify.post(
"/api/uploads-manualTestVideo-work/:customerId/:installationId", "/api/uploads-manualTestVideo-work/:customerId/:installationId",
{ preHandler: upload.any() }, // Multer handles multiple files { preHandler: upload.any() }, // Multer saves files to "uploads/"
async (request, reply) => { async (request, reply) => {
try { try {
const { customerId, installationId } = request.params; const { customerId, installationId } = request.params;
const files = request.files; // Multer stores files here const files = request.files; // Multer saves file info here
if (!files || files.length === 0) { if (!files || files.length === 0) {
return reply.code(400).send({ error: "No files uploaded" }); return reply.code(400).send({ error: "No files uploaded" });
@ -1385,17 +1460,19 @@ fastify.post(
.substring(7)}-${file.originalname}`; .substring(7)}-${file.originalname}`;
const filePath = `manual_test_video/${uniqueFileName}`; const filePath = `manual_test_video/${uniqueFileName}`;
console.log(`Uploading video: ${file.originalname}${filePath}`); console.log(`Uploading video: ${file.path}${filePath}`);
// Create a write stream for large video files
const gcsFile = storage.bucket(bucketName).file(filePath); const gcsFile = storage.bucket(bucketName).file(filePath);
const stream = gcsFile.createWriteStream({ const stream = gcsFile.createWriteStream({
metadata: { contentType: file.mimetype }, // Keep correct video type metadata: {
contentType: file.mimetype || "video/mp4",
contentDisposition: "inline",
},
resumable: false, resumable: false,
}); });
// Write buffer to GCS stream // Pipe from disk to GCS
stream.end(file.buffer); fs.createReadStream(file.path).pipe(stream);
await new Promise((resolve, reject) => { await new Promise((resolve, reject) => {
stream.on("finish", async () => { stream.on("finish", async () => {
@ -1414,9 +1491,14 @@ fastify.post(
reject(err); reject(err);
}); });
}); });
// optional: cleanup temp file
fs.unlink(file.path, (err) => {
if (err) console.error("Temp file cleanup failed:", err);
});
} }
// Update MongoDB: store array of { url: "..." } // Update MongoDB
const updatedRecord = await ManualTestVideo.findOneAndUpdate( const updatedRecord = await ManualTestVideo.findOneAndUpdate(
{ customerId, installationId }, { customerId, installationId },
{ $push: { pictureUrl: { $each: publicUrls.map((url) => ({ url })) } } }, { $push: { pictureUrl: { $each: publicUrls.map((url) => ({ url })) } } },

Loading…
Cancel
Save