adds filename fallback for trail upload
This commit is contained in:
@@ -2,7 +2,11 @@
|
|||||||
import { page } from "$app/stores";
|
import { page } from "$app/stores";
|
||||||
import { Settings } from "$lib/models/settings";
|
import { Settings } from "$lib/models/settings";
|
||||||
import type { Trail } from "$lib/models/trail";
|
import type { Trail } from "$lib/models/trail";
|
||||||
import { createMarkerFromWaypoint, endIcon, startIcon } from "$lib/util/leaflet_util";
|
import {
|
||||||
|
createMarkerFromWaypoint,
|
||||||
|
endIcon,
|
||||||
|
startIcon,
|
||||||
|
} from "$lib/util/leaflet_util";
|
||||||
import "$lib/vendor/leaflet-elevation/src/index.css";
|
import "$lib/vendor/leaflet-elevation/src/index.css";
|
||||||
import type AutoGraticule from "$lib/vendor/leaflet-graticule/leaflet-auto-graticule";
|
import type AutoGraticule from "$lib/vendor/leaflet-graticule/leaflet-auto-graticule";
|
||||||
import type { Map, Marker } from "leaflet";
|
import type { Map, Marker } from "leaflet";
|
||||||
@@ -27,7 +31,7 @@
|
|||||||
let selectedMetric: "altitude" | "slope" | "speed" | false = "altitude";
|
let selectedMetric: "altitude" | "slope" | "speed" | false = "altitude";
|
||||||
|
|
||||||
$: gpxData = trail?.expand.gpx_data;
|
$: gpxData = trail?.expand.gpx_data;
|
||||||
$: if (gpxData && controlElevation) {
|
$: if (gpxData && controlElevation) {
|
||||||
controlElevation.updateOptions({
|
controlElevation.updateOptions({
|
||||||
autofitBounds: options.autofitBounds ?? true,
|
autofitBounds: options.autofitBounds ?? true,
|
||||||
});
|
});
|
||||||
@@ -103,7 +107,9 @@
|
|||||||
const baseMaps: Record<string, L.TileLayer> = {
|
const baseMaps: Record<string, L.TileLayer> = {
|
||||||
OpenStreetMaps: baseLayer,
|
OpenStreetMaps: baseLayer,
|
||||||
OpenTopoMaps: topoLayer,
|
OpenTopoMaps: topoLayer,
|
||||||
...($page.data.settings as Settings)?.tilesets?.reduce< Record<string, string>>((t, current) => {
|
...($page.data.settings as Settings)?.tilesets?.reduce<
|
||||||
|
Record<string, string>
|
||||||
|
>((t, current) => {
|
||||||
t[current.name] = L.tileLayer(current.url);
|
t[current.name] = L.tileLayer(current.url);
|
||||||
return t;
|
return t;
|
||||||
}, {}),
|
}, {}),
|
||||||
@@ -139,7 +145,7 @@
|
|||||||
closeBtn: false,
|
closeBtn: false,
|
||||||
followMarker: true,
|
followMarker: true,
|
||||||
autofitBounds: true,
|
autofitBounds: true,
|
||||||
imperial: $page.data.settings.unit == "imperial",
|
imperial: ($page.data.settings?.unit ?? "metric") == "imperial",
|
||||||
reverseCoords: false,
|
reverseCoords: false,
|
||||||
acceleration: false,
|
acceleration: false,
|
||||||
slope: true,
|
slope: true,
|
||||||
@@ -174,7 +180,7 @@
|
|||||||
trkStart: {
|
trkStart: {
|
||||||
interactive: false,
|
interactive: false,
|
||||||
className: "hihi",
|
className: "hihi",
|
||||||
icon: startIcon()
|
icon: startIcon(),
|
||||||
},
|
},
|
||||||
trkEnd: {
|
trkEnd: {
|
||||||
icon: endIcon(),
|
icon: endIcon(),
|
||||||
@@ -241,4 +247,4 @@
|
|||||||
<slot />
|
<slot />
|
||||||
<div class="basis-[300px] flex-grow flex-shrink-0" id="elevation"></div>
|
<div class="basis-[300px] flex-grow flex-shrink-0" id="elevation"></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -129,7 +129,7 @@
|
|||||||
const baseMaps: Record<string, L.TileLayer> = {
|
const baseMaps: Record<string, L.TileLayer> = {
|
||||||
OpenStreetMaps: baseLayer,
|
OpenStreetMaps: baseLayer,
|
||||||
OpenTopoMaps: topoLayer,
|
OpenTopoMaps: topoLayer,
|
||||||
...($page.data.settings as Settings)?.tilesets?.reduce<
|
...($page.data?.settings as Settings)?.tilesets?.reduce<
|
||||||
Record<string, string>
|
Record<string, string>
|
||||||
>((t, current) => {
|
>((t, current) => {
|
||||||
t[current.name] = L.tileLayer(current.url);
|
t[current.name] = L.tileLayer(current.url);
|
||||||
|
|||||||
@@ -346,12 +346,14 @@ export async function trails_get_bounding_box(f: (url: RequestInfo | URL, config
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function trails_upload(file: File, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch): Promise<TrailFilterValues> {
|
export async function trails_upload(file: File, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch): Promise<TrailFilterValues> {
|
||||||
|
const fd = new FormData()
|
||||||
|
|
||||||
|
fd.append("name", file.name),
|
||||||
|
fd.append("file", file)
|
||||||
|
|
||||||
const r = await f('/api/v1/trail/upload', {
|
const r = await f('/api/v1/trail/upload', {
|
||||||
headers: {
|
|
||||||
"Content-Type": "multipart/form-data"
|
|
||||||
},
|
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: file
|
body: fd
|
||||||
})
|
})
|
||||||
|
|
||||||
if (r.ok) {
|
if (r.ok) {
|
||||||
|
|||||||
@@ -15,7 +15,8 @@ import * as xmldom from 'xmldom'
|
|||||||
import type { Feature, FeatureCollection, GeoJsonProperties, Position } from 'geojson';
|
import type { Feature, FeatureCollection, GeoJsonProperties, Position } from 'geojson';
|
||||||
|
|
||||||
|
|
||||||
export async function gpx2trail(gpxString: string) {
|
export async function gpx2trail(gpxString: string, fallbackName?: string) {
|
||||||
|
|
||||||
const gpx = await GPX.parse(gpxString);
|
const gpx = await GPX.parse(gpxString);
|
||||||
|
|
||||||
if (gpx instanceof Error) {
|
if (gpx instanceof Error) {
|
||||||
@@ -24,7 +25,7 @@ export async function gpx2trail(gpxString: string) {
|
|||||||
|
|
||||||
const trail = new Trail("");
|
const trail = new Trail("");
|
||||||
|
|
||||||
trail.name = gpx.metadata?.name || gpx.trk?.at(0)?.name || gpx.rte?.at(0)?.name || `trail-${new Date().toISOString()}`;
|
trail.name = gpx.metadata?.name || gpx.trk?.at(0)?.name || gpx.rte?.at(0)?.name || fallbackName || `trail-${new Date().toISOString()}`;
|
||||||
|
|
||||||
trail.description = gpx.metadata?.desc;
|
trail.description = gpx.metadata?.desc;
|
||||||
|
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ import { ClientResponseError } from "pocketbase";
|
|||||||
|
|
||||||
export async function PUT(event: RequestEvent) {
|
export async function PUT(event: RequestEvent) {
|
||||||
try {
|
try {
|
||||||
const data = await event.request.blob();
|
const data = await event.request.formData();
|
||||||
const fileBuffer = await data.arrayBuffer();
|
const fileBuffer = await (data.get("file") as Blob).arrayBuffer();
|
||||||
const fileContent = await data.text();
|
const fileContent = await (data.get("file") as Blob).text();
|
||||||
let gpxData = ""
|
let gpxData = ""
|
||||||
let gpxFile: Blob;
|
let gpxFile: Blob;
|
||||||
if (isFITFile(fileBuffer)) {
|
if (isFITFile(fileBuffer)) {
|
||||||
@@ -29,14 +29,14 @@ export async function PUT(event: RequestEvent) {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
gpxData = fileContent;
|
gpxData = fileContent;
|
||||||
gpxFile = data
|
gpxFile = data.get("file") as Blob
|
||||||
}
|
}
|
||||||
if (!gpxData.length) {
|
if (!gpxData.length) {
|
||||||
throw new ClientResponseError({ status: 400, response: { message: "Empty file" } })
|
throw new ClientResponseError({ status: 400, response: { message: "Empty file" } })
|
||||||
}
|
}
|
||||||
let trail: Trail;
|
let trail: Trail;
|
||||||
try {
|
try {
|
||||||
trail = (await gpx2trail(gpxData)).trail;
|
trail = (await gpx2trail(gpxData, data.get("name") as string | undefined)).trail;
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
throw new ClientResponseError({ status: 400, response: { message: "Invalid file" } })
|
throw new ClientResponseError({ status: 400, response: { message: "Invalid file" } })
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -285,7 +285,7 @@
|
|||||||
let gpxData = await parseFile(selectedFile);
|
let gpxData = await parseFile(selectedFile);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const parseResult = await gpx2trail(gpxData);
|
const parseResult = await gpx2trail(gpxData, selectedFile.name);
|
||||||
$form = parseResult.trail;
|
$form = parseResult.trail;
|
||||||
$form.expand.gpx_data = gpxData;
|
$form.expand.gpx_data = gpxData;
|
||||||
$form.category = $page.data.settings.category || $categories[0].id;
|
$form.category = $page.data.settings.category || $categories[0].id;
|
||||||
|
|||||||
Reference in New Issue
Block a user