Skip to content

Commit

Permalink
Merge pull request #101 from nulib/3456-collection-rep-image
Browse files Browse the repository at this point in the history
Refactor getThumbnail endpoint to support new indexed collection representative_image data shape
  • Loading branch information
bmquinn authored Feb 3, 2023
2 parents 376e7d4 + d1610db commit 555450c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 77 deletions.
63 changes: 24 additions & 39 deletions src/handlers/get-thumbnail.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,33 @@ function validateRequest(event) {
return { id, aspect, size };
}

const getWorkThumbnail = async (id, aspect, size, event) => {
const getThumbnail = async (id, aspect, size, event) => {
const allowUnpublished = event.userToken.hasEntitlement(id);
const allowPrivate = allowUnpublished || event.userToken.isReadingRoom();

const esResponse = await getWork(id, {
allowPrivate,
allowUnpublished,
});

if (esResponse.statusCode != 200) {
return opensearchResponse.transform(esResponse);
let esResponse;
let body;
let iiif_base;
if (event.rawPath.match(/\/collections\//)) {
esResponse = await getCollection(id, {
allowPrivate,
allowUnpublished,
});
if (esResponse.statusCode != 200)
return { error: await opensearchResponse.transform(esResponse) };
body = JSON.parse(esResponse.body);
iiif_base = body?._source?.representative_image?.url;
} else {
esResponse = await getWork(id, {
allowPrivate,
allowUnpublished,
});
if (esResponse.statusCode != 200)
return { error: await opensearchResponse.transform(esResponse) };
body = JSON.parse(esResponse.body);
iiif_base = body?._source?.representative_file_set?.url;
}

const body = JSON.parse(esResponse.body);
const iiif_base = body?._source?.representative_file_set?.url;

if (!iiif_base) {
return {
statusCode: 404,
Expand Down Expand Up @@ -89,39 +100,13 @@ const getWorkThumbnail = async (id, aspect, size, event) => {
};
};

const getParameters = async (event) => {
const { id, aspect, size } = validateRequest(event);
if (event.rawPath.match(/\/collections\//)) {
const esResponse = await getCollection(id);
if (esResponse.statusCode != 200) {
return { error: await opensearchResponse.transform(esResponse) };
}

const body = JSON.parse(esResponse.body);
const workId = body?._source?.representative_image?.work_id;
return { id: workId, aspect, size };
} else {
return { id, aspect, size };
}
};

/**
* A simple function to proxy a Collection or Work thumbnail from the IIIF server
*/
exports.handler = wrap(async (event) => {
try {
const { id, aspect, size, error } = await getParameters(event);
if (error) {
return error;
} else if (!id) {
return {
statusCode: 404,
headers: { "content-type": "text/plain" },
body: "Not Found",
};
} else {
return await getWorkThumbnail(id, aspect, size, event);
}
const { id, aspect, size } = validateRequest(event);
return await getThumbnail(id, aspect, size, event);
} catch (err) {
return {
statusCode: 400,
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/mocks/collection-1234-private-published.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"published": true,
"visibility": "Private",
"representative_image": {
"work_id": "1234"
"work_id": "1234",
"url": "https://index.test.library.northwestern.edu/iiif/2/mbk-dev/5678"
}
}
}
3 changes: 2 additions & 1 deletion test/fixtures/mocks/collection-1234.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"api_model": "Collection",
"published": true,
"representative_image": {
"work_id": "1234"
"work_id": "1234",
"url": "https://index.test.library.northwestern.edu/iiif/2/mbk-dev/5678"
}
}
}
40 changes: 4 additions & 36 deletions test/integration/get-thumbnail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ describe("Thumbnail routes", () => {
mock
.get("/dc-v2-collection/_doc/1234")
.reply(200, helpers.testFixture("mocks/collection-1234.json"));
mock
.get("/dc-v2-work/_doc/1234")
.reply(200, helpers.testFixture("mocks/work-1234.json"));
mock
.get("/iiif/2/mbk-dev/5678/full/!300,300/0/default.jpg")
.reply(200, helpers.testFixture("mocks/thumbnail_full.jpg"), {
Expand All @@ -50,9 +47,6 @@ describe("Thumbnail routes", () => {
mock
.get("/dc-v2-collection/_doc/1234")
.reply(200, helpers.testFixture("mocks/collection-1234.json"));
mock
.get("/dc-v2-work/_doc/1234")
.reply(200, helpers.testFixture("mocks/work-1234.json"));
mock
.get("/iiif/2/mbk-dev/5678/full/!300,300/0/default.jpg")
.reply(403, "Forbidden", { "Content-Type": "text/plain" });
Expand All @@ -69,20 +63,7 @@ describe("Thumbnail routes", () => {
.reply(200, helpers.testFixture("mocks/missing-collection-1234.json"));

const result = await handler(event.render());
expect(result.statusCode).to.eq(404);
expectCorsHeaders(result);
});

it("returns 404 if the work doc can't be found", async () => {
mock
.get("/dc-v2-collection/_doc/1234")
.reply(200, helpers.testFixture("mocks/collection-1234.json"));
mock
.get("/dc-v2-work/_doc/1234")
.reply(200, helpers.testFixture("mocks/missing-work-1234.json"));

const result = await handler(event.render());
expect(result.statusCode).to.eq(404);
expect(result.error.statusCode).to.eq(404);
expectCorsHeaders(result);
});

Expand All @@ -98,19 +79,6 @@ describe("Thumbnail routes", () => {
expect(result.statusCode).to.eq(404);
expectCorsHeaders(result);
});

it("returns 404 if the work doc has no thumbnail", async () => {
mock
.get("/dc-v2-collection/_doc/1234")
.reply(200, helpers.testFixture("mocks/collection-1234.json"));
mock
.get("/dc-v2-work/_doc/1234")
.reply(200, helpers.testFixture("mocks/work-1234-no-thumbnail.json"));

const result = await handler(event.render());
expect(result.statusCode).to.eq(404);
expectCorsHeaders(result);
});
});

describe("Work", () => {
Expand Down Expand Up @@ -155,7 +123,7 @@ describe("Thumbnail routes", () => {
.reply(200, helpers.testFixture("mocks/missing-work-1234.json"));

const result = await handler(event.render());
expect(result.statusCode).to.eq(404);
expect(result.error.statusCode).to.eq(404);
expectCorsHeaders(result);
});

Expand All @@ -175,7 +143,7 @@ describe("Thumbnail routes", () => {
.reply(200, helpers.testFixture("mocks/private-work-1234.json"));

const result = await handler(event.render());
expect(result.statusCode).to.eq(404);
expect(result.error.statusCode).to.eq(404);
});

it("returns 200 if the work is private and the user is in the reading room", async () => {
Expand All @@ -200,7 +168,7 @@ describe("Thumbnail routes", () => {
.reply(200, helpers.testFixture("mocks/unpublished-work-1234.json"));

const result = await handler(event.render());
expect(result.statusCode).to.eq(404);
expect(result.error.statusCode).to.eq(404);
});

it("returns 200 if there is an entitlement for an unpublished, private work", async () => {
Expand Down

0 comments on commit 555450c

Please sign in to comment.