Skip to content

Commit

Permalink
Merge pull request #990 from geoadmin/fix-PB-783-legacy-gpx
Browse files Browse the repository at this point in the history
PB-783 : adding legacy GPX parsing - #patch
  • Loading branch information
pakb authored Jul 4, 2024
2 parents d4e3fa3 + bb2309f commit b2c8712
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
46 changes: 30 additions & 16 deletions src/api/file-proxy.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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)
}
})
}
3 changes: 2 additions & 1 deletion src/store/plugins/load-gpx-data.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/utils/legacyLayerParamUtils.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion tests/cypress/tests-e2e/importToolFile.cy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// <reference types="cypress" />

import { proxifyUrl } from '@/api/file-proxy.api.js'

describe('The Import File Tool', () => {
it('Import KML file', () => {
cy.goToMapView({}, true)
Expand Down Expand Up @@ -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')

Expand Down

0 comments on commit b2c8712

Please sign in to comment.