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

Returning Content-Type header - based on the S3 file Content-Type pro… #1

Merged
merged 1 commit into from
Jul 3, 2020

Conversation

ofir-shapira-como
Copy link

First, thanks for the fork.

This PR solves this issue aws-solutions#103, we want to have the correct Content-Type header returned to clients.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

const bufferImage = await modifiedImage.toBuffer();
return {
body: bufferImage.toString('base64'),
contentType: request.originalImageContentType
Copy link
Author

Choose a reason for hiding this comment

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

Splitting the response - we now return both body and contentType from the request.

@@ -46,9 +48,9 @@ class ImageRequest {
const request = s3.getObject(imageLocation).promise();
try {
const originalImage = await request;
return Promise.resolve(originalImage.Body);
return Promise.resolve({ body: originalImage.Body, contentType: originalImage.ContentType });
Copy link
Author

Choose a reason for hiding this comment

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

s3.getObject returns the ContentType (note the uppercase convention), we want to return in, with the body

@@ -26,7 +26,9 @@ class ImageRequest {
this.bucket = this.parseImageBucket(event, this.requestType);
this.key = this.parseImageKey(event, this.requestType);
this.edits = this.parseImageEdits(event, this.requestType);
this.originalImage = await this.getOriginalImage(this.bucket, this.key)
const originalImageWithContentType = await this.getOriginalImage(this.bucket, this.key)
Copy link
Author

Choose a reason for hiding this comment

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

We want to have a new property on the request - originalImageContentType

const response = {
"statusCode": 200,
"headers" : getResponseHeaders(),
"body": processedRequest,
"headers": getResponseHeaders(false, contentType),
Copy link
Author

Choose a reason for hiding this comment

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

We now use false to indicate there is no error, we use the contentType we got from the imageHandler

@@ -20,20 +20,19 @@ exports.handler = async (event) => {
const imageHandler = new ImageHandler();
try {
const request = await imageRequest.setup(event);
console.log(request);
const processedRequest = await imageHandler.process(request);
const { contentType, body } = await imageHandler.process(request);
Copy link
Author

Choose a reason for hiding this comment

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

get relevant data from handler

const corsEnabled = (process.env.CORS_ENABLED === "Yes");
const headers = {
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Allow-Credentials": true,
"Content-Type": "image"
"Content-Type": contentType
Copy link
Author

Choose a reason for hiding this comment

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

Returning S3 content type

@rpong
Copy link

rpong commented Jul 3, 2020

hi @ofir-shapira-como , sorry for overlooking this, just noticed this just. They have fixed the official project, so it best to use that one instead :) i am merging this anyway, we are still running this fork and your changes will benefit us.

@rpong rpong merged commit 55b1a12 into innovationlove:master Jul 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants