diff --git a/src/api/file-proxy.api.js b/src/api/file-proxy.api.js index 8a0c5fd8b..426a0f7f7 100644 --- a/src/api/file-proxy.api.js +++ b/src/api/file-proxy.api.js @@ -16,11 +16,22 @@ export function transformFileUrl(fileUrl) { } // copy from https://github.com/geoadmin/mf-geoadmin3/blob/master/src/components/UrlUtilsService.js#L59-L69 const parts = /^(http|https)(:\/\/)(.+)/.exec(fileUrl) + if (parts?.length < 4) { + return null + } const protocol = parts[1] const resource = parts[3] return `${protocol}/${encodeURIComponent(resource)}` } +export function proxifyUrl(url) { + const fileAsPath = transformFileUrl(url) + if (!fileAsPath) { + throw new Error(`Malformed URL: ${url}, can't proxify`) + } + return `${API_SERVICE_PROXY_BASE_URL}${fileAsPath}` +} + /** * Get a file through our service-proxy backend, taking care of CORS headers in the process. * @@ -35,22 +46,25 @@ export function transformFileUrl(fileUrl) { export default function getFileThroughProxy(fileUrl, options = {}) { const { timeout = null } = options return new Promise((resolve, reject) => { - const fileAsPath = transformFileUrl(fileUrl) - if (!fileAsPath) { - reject(new Error(`Malformed file URL: ${fileUrl}`)) - return - } - axios({ - method: 'get', - url: `${API_SERVICE_PROXY_BASE_URL}${fileAsPath}`, - timeout, - }) - .then((response) => { - resolve(response) - }) - .catch((error) => { - log.error('Error while accessing file URL through service-proxy', fileUrl, error) - reject(error) + try { + axios({ + method: 'get', + url: proxifyUrl(fileUrl), + timeout, }) + .then((response) => { + resolve(response) + }) + .catch((error) => { + log.error( + 'Error while accessing file URL through service-proxy', + fileUrl, + error + ) + reject(error) + }) + } catch (error) { + reject(error) + } }) } diff --git a/src/store/plugins/load-gpx-data.plugin.js b/src/store/plugins/load-gpx-data.plugin.js index 119503121..2c3f2579b 100644 --- a/src/store/plugins/load-gpx-data.plugin.js +++ b/src/store/plugins/load-gpx-data.plugin.js @@ -6,6 +6,7 @@ import axios from 'axios' import GPX from 'ol/format/GPX' +import { proxifyUrl } from '@/api/file-proxy.api' import GPXLayer from '@/api/layers/GPXLayer.class' import log from '@/utils/logging' @@ -19,7 +20,7 @@ const dispatcher = { dispatcher: 'load-gpx-data.plugin' } async function loadGpx(store, gpxLayer) { log.debug(`Loading data for added GPX layer`, gpxLayer) try { - const response = await axios.get(gpxLayer.gpxFileUrl) + const response = await axios.get(proxifyUrl(gpxLayer.gpxFileUrl)) const gpxContent = response.data const gpxParser = new GPX() const metadata = gpxParser.readMetadata(gpxContent) diff --git a/src/utils/legacyLayerParamUtils.js b/src/utils/legacyLayerParamUtils.js index 1714d3efc..ee52e2ffc 100644 --- a/src/utils/legacyLayerParamUtils.js +++ b/src/utils/legacyLayerParamUtils.js @@ -1,6 +1,7 @@ import { getKmlMetadataByAdminId } from '@/api/files.api' import ExternalWMSLayer from '@/api/layers/ExternalWMSLayer.class' import ExternalWMTSLayer from '@/api/layers/ExternalWMTSLayer.class' +import GPXLayer from '@/api/layers/GPXLayer.class' import KMLLayer from '@/api/layers/KMLLayer.class' import storeSyncConfig from '@/router/storeSync/storeSync.config' import log from '@/utils/logging' @@ -112,6 +113,10 @@ export function getLayersFromLegacyUrlParams( const [_layerType, url] = layerId.split('||') layer = new KMLLayer({ kmlFileUrl: url, visible: true }) } + if (layerId.startsWith('GPX||')) { + const [_layerType, url] = layerId.split('||') + layer = new GPXLayer({ gpxFileUrl: url, visible: true }) + } if (layerId.startsWith('WMTS||')) { const [_layerType, id, url] = layerId.split('||') if (layerId && url) { diff --git a/tests/cypress/tests-e2e/importToolFile.cy.js b/tests/cypress/tests-e2e/importToolFile.cy.js index 852d30b78..e3dce56be 100644 --- a/tests/cypress/tests-e2e/importToolFile.cy.js +++ b/tests/cypress/tests-e2e/importToolFile.cy.js @@ -1,5 +1,7 @@ /// +import { proxifyUrl } from '@/api/file-proxy.api.js' + describe('The Import File Tool', () => { it('Import KML file', () => { cy.goToMapView({}, true) @@ -501,7 +503,7 @@ describe('The Import File Tool', () => { cy.log('Test online import') const validOnlineUrl = 'http://example.com/valid-gpx-file.gpx' const gpxOnlineLayerId = `GPX|${validOnlineUrl}` - cy.intercept('GET', validOnlineUrl, { + cy.intercept('GET', proxifyUrl(validOnlineUrl), { fixture: gpxFileFixture, }).as('getGpxFile')