Skip to content

Commit

Permalink
Merge pull request #313 from nasa/CMR-9207
Browse files Browse the repository at this point in the history
CMR-9207: Added legacy labeling to asset thumbnails
  • Loading branch information
dpesall authored Jul 20, 2023
2 parents 8fa389f + 297013f commit 364960b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 5 deletions.
3 changes: 2 additions & 1 deletion resources/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,8 @@
"thumbnail": {
"title": "Thumbnail",
"href": "http://cool-sat.com/catalog/collections/cs/items/CS3-20160503_132130_04/thumb.png",
"type": "image/png"
"type": "image/png",
"key": "http://cool-sat.com/catalog/collections/cs/items/CS3-20160503_132130_04/thumb.png"
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions src/domains/__tests__/items.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ describe("granuleToStac", () => {
title: "Provider Metadata",
roles: ["metadata"],
},
thumbnail_0: {
href: "ftp://e4ftl01.cr.usgs.gov/MODIS_Composites/MOTA/MCD43A4.005/2009.09.14/MCD43A4.A2009257.h29v03.005.2009276045828.vinr.img",
title: "Thumbnail [0]",
description: "Browse image for Earthdata Search",
vinr: "ftp://e4ftl01.cr.usgs.gov/MODIS_Composites/MOTA/MCD43A4.005/2009.09.14/MCD43A4.A2009257.h29v03.005.2009276045828.vinr.img",
roles: ["thumbnail"],
},
thumbnail_1: {
href: "ftp://e4ftl012/ExampleBadUrl",
title: "Thumbnail [1]",
description: "Browse image for Earthdata Search",
key: "ftp://e4ftl012/ExampleBadUrl",
roles: ["thumbnail"],
},
},
});
});
Expand Down Expand Up @@ -133,6 +147,20 @@ describe("granuleToStac", () => {
title: "Provider Metadata",
roles: ["metadata"],
},
thumbnail_0: {
href: "ftp://e4ftl01.cr.usgs.gov/MODIS_Composites/MOTA/MCD43A4.005/2009.09.14/MCD43A4.A2009257.h29v03.005.2009276045828.vinr.img",
title: "Thumbnail [0]",
description: "Browse image for Earthdata Search",
vinr: "ftp://e4ftl01.cr.usgs.gov/MODIS_Composites/MOTA/MCD43A4.005/2009.09.14/MCD43A4.A2009257.h29v03.005.2009276045828.vinr.img",
roles: ["thumbnail"],
},
thumbnail_1: {
href: "ftp://e4ftl012/ExampleBadUrl",
title: "Thumbnail [1]",
description: "Browse image for Earthdata Search",
key: "ftp://e4ftl012/ExampleBadUrl",
roles: ["thumbnail"],
},
},
});
});
Expand Down Expand Up @@ -192,6 +220,20 @@ describe("granuleToStac", () => {
title: "Provider Metadata",
roles: ["metadata"],
},
thumbnail_0: {
href: "ftp://e4ftl01.cr.usgs.gov/MODIS_Composites/MOTA/MCD43A4.005/2009.09.14/MCD43A4.A2009257.h29v03.005.2009276045828.vinr.img",
title: "Thumbnail [0]",
description: "Browse image for Earthdata Search",
vinr: "ftp://e4ftl01.cr.usgs.gov/MODIS_Composites/MOTA/MCD43A4.005/2009.09.14/MCD43A4.A2009257.h29v03.005.2009276045828.vinr.img",
roles: ["thumbnail"],
},
thumbnail_1: {
href: "ftp://e4ftl012/ExampleBadUrl",
title: "Thumbnail [1]",
description: "Browse image for Earthdata Search",
key: "ftp://e4ftl012/ExampleBadUrl",
roles: ["thumbnail"],
},
},
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/domains/stac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
flattenTree,
mergeMaybe,
buildClientId,
extractAssetMapKey,
scrubTokens,
generatePossibleCollectionIds,
} from "../utils";
Expand Down Expand Up @@ -78,21 +79,20 @@ const metadataAssets = (concept: Collection | Granule) => {
* Return thumbnail assets if present.
*/
const thumbnailAssets = (concept: Collection | Granule) => {
const thumbnailTypes = [
RelatedUrlType.GET_RELATED_VISUALIZATION /* , RelatedUrlType.THUMBNAIL */,
];
const thumbnailTypes = [RelatedUrlType.THUMBNAIL, RelatedUrlType.GET_RELATED_VISUALIZATION];
return (concept.relatedUrls ?? [])
.filter((relatedUrl) =>
thumbnailTypes.find((thumbnailType) => thumbnailType === relatedUrl["type"])
)
.reduce((metadataAssets, relatedUrl, idx, available) => {
const [entry, display] = available.length > 1 ? [`_${idx}`, ` [${idx}]`] : ["", ""];

const relatedUrlKey = extractAssetMapKey(relatedUrl.url);
const thumbnailAsset: AssetLinks = {};
thumbnailAsset[`thumbnail${entry}`] = {
href: relatedUrl.url,
title: `Thumbnail${display}`,
description: relatedUrl.description,
[relatedUrlKey]: relatedUrl.url,
roles: ["thumbnail"],
};
return { ...metadataAssets, ...thumbnailAsset };
Expand Down
10 changes: 10 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ export const wrapErrorHandler = (fn: (rq: Request, rs: Response) => Promise<void
};
};

/**
* Returns second to last value in a relatedUrl
* to be used as a key for thumbnail assets link.
* Defaults to string 'key' in the case of an unexpected URL format.
*/
export const extractAssetMapKey = (relatedUrl: string) => {
const urlArray = relatedUrl.split(".");
return urlArray[urlArray.length - 2] ? urlArray[urlArray.length - 2] : "key";
};

export const stacContext = (req: Request) => {
const { headers, originalUrl } = req;
const isCloudStac = headers["cloud-stac"] === "true";
Expand Down
12 changes: 12 additions & 0 deletions src/utils/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ const baseGranule: Granule = {
description: "metadata",
type: RelatedUrlType.DATA_SET_LANDING_PAGE,
},
{
urlContentType: UrlContentType.VISUALIZATION_URL,
url: "ftp://e4ftl01.cr.usgs.gov/MODIS_Composites/MOTA/MCD43A4.005/2009.09.14/MCD43A4.A2009257.h29v03.005.2009276045828.vinr.img",
description: "Browse image for Earthdata Search",
type: RelatedUrlType.THUMBNAIL,
},
{
urlContentType: UrlContentType.VISUALIZATION_URL,
url: "ftp://e4ftl012/ExampleBadUrl",
description: "Browse image for Earthdata Search",
type: RelatedUrlType.THUMBNAIL,
},
],
};

Expand Down

0 comments on commit 364960b

Please sign in to comment.