adds api gpx upload route
This commit is contained in:
@@ -193,10 +193,10 @@ export async function trails_show(id: string, loadGPX?: boolean, f: (url: Reques
|
||||
return response;
|
||||
}
|
||||
|
||||
export async function trails_create(trail: Trail, photos: File[], gpx: File | null) {
|
||||
export async function trails_create(trail: Trail, photos: File[], gpx: File | Blob | null, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch) {
|
||||
|
||||
if (!pb.authStore.model) {
|
||||
throw new Error("Unauthenticated");
|
||||
throw new ClientResponseError({ status: 401, response: { message: "Forbidden" } });
|
||||
}
|
||||
|
||||
for (const waypoint of trail.expand.waypoints) {
|
||||
@@ -213,7 +213,7 @@ export async function trails_create(trail: Trail, photos: File[], gpx: File | nu
|
||||
|
||||
trail.author = pb.authStore.model!.id
|
||||
|
||||
let r = await fetch('/api/v1/trail', {
|
||||
let r = await f('/api/v1/trail', {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify({ ...trail, expand: undefined }),
|
||||
})
|
||||
@@ -233,7 +233,7 @@ export async function trails_create(trail: Trail, photos: File[], gpx: File | nu
|
||||
formData.append("photos", photo)
|
||||
}
|
||||
|
||||
r = await fetch(`/api/v1/trail/${model.id!}/file`, {
|
||||
r = await f(`/api/v1/trail/${model.id!}/file`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
})
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
const privateRoutes = [
|
||||
"/profile",
|
||||
"/lists",
|
||||
]
|
||||
|
||||
export function isRouteProtected(path: string) {
|
||||
return privateRoutes.some(allowedPath =>
|
||||
path === allowedPath || path.startsWith(allowedPath + '/')
|
||||
);
|
||||
}
|
||||
"/profile",
|
||||
"/lists",
|
||||
]
|
||||
|
||||
export function isRouteProtected(path: string) {
|
||||
return privateRoutes.some(allowedPath =>
|
||||
path === allowedPath || path.startsWith(allowedPath + '/')
|
||||
);
|
||||
}
|
||||
38
web/src/lib/util/gpx_util.ts
Normal file
38
web/src/lib/util/gpx_util.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Trail } from "$lib/models/trail";
|
||||
import { Waypoint } from "$lib/models/waypoint";
|
||||
import { JSDOM } from "jsdom"
|
||||
|
||||
export function gpx2trail(gpx: string) {
|
||||
const xml = new JSDOM(gpx).window.document
|
||||
const trail = new Trail("");
|
||||
|
||||
let name = xml.getElementsByTagName('name');
|
||||
if (name.length > 0) {
|
||||
trail.name = name[0].textContent ?? "";
|
||||
}
|
||||
let desc = xml.getElementsByTagName('desc');
|
||||
if (desc.length > 0) {
|
||||
trail.description = desc[0].textContent ?? "";
|
||||
}
|
||||
|
||||
const el = xml.getElementsByTagName('wpt');
|
||||
for (let i = 0; i < el.length; i++) {
|
||||
const wp = new Waypoint(parseFloat(el[i].getAttribute('lat')!), parseFloat(el[i].getAttribute('lon')!));
|
||||
|
||||
let nameEl = el[i].getElementsByTagName('name');
|
||||
wp.name = nameEl.length > 0 ? nameEl[0].textContent ?? "" : '';
|
||||
|
||||
let descEl = el[i].getElementsByTagName('desc');
|
||||
wp.description = descEl.length > 0 ? descEl[0].textContent ?? "" : '';
|
||||
|
||||
trail.expand.waypoints.push(wp);
|
||||
}
|
||||
|
||||
const startPoint = xml.getElementsByTagName("trk")[0].getElementsByTagName("trkseg")[0].getElementsByTagName("trkpt")[0];
|
||||
if(startPoint) {
|
||||
trail.lat = parseFloat(startPoint.getAttribute('lat')!)
|
||||
trail.lon = parseFloat(startPoint.getAttribute('lon')!)
|
||||
}
|
||||
|
||||
return trail
|
||||
}
|
||||
Reference in New Issue
Block a user