Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use HTTP 404 & forward Cache-Control, Content-Type, Expires, and Last-Modified headers from S3. #158

Closed
wants to merge 9 commits into from
16 changes: 14 additions & 2 deletions source/image-handler/image-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down Expand Up @@ -235,4 +247,4 @@ class ImageRequest {
}

// Exports
module.exports = ImageRequest;
module.exports = ImageRequest;
12 changes: 8 additions & 4 deletions source/image-handler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if you are changing the image from a jpg to a png.

If i put
'toFormat' => 'png'
in my image settings but the source is a jpeg this will output image/jpg

headers["Expires"] = request.Expires;
headers["Last-Modified"] = request.LastModified;
headers["Cache-Control"] = request.CacheControl;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to supply overridden values? In the base64 encoded string
{
"header": [{'Expires' : x }]
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, @bretto36. I'm not using either of those features (changing formats or base64), but I'll take a look when I get a chance.

const response = {
"statusCode": 200,
"headers" : getResponseHeaders(),
"headers" : headers,
"body": processedRequest,
"isBase64Encoded": true
}
Expand All @@ -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;
Expand All @@ -61,4 +65,4 @@ const getResponseHeaders = (isErr) => {
headers["Content-Type"] = "application/json"
}
return headers;
}
}