Skip to content

Commit 3018f9c

Browse files
authored
Fix URL resolving because OGC APIs have fucked up the URI resolving #486 (#646)
1 parent 4e30e19 commit 3018f9c

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

src/store/index.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import URI from "urijs";
66
import i18n from '../i18n';
77
import Utils, { BrowserError } from '../utils';
88
import { addMissingChildren, getDisplayTitle, createSTAC } from '../models/stac';
9-
import { Collection, CatalogLike, STAC } from 'stac-js';
9+
import { CatalogLike, STAC } from 'stac-js';
1010

1111
import auth from './auth.js';
1212
import { addQueryIfNotExists, isAuthenticationError, Loading, processSTAC, proxyUrl, unproxyUrl, stacRequest } from './utils';
@@ -772,7 +772,7 @@ function getStore(config, router) {
772772

773773
// Load API Collections
774774
const apiCollectionLink = data instanceof CatalogLike && data.getApiCollectionsLink();
775-
const apiItemLink = data instanceof Collection && data.getApiItemsLink();
775+
const apiItemLink = data instanceof CatalogLike && data.getApiItemsLink();
776776
if (!omitApi && apiCollectionLink) {
777777
let args = { stac: data, show: loading.show };
778778
try {
@@ -828,6 +828,9 @@ function getStore(config, router) {
828828
link = stac.getApiItemsLink();
829829
baseUrl = stac.getAbsoluteUrl();
830830
}
831+
if (baseUrl) {
832+
baseUrl = new URI(baseUrl);
833+
}
831834

832835
link = Utils.addFiltersToLink(link, filters, cx.state.itemsPerPage);
833836

@@ -841,21 +844,31 @@ function getStore(config, router) {
841844
if (!Utils.isObject(item) || item.type !== 'Feature') {
842845
return null;
843846
}
847+
// See https://github.com/radiantearth/stac-browser/issues/486
844848
let selfLink = Utils.getLinkWithRel(item.links, 'self');
845849
let url;
846850
if (selfLink?.href) {
847-
url = Utils.toAbsolute(selfLink.href, baseUrl);
851+
url = Utils.toAbsolute(selfLink.href, baseUrl, false);
848852
}
849853
else if (typeof item.id !== 'undefined') {
850-
let apiCollectionsLink = cx.getters.root?.getApiCollectionsLink();
851-
if (baseUrl) {
852-
url = Utils.toAbsolute(`items/${item.id}`, baseUrl);
854+
let apiCollectionsLink = cx.getters.root?.getApiCollectionsLink()?.href;
855+
if (apiCollectionsLink) {
856+
apiCollectionsLink = new URI(apiCollectionsLink);
857+
}
858+
if (baseUrl && baseUrl.path().endsWith('/')) {
859+
url = Utils.toAbsolute(`items/${item.id}`, baseUrl, false);
860+
}
861+
else if (baseUrl) {
862+
url = Utils.toAbsolute(`${collectionId}/items/${item.id}`, baseUrl, false);
863+
}
864+
else if (apiCollectionsLink?.path().endsWith('/')) {
865+
url = Utils.toAbsolute(`${collectionId}/items/${item.id}`, apiCollectionsLink, false);
853866
}
854867
else if (apiCollectionsLink) {
855-
url = Utils.toAbsolute(`${collectionId}/items/${item.id}`, apiCollectionsLink.href);
868+
url = Utils.toAbsolute(`collections/${collectionId}/items/${item.id}`, apiCollectionsLink, false);
856869
}
857870
else if (cx.state.catalogUrl) {
858-
url = Utils.toAbsolute(`collections/${collectionId}/items/${item.id}`, cx.state.catalogUrl);
871+
url = Utils.toAbsolute(`collections/${collectionId}/items/${item.id}`, cx.state.catalogUrl, false);
859872
}
860873
else {
861874
return null;
@@ -864,6 +877,7 @@ function getStore(config, router) {
864877
else {
865878
return null;
866879
}
880+
url = url.toString();
867881
let data = cx.getters.getStac(url);
868882
if (data) {
869883
return data;
@@ -925,11 +939,23 @@ function getStore(config, router) {
925939
let selfLink = Utils.getLinkWithRel(collection.links, 'self');
926940
let url;
927941
if (selfLink?.href) {
928-
url = Utils.toAbsolute(selfLink.href, cx.state.url || stac.getAbsoluteUrl());
942+
url = Utils.toAbsolute(selfLink.href, cx.state.url || stac.getAbsoluteUrl(), false);
929943
}
930944
else {
931-
url = Utils.toAbsolute(`collections/${collection.id}`, cx.state.catalogUrl || stac.getAbsoluteUrl());
945+
// see https://github.com/radiantearth/stac-browser/issues/486
946+
let baseUrl = cx.state.catalogUrl || stac.getAbsoluteUrl();
947+
if (baseUrl) {
948+
baseUrl = new URI(baseUrl);
949+
if (!baseUrl.path().endsWith('/')) {
950+
baseUrl.path(baseUrl.path() + '/');
951+
}
952+
url = Utils.toAbsolute(`collections/${collection.id}`, baseUrl, false);
953+
}
954+
}
955+
if (!url) {
956+
return null; // We can't detect a URL, skip this flawed collection
932957
}
958+
url = url.toString();
933959
let data = cx.getters.getStac(url);
934960
if (data) {
935961
return data;

0 commit comments

Comments
 (0)