Skip to content

Commit 0d1e1d2

Browse files
authored
fix: expand the assets that can be considered thumbnails (#298)
Closes #288 cc @weiji14
1 parent ab2ca75 commit 0d1e1d2

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/utils/stac.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import bboxPolygon from "@turf/bbox-polygon";
33
import type { FeatureCollection } from "geojson";
44
import type {
55
SpatialExtent,
6-
StacAsset,
76
StacCollection,
87
StacItem,
98
StacLink,
@@ -62,7 +61,13 @@ export function getSelfHref(value: StacValue) {
6261

6362
export function getThumbnailAsset(value: StacValue) {
6463
if ("assets" in value) {
65-
const asset = (value.assets as { [key: string]: StacAsset })["thumbnail"];
64+
const assets = value.assets as StacAssets;
65+
const asset =
66+
assets["thumbnail"] ||
67+
Object.values(assets).find((asset) =>
68+
asset.roles?.includes("thumbnail")
69+
) ||
70+
assets["thumbnails"];
6671
return asset?.href.startsWith("http") && asset;
6772
}
6873
}

tests/utils/stac.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,30 @@ describe("getThumbnailAsset", () => {
168168
item.assets = { data: { href: "http://example.com/data.tiff" } };
169169
expect(getThumbnailAsset(item)).toBeUndefined();
170170
});
171+
172+
test("returns thumbnail asset if it has thumbnail role", () => {
173+
const item = makeItem("test");
174+
item.assets = {
175+
foo: { href: "http://example.com/thumb.png", roles: ["thumbnail"] },
176+
};
177+
expect(getThumbnailAsset(item)).toEqual({
178+
href: "http://example.com/thumb.png",
179+
roles: ["thumbnail"],
180+
});
181+
});
182+
183+
test("returns thumbnail asset if it has thumbnails key", () => {
184+
// https://github.com/englacial/xopr/issues/64
185+
const item = makeItem("test");
186+
item.assets = {
187+
thumbnails: {
188+
href: "http://example.com/thumb.png",
189+
},
190+
};
191+
expect(getThumbnailAsset(item)).toEqual({
192+
href: "http://example.com/thumb.png",
193+
});
194+
});
171195
});
172196

173197
describe("makeHrefsAbsolute", () => {

0 commit comments

Comments
 (0)