Skip to content

Commit

Permalink
Merge pull request #102 from nulib/deploy/staging
Browse files Browse the repository at this point in the history
Deploy Staging to Production
  • Loading branch information
kdid authored Feb 6, 2023
2 parents 77a8795 + 555450c commit 65186db
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 79 deletions.
28 changes: 27 additions & 1 deletion docs/docs/spec/data-types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,24 @@ components:
- keywords
- modified_date
- published
- representative_image
- thumbnail
- title
- visibility
CollectionRepresentativeImage:
type: object
nullable: true
properties:
work_id:
type: string
nullable: true
format: uuid
url:
type: string
format: uri
required:
- work_id
- url
- work_id
ControlledTerm:
type: object
properties:
Expand Down Expand Up @@ -190,13 +193,15 @@ components:
format: date-time
digests:
type: object
nullable: true
additionalProperties:
type: string
description:
type: string
nullable: true
extracted_metadata:
type: object
nullable: true
additional_properties: true
indexed_at:
type: string
Expand All @@ -218,6 +223,8 @@ components:
- api_model
- create_date
- description
- digests
- extracted_metadata
- indexed_at
- modified_date
- published
Expand All @@ -234,6 +241,7 @@ components:
- Supplemental
GenericIdLabel:
type: object
nullable: true
properties:
id:
type: string
Expand Down Expand Up @@ -319,6 +327,7 @@ components:
- Resource Guide
RepresentativeFileSet:
type: object
nullable: true
description: Information about the representative image for the resource
properties:
aspect_ratio:
Expand Down Expand Up @@ -412,6 +421,7 @@ components:
type: string
collection:
type: object
nullable: true
description: The parent collection of the resource
properties:
id:
Expand Down Expand Up @@ -503,6 +513,7 @@ components:
desciption: Date/time of last index
ingest_project:
type: object
nullable: true
description: Associated ingest project
properties:
id:
Expand All @@ -515,6 +526,7 @@ components:
- title
ingest_sheet:
type: object
nullable: true
description: Associated ingest sheet
properties:
id:
Expand Down Expand Up @@ -580,6 +592,7 @@ components:
$ref: "#/components/schemas/PreservationLevel"
project:
type: object
nullable: true
description: Project related information
properties:
desc:
Expand All @@ -600,6 +613,13 @@ components:
task_number:
type: string
nullable: true
required:
- desc
- cycle
- manager
- name
- proposer
- task_number
provenance:
type: array
description: Location of Physical Object // will also include messy dates. Information about the provenance, such as origin, ownership and custodial history (chain of custody), of a resource.
Expand Down Expand Up @@ -703,6 +723,7 @@ components:
- box_number
- caption
- catalog_key
- collection
- contributor
- create_date
- creator
Expand All @@ -718,10 +739,13 @@ components:
- identifier
- iiif_manifest
- indexed_at
- ingest_project
- ingest_sheet
- keywords
- language
- legacy_identifier
- library_unit
- license
- location
- modified_date
- notes
Expand All @@ -734,7 +758,9 @@ components:
- publisher
- related_material
- related_url
- representative_file_set
- rights_holder
- rights_statement
- scope_and_contents
- series
- source
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dc-api-build",
"version": "2.0.0-rc.1",
"version": "2.0.0-rc.4",
"description": "NUL Digital Collections API Build Environment",
"repository": "https://github.com/nulib/dc-api-v2",
"author": "nulib",
Expand Down
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 65186db

Please sign in to comment.