Skip to content

Commit

Permalink
Merge pull request #59 from bu-ist/check-prescaled
Browse files Browse the repository at this point in the history
Check for prescaled
  • Loading branch information
jdub233 authored Jul 24, 2024
2 parents bf8d263 + afaa5f1 commit 9497d66
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4.1.7
- uses: aws-actions/setup-sam@v2
- uses: actions/setup-node@v2
- uses: actions/setup-node@v4.0.3
with:
node-version: '18'
- uses: aws-actions/configure-aws-credentials@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4.1.7
- uses: actions/setup-node@v4.0.3
with:
node-version: '18'
- run: npm ci
Expand Down
11 changes: 9 additions & 2 deletions src/getOrCreateObject/getOrCreateObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,16 @@ async function getOrCreateObject(userRequest, domain) {
const originalKey = `${ORIGINAL_PATH_ROOT}/${domain}${originalPath}`;

const originalResponse = await tryGetObject(userRequest, originalKey);
// If there's no original image, then return the 404 response.
// If there's no original image, then do one more check for a "pre-scaled" original.
if (originalResponse.Code === 'NoSuchKey') {
return response;
// Run an extra check here to see if this is a "pre-scaled" image in the original media path
// where the original has the size in the name.
const prescaledOriginalKey = `${ORIGINAL_PATH_ROOT}/${domain}${decodedPathname}`;
const prescaledOriginalResponse = await tryGetObject(userRequest, prescaledOriginalKey);

// This was our last chance to find and image, so return the reponse
// whether it is a file or a 404
return prescaledOriginalResponse;
}
// If there is an original, resize the image data with sharp and save it for future requests.
const resized = await resizeAndSave(originalResponse, `/${domain}${originalPath}`, sizeMatch, crop);
Expand Down
20 changes: 20 additions & 0 deletions src/getOrCreateObject/getOrCreateObject.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ s3Mock.on(GetObjectCommand, {
Body: singlePixelJpgReadable,
});

// Define a pre-scaled object in the bucket,
// with a scale factor baked into the name of the original.
s3Mock.on(GetObjectCommand, {
Bucket: 'test-bucket',
Key: 'original_media/www.bu.edu/somesite/files/01/prescaled-758x460.jpg',
}).resolves({
Body: singlePixelJpgReadable,
});

describe('getOrCreateObject', () => {
it('should return an object if it exists', async () => {
const result = await getOrCreateObject(
Expand Down Expand Up @@ -89,4 +98,15 @@ describe('getOrCreateObject', () => {
);
expect(result.Code).toEqual('NoSuchKey');
});

it('should return a pre-scaled original object if it exists', async () => {
const result = await getOrCreateObject(
{
url: 'https://example-1111.s3-object-lambda.us-east-1.amazonaws.com/somesite/files/01/prescaled-758x460.jpg',
headers: { },
},
'www.bu.edu',
);
expect(result.Body).toBeDefined();
});
});

0 comments on commit 9497d66

Please sign in to comment.