fixes gpx import/export errors on Safari

This commit is contained in:
Christian Beutel
2025-01-10 16:17:19 +01:00
parent 3d953aa0e9
commit 5274d25199
3 changed files with 15 additions and 9 deletions

View File

@@ -119,11 +119,13 @@ export default class GPX {
}
static parse(gpxString: string): Promise<GPX | Error> {
return new Promise<GPX | Error>((resolve, reject) => xml2js.parseString(gpxString, {
const sanitizedGPX = gpxString.replace(/\sxmlns=""/g, '');
return new Promise<GPX | Error>((resolve, reject) => xml2js.parseString(sanitizedGPX, {
explicitArray: false,
attrValueProcessors: [(str: string | number) => {
if (!isNaN(Number(str))) {
str = Number.isInteger(Number(str)) ? parseInt(String(str), 10) : parseFloat(String(str));
attrValueProcessors: [(str: string) => {
if (str.length && !isNaN(Number(str))) {
return Number.isInteger(Number(str)) ? parseInt(String(str), 10) : parseFloat(String(str));
}
return str;
}
@@ -158,6 +160,9 @@ export default class GPX {
xmlString = xmlString.replace('<gpx', `<gpx xmlns="${defaultAttributes["xmlns"]}"`);
}
// Safari for some ungodly reason adds empty xmlns attributes to every tag...
xmlString = xmlString.replace(/ xmlns=""/g, '');
return xmlString;
}
}

View File

@@ -123,8 +123,8 @@
/>
{:else}
{#each data.trails as trail}
<a href="/trail/view/{trail.id}">
<TrailCard {trail}></TrailCard></a
<a class="w-full md:max-w-72" href="/trail/view/{trail.id}">
<TrailCard {trail} fullWidth></TrailCard></a
>
{/each}
{/if}

View File

@@ -2,8 +2,8 @@ import { SummitLog } from "$lib/models/summit_log";
import type { Trail } from "$lib/models/trail";
import { trails_create } from "$lib/stores/trail_store";
import { handleError } from "$lib/util/api_util";
import { fromFile, fromFIT, fromKML, fromTCX, gpx2trail, isFITFile } from "$lib/util/gpx_util";
import { error, json, type RequestEvent } from "@sveltejs/kit";
import { fromFile, gpx2trail } from "$lib/util/gpx_util";
import { json, type RequestEvent } from "@sveltejs/kit";
import { ClientResponseError } from "pocketbase";
export async function PUT(event: RequestEvent) {
@@ -30,6 +30,7 @@ export async function PUT(event: RequestEvent) {
trail.expand!.summit_logs.push(log);
} catch (e: any) {
console.error(e)
throw new ClientResponseError({ status: 400, response: { message: "Invalid file" } })
}