Skip to content

Commit

Permalink
Use mime-types for extension in download
Browse files Browse the repository at this point in the history
  • Loading branch information
kdid committed May 2, 2024
1 parent c12b649 commit 8cdadca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
8 changes: 8 additions & 0 deletions docs/docs/spec/data-types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ components:
description:
type: string
nullable: true
download_url:
type: string
nullable: true
duration:
type: number
nullable: true
Expand Down Expand Up @@ -172,6 +175,7 @@ components:
- id
- accession_number
- description
- download_url
- duration
- height
- label
Expand Down Expand Up @@ -489,6 +493,9 @@ components:
description: An account of the resource.
items:
type: string
download_url:
type: string
nullable: true
file_sets:
type: array
description: File sets associated with the resource.
Expand Down Expand Up @@ -749,6 +756,7 @@ components:
- cultural_context
- date_created
- description
- download_url
- file_sets
- folder_name
- folder_number
Expand Down
18 changes: 5 additions & 13 deletions node/src/handlers/get-file-set-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { apiTokenName } = require("../environment");
const ApiToken = require("../api/api-token");
const axios = require("axios").default;
const cookie = require("cookie");

const mime = require("mime-types");
const opensearchResponse = require("../api/response/opensearch");
const path = require("path");

Expand All @@ -18,9 +18,6 @@ const path = require("path");
exports.handler = wrap(async (event) => {
const id = event.pathParameters.id;
const email = event.queryStringParameters?.email;
if (!email) {
return invalidRequest(400, "Query string must include email address");
}

const allowPrivate =
event.userToken.isSuperUser() ||
Expand All @@ -36,6 +33,9 @@ exports.handler = wrap(async (event) => {
if (esResponse.statusCode == "200") {
const doc = JSON.parse(esResponse.body);
if (isVideoDownload(doc)) {
if (!email) {
return invalidRequest(400, "Query string must include email address");
}
if (!event.userToken.isSuperUser()) {
return invalidRequest(401, "Unauthorized");
}
Expand Down Expand Up @@ -104,14 +104,6 @@ function derivativeKey(doc) {
return "derivatives/" + prefix + "/" + id;
}

function extensionFromMimeType(mimeType) {
const parts = mimeType.split("/");
if (parts.length > 1) {
return parts[1];
}
return "";
}

async function getDownloadLink(doc) {
const clientParams = {};
const bucket = process.env.PYRAMID_BUCKET;
Expand All @@ -122,7 +114,7 @@ async function getDownloadLink(doc) {
Key: key,
ResponseContentDisposition: `attachment; filename=${
doc._source.label
}.${extensionFromMimeType(doc._source.mime_type)}`,
}.${mime.extension(doc._source.mime_type)}`,
};

const client = new S3Client(clientParams);
Expand Down

0 comments on commit 8cdadca

Please sign in to comment.