diff --git a/src/index.js b/src/index.js index 6a3e9987..d2573000 100644 --- a/src/index.js +++ b/src/index.js @@ -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( "/api/uploads-manualTestVideo-work/:customerId/:installationId", - { preHandler: upload.any() }, // Multer handles multiple files + { preHandler: upload.any() }, // Multer saves files to "uploads/" async (request, reply) => { try { 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) { return reply.code(400).send({ error: "No files uploaded" }); @@ -1385,17 +1460,19 @@ fastify.post( .substring(7)}-${file.originalname}`; 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 stream = gcsFile.createWriteStream({ - metadata: { contentType: file.mimetype }, // Keep correct video type + metadata: { + contentType: file.mimetype || "video/mp4", + contentDisposition: "inline", + }, resumable: false, }); - // Write buffer to GCS stream - stream.end(file.buffer); + // Pipe from disk to GCS + fs.createReadStream(file.path).pipe(stream); await new Promise((resolve, reject) => { stream.on("finish", async () => { @@ -1414,9 +1491,14 @@ fastify.post( 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( { customerId, installationId }, { $push: { pictureUrl: { $each: publicUrls.map((url) => ({ url })) } } },