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

Return 404 for non-existent file #183

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 50 additions & 32 deletions src/lambda-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,61 @@ const streamify_handler: StreamifyHandler = async ( event, response ) => {
delete args.presign;
}

let s3_response = await getS3File( config, key, args );
if ( ! s3_response.Body ) {
throw new Error( 'No body in file.' );
}
let buffer = Buffer.from( await s3_response.Body.transformToByteArray() );
try {
jerico marked this conversation as resolved.
Show resolved Hide resolved
let s3_response = await getS3File( config, key, args );
if ( ! s3_response.Body ) {
throw new Error( 'No body in file.' );
}
let buffer = Buffer.from( await s3_response.Body.transformToByteArray() );

let { info, data } = await resizeBuffer( buffer, args );
// If this is a signed URL, we need to calculate the max-age of the image.
let maxAge = 31536000;
if ( args['X-Amz-Expires'] ) {
// Date format of X-Amz-Date is YYYYMMDDTHHMMSSZ, which is not parsable by Date.
const dateString = args['X-Amz-Date']!.replace(
/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/,
'$1-$2-$3T$4:$5:$6Z'
);
const date = new Date( dateString );
let { info, data } = await resizeBuffer( buffer, args );
// If this is a signed URL, we need to calculate the max-age of the image.
let maxAge = 31536000;
if ( args['X-Amz-Expires'] ) {
// Date format of X-Amz-Date is YYYYMMDDTHHMMSSZ, which is not parsable by Date.
const dateString = args['X-Amz-Date']!.replace(
/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/,
'$1-$2-$3T$4:$5:$6Z'
);
const date = new Date( dateString );

// Calculate when the signed URL will expire, as we'll set the max-age
// cache control to this value.
const expires = date.getTime() / 1000 + Number( args['X-Amz-Expires'] );
// Calculate when the signed URL will expire, as we'll set the max-age
// cache control to this value.
const expires = date.getTime() / 1000 + Number( args['X-Amz-Expires'] );

// Mage age is the date the URL expires minus the current time.
maxAge = Math.round( expires - new Date().getTime() / 1000 ); // eslint-disable-line no-unused-vars
}
// Mage age is the date the URL expires minus the current time.
maxAge = Math.round( expires - new Date().getTime() / 1000 ); // eslint-disable-line no-unused-vars
}

// Somewhat undocumented API on how to pass headers to a stream response.
response = awslambda.HttpResponseStream.from( response, {
statusCode: 200,
headers: {
'Cache-Control': `max-age=${ maxAge }`,
'Last-Modified': ( new Date() ).toUTCString(),
'Content-Type': 'image/' + info.format,
},
} );
// Somewhat undocumented API on how to pass headers to a stream response.
response = awslambda.HttpResponseStream.from( response, {
statusCode: 200,
headers: {
'Cache-Control': `max-age=${ maxAge }`,
'Last-Modified': ( new Date() ).toUTCString(),
'Content-Type': 'image/' + info.format,
},
} );

response.write( data );
response.end();
response.write( data );
response.end();
} catch ( e: any ) {
// An AccessDenied error means the file is either protected, or doesn't exist.
if ( e.Code === 'AccessDenied' ) {
const metadata = {
statusCode: 404,
headers: {
'Content-Type': 'text/html',
'Cache-Control': 'no-cache',
jerico marked this conversation as resolved.
Show resolved Hide resolved
},
};
response = awslambda.HttpResponseStream.from( response, metadata );
response.write( 'File not found.' );
response.end();
} else {
throw e;
}
}
};

if ( typeof awslambda === 'undefined' ) {
Expand Down
6 changes: 2 additions & 4 deletions tests/test-lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ test( 'Test content type headers', async () => {
expect( testResponseStream.contentType ).toBe( 'image/gif' );
} );

// Currently the handler will throw an error if the file is not found, rather than correctly
// return a status code and message.
test.failing( 'Test image not found', async () => {
test( 'Test image not found', async () => {
const testResponseStream = new TestResponseStream();
animatedGifLambdaEvent.rawPath = '/tachyon/does-not-exist.gif';

await handler( animatedGifLambdaEvent, testResponseStream );

expect( testResponseStream.contentType ).toBe( 'image/gif' );
jerico marked this conversation as resolved.
Show resolved Hide resolved
expect( testResponseStream.metadata.statusCode ).toBe( 404 );
} );

/**
Expand Down
Loading