From 7e0f7b0b310aab7ce0886f03522807727ce7f8ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20de=20Vasconcelos?= Date: Tue, 7 Nov 2023 21:50:08 +0000 Subject: [PATCH] Update download.endpoint.js --- server/endpoints/download.endpoint.js | 32 +++++++++------------------ 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/server/endpoints/download.endpoint.js b/server/endpoints/download.endpoint.js index fc75752..12d77b5 100644 --- a/server/endpoints/download.endpoint.js +++ b/server/endpoints/download.endpoint.js @@ -1,9 +1,14 @@ /* * */ +const fs = require('fs'); const QUEUEDB = require('../services/QUEUEDB'); /* * */ +const OUTPUT_DIRECTORY = '/app/jobsdata/pdfs'; + +/* * */ + module.exports = async (request, reply) => { // @@ -13,28 +18,11 @@ module.exports = async (request, reply) => { // If no job is found with this id return 404 Not Found if (!foundRequestedJob) return reply.code(404).send({}); - // Simplify the requested job object - const foundRequestedJobSimplified = { ...foundRequestedJob, owner_name: undefined, owner_email: undefined, gdpr_consent: undefined }; - - // If job is already expired (the file was deleted) return 410 Gone - if (foundRequestedJob.status === 'expired') return reply.code(410).send(foundRequestedJobSimplified); - - // If job is already done (the job can no longe be updated) return 403 Forbidden - if (foundRequestedJob.status === 'ready' || foundRequestedJob.status === 'done') return reply.code(403).send(foundRequestedJobSimplified); - - // Update the job with the new details - await QUEUEDB.Job.updateOne( - { _id: QUEUEDB.toObjectId(request.params.id) }, - { - $set: { - status: 'downloaded', - download_count: foundRequestedJob.download_count++, - date_downloaded: [...foundRequestedJob.date_downloaded, new Date().toISOString()], - }, - } - ); - - // Redirect client to the file? + // Read the file to the client + reply.header('Content-Type', 'application/pdf'); + reply.header('Content-Disposition', `attachment; filename=${foundRequestedJob._id}`); + const fileStream = fs.createReadStream(`${OUTPUT_DIRECTORY}/${foundRequestedJob._id}.pdf`); + return reply.send(fileStream); // };