diff --git a/node/src/handlers/get-file-set-download.js b/node/src/handlers/get-file-set-download.js index 15d8d649..aed33ef7 100644 --- a/node/src/handlers/get-file-set-download.js +++ b/node/src/handlers/get-file-set-download.js @@ -2,6 +2,8 @@ const { SFNClient, StartExecutionCommand } = require("@aws-sdk/client-sfn"); const { wrap } = require("./middleware"); const { getFileSet } = require("../api/opensearch"); const { videoTranscodeSettings } = require("./transcode-templates"); +const { getSignedUrl } = require("@aws-sdk/s3-request-presigner"); +const { S3Client, GetObjectCommand } = require("@aws-sdk/client-s3"); const opensearchResponse = require("../api/response/opensearch"); const path = require("path"); @@ -26,8 +28,16 @@ exports.handler = wrap(async (event) => { if (esResponse.statusCode == "200") { const doc = JSON.parse(esResponse.body); - if (downloadAvailable(doc)) { + if (isVideoDownload(doc)) { return await processDownload(doc, email); + } else if (isPDFDownload(doc)) { + const url = await getDownloadLink(doc) + return { + statusCode: 302, + headers: { + 'Location': url + } + } } else { return invalidRequest( 405, @@ -39,7 +49,16 @@ exports.handler = wrap(async (event) => { } }); -function downloadAvailable(doc) { +function isPDFDownload(doc) { + return ( + doc.found && + doc._source.role === "Auxiliary" && + doc._source.mime_type != null && + doc._source.mime_type === "application/pdf" + ); +} + +function isVideoDownload(doc) { // Note - audio is not currently implemented due to an issue with AWS // & MediaConvert and our .m3u8 files return ( @@ -51,6 +70,23 @@ function downloadAvailable(doc) { ); } +async function getDownloadLink(doc) { + const clientParams = {} + const bucket = "" + const key = "" + + const getObjectParams = { + Bucket: bucket, + Key: key, + ResponseContentDisposition: `attachment; filename=${doc._source.label}`, + }; + + const client = new S3Client(clientParams); + const command = new GetObjectCommand(getObjectParams); + const url = await getSignedUrl(client, command, { expiresIn: 3600 * 24 * 3 }); // 3 days + return url; +} + async function processDownload(doc, email) { const stepFunctionConfig = process.env.STEP_FUNCTION_ENDPOINT ? { endpoint: process.env.STEP_FUNCTION_ENDPOINT }