Skip to content

Commit

Permalink
Auxiliary & Access file download
Browse files Browse the repository at this point in the history
  • Loading branch information
kdid committed Apr 29, 2024
1 parent 8c0c583 commit dd24398
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions node/src/handlers/get-file-set-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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,
Expand All @@ -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 (
Expand All @@ -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 }
Expand Down

0 comments on commit dd24398

Please sign in to comment.