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 760a2f5
Show file tree
Hide file tree
Showing 3 changed files with 19 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
19 changes: 16 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,21 @@ 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 fileType = event.queryStringParameters.type;

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

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: fileType,
});

return {
Expand Down

0 comments on commit 760a2f5

Please sign in to comment.