From 46643a5757672085e7f14695b11602043dad77cb Mon Sep 17 00:00:00 2001 From: Susan Begley Date: Mon, 3 Feb 2020 18:35:50 -0500 Subject: [PATCH] Adding code changes from pull request: https://github.com/awslabs/serverless-image-handler/pull/158 --- source/image-handler/image-request.js | 14 +++++++++++++- source/image-handler/index.js | 10 +++++++--- source/image-handler/package.json | 8 ++++---- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/source/image-handler/image-request.js b/source/image-handler/image-request.js index 405ba6313..79a0aded4 100644 --- a/source/image-handler/image-request.js +++ b/source/image-handler/image-request.js @@ -46,11 +46,23 @@ class ImageRequest { const request = s3.getObject(imageLocation).promise(); try { const originalImage = await request; + if (originalImage.ContentType) { + this.ContentType = originalImage.ContentType; + } + if (originalImage.Expires) { + this.Expires = new Date(originalImage.Expires).toUTCString(); + } + if (originalImage.LastModified) { + this.LastModified = new Date(originalImage.LastModified).toUTCString(); + } + if (originalImage.CacheControl) { + this.CacheControl = originalImage.CacheControl; + } return Promise.resolve(originalImage.Body); } catch(err) { return Promise.reject({ - status: 500, + status: ("NoSuchKey" == err.code) ? 404 : 500, code: err.code, message: err.message }) diff --git a/source/image-handler/index.js b/source/image-handler/index.js index f6a9cba8a..277ae267a 100755 --- a/source/image-handler/index.js +++ b/source/image-handler/index.js @@ -22,9 +22,14 @@ exports.handler = async (event) => { const request = await imageRequest.setup(event); console.log(request); const processedRequest = await imageHandler.process(request); + const headers = getResponseHeaders(); + headers["Content-Type"] = request.ContentType; + headers["Expires"] = request.Expires; + headers["Last-Modified"] = request.LastModified; + headers["Cache-Control"] = request.CacheControl; const response = { "statusCode": 200, - "headers" : getResponseHeaders(), + "headers" : headers, "body": processedRequest, "isBase64Encoded": true } @@ -51,8 +56,7 @@ const getResponseHeaders = (isErr) => { const headers = { "Access-Control-Allow-Methods": "GET", "Access-Control-Allow-Headers": "Content-Type, Authorization", - "Access-Control-Allow-Credentials": true, - "Content-Type": "image" + "Access-Control-Allow-Credentials": true } if (corsEnabled) { headers["Access-Control-Allow-Origin"] = process.env.CORS_ORIGIN; diff --git a/source/image-handler/package.json b/source/image-handler/package.json index e1a5d0028..0dda49e5d 100644 --- a/source/image-handler/package.json +++ b/source/image-handler/package.json @@ -9,9 +9,9 @@ "private": true, "dependencies": { "mocha": "^6.1.4", - "sharp": "^0.23.3", - "sinon": "^7.3.2", - "nyc": "^14.0.0" + "nyc": "^14.0.0", + "sharp": "^0.23.4", + "sinon": "^7.3.2" }, "devDependencies": { "aws-sdk": "^2.437.0", @@ -23,7 +23,7 @@ "build:init": "rm -rf package-lock.json && rm -rf dist && rm -rf node_modules", "build:zip": "zip -rq image-handler.zip . -x template.yml", "build:dist": "mkdir dist && mv image-handler.zip dist/", - "build": "npm run build:init && npm install --production && npm run build:zip && npm run build:dist" + "build": "npm run build:init && npm install --arch=x64 --platform=linux --production && npm run build:zip && npm run build:dist" }, "license": "Apache-2.0" }