Skip to content

Commit

Permalink
fix: content type
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeny Generalov authored and Evgeny Generalov committed Nov 14, 2020
1 parent e9b0492 commit ae650d1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/import-service/package.json
Expand Up @@ -23,6 +23,7 @@
"license": "MIT",
"dependencies": {
"aws-sdk": "^2.792.0",
"csv-parser": "^2.3.3"
"csv-parser": "^2.3.3",
"http-errors": "^1.8.0"
}
}
1 change: 1 addition & 0 deletions packages/import-service/serverless.yml
Expand Up @@ -49,6 +49,7 @@ functions:
parameters:
querystrings:
name: true
type: true

importFileParser:
handler: src/handlers/importFileParser.handler
Expand Down
21 changes: 18 additions & 3 deletions packages/import-service/src/handlers/importProductsFile.js
@@ -1,10 +1,16 @@
import AWS from "aws-sdk";
import httpError from "http-errors";
import middy from "@middy/core";
import middyHttpCors from "@middy/http-cors";
import middyErrorHandler from "middy-error-handler";
import middyRequestLogger from "middy-request-logger";

const { IMPORT_S3_BUCKET, IMPORT_S3_PREFIX, IMPORT_S3_REGION } = process.env;
const ALLOWED_CONTENT_TYPES = [
"text/csv",
"application/vnd.ms-excel",
"text/x-csv",
];

export const handler = middy(importProductsFile).use([
middyErrorHandler(),
Expand All @@ -14,14 +20,23 @@ export const handler = middy(importProductsFile).use([

export async function importProductsFile(event, context, callback) {
const fileName = event.queryStringParameters.name;
const catalogPath = [IMPORT_S3_PREFIX, fileName].join("");
const fileMimeType = event.queryStringParameters.type;

if (
!ALLOWED_CONTENT_TYPES.find((allowedType) =>
fileMimeType.includes(allowedType)
)
) {
throw new httpError.BadRequest(`Unsupported file type ${fileMimeType}`);
}

const s3 = new AWS.S3({ region: IMPORT_S3_REGION });
const uploadPath = [IMPORT_S3_PREFIX, fileName].join("");
const url = await s3.getSignedUrlPromise("putObject", {
Bucket: IMPORT_S3_BUCKET,
Key: catalogPath,
Key: uploadPath,
Expires: 60,
ContentType: "text/csv",
ContentType: fileMimeType,
});

return {
Expand Down

0 comments on commit ae650d1

Please sign in to comment.