mirror of
https://github.com/seanmorley15/AdventureLog.git
synced 2025-07-30 18:29:37 +02:00
refactor: Optimize GPX file processing with concurrent fetching and improved error handling
This commit is contained in:
parent
d9a88b644f
commit
50a732b4d7
1 changed files with 26 additions and 17 deletions
|
@ -24,42 +24,51 @@
|
||||||
|
|
||||||
// Collect all GPX file attachments
|
// Collect all GPX file attachments
|
||||||
if (adventure.attachments && adventure.attachments.length > 0) {
|
if (adventure.attachments && adventure.attachments.length > 0) {
|
||||||
adventure.attachments
|
gpxfiles = adventure.attachments
|
||||||
.filter((attachment) => attachment.extension === 'gpx')
|
.filter((attachment) => attachment.extension === 'gpx')
|
||||||
.forEach((attachment) => gpxfiles.push(attachment.file));
|
.map((attachment) => attachment.file);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize a collection GeoJSON object
|
// Initialize the GeoJSON collection
|
||||||
geojson = {
|
geojson = {
|
||||||
type: 'FeatureCollection',
|
type: 'FeatureCollection',
|
||||||
features: []
|
features: []
|
||||||
};
|
};
|
||||||
|
|
||||||
// Process each GPX file
|
// Process each GPX file concurrently
|
||||||
if (gpxfiles.length > 0) {
|
if (gpxfiles.length > 0) {
|
||||||
for (const gpxfile of gpxfiles) {
|
const promises = gpxfiles.map(async (gpxfile) => {
|
||||||
try {
|
try {
|
||||||
let gpxFileName = gpxfile.split('/').pop();
|
const gpxFileName = gpxfile.split('/').pop();
|
||||||
let res = await fetch('/gpx/' + gpxFileName);
|
const res = await fetch('/gpx/' + gpxFileName);
|
||||||
|
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
console.error(`Failed to fetch GPX file: ${gpxFileName}`);
|
console.error(`Failed to fetch GPX file: ${gpxFileName}`);
|
||||||
continue;
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
let gpxData = await res.text();
|
const gpxData = await res.text();
|
||||||
let parser = new DOMParser();
|
const parser = new DOMParser();
|
||||||
let gpx = parser.parseFromString(gpxData, 'text/xml');
|
const gpx = parser.parseFromString(gpxData, 'text/xml');
|
||||||
|
|
||||||
// Convert GPX to GeoJSON and merge features
|
// Convert GPX to GeoJSON and return features
|
||||||
let convertedGeoJSON = toGeoJSON.gpx(gpx);
|
const convertedGeoJSON = toGeoJSON.gpx(gpx);
|
||||||
if (convertedGeoJSON.features) {
|
return convertedGeoJSON.features || [];
|
||||||
geojson.features.push(...convertedGeoJSON.features);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error processing GPX file ${gpxfile}:`, error);
|
console.error(`Error processing GPX file ${gpxfile}:`, error);
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
// Use Promise.allSettled to ensure every promise resolves,
|
||||||
|
// even if some requests fail.
|
||||||
|
const results = await Promise.allSettled(promises);
|
||||||
|
|
||||||
|
results.forEach((result) => {
|
||||||
|
if (result.status === 'fulfilled' && result.value.length > 0) {
|
||||||
|
geojson.features.push(...result.value);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue