adds gpx upload for summit logs
This commit is contained in:
@@ -1,15 +1,35 @@
|
||||
import { SummitLog } from "$lib/models/summit_log";
|
||||
import { pb } from "$lib/pocketbase";
|
||||
import { ClientResponseError } from "pocketbase";
|
||||
import { writable, type Writable } from "svelte/store";
|
||||
|
||||
export const summitLog: Writable<SummitLog> = writable(new SummitLog(new Date().toISOString().substring(0, 10)));
|
||||
|
||||
export async function summit_logs_create(summitLog: SummitLog) {
|
||||
const r = await fetch('/api/v1/summit-log', {
|
||||
summitLog.author = pb.authStore.model!.id
|
||||
|
||||
let r = await fetch('/api/v1/summit-log', {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(summitLog),
|
||||
})
|
||||
|
||||
if (!r.ok) {
|
||||
throw new ClientResponseError(await r.json())
|
||||
}
|
||||
|
||||
if (summitLog._gpx && summitLog._gpx instanceof File) {
|
||||
let model: SummitLog = await r.json();
|
||||
|
||||
const formData = new FormData()
|
||||
|
||||
formData.append("gpx", summitLog._gpx)
|
||||
|
||||
r = await fetch(`/api/v1/summit-log/${model.id!}/file`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
})
|
||||
}
|
||||
|
||||
if (r.ok) {
|
||||
return await r.json();
|
||||
} else {
|
||||
@@ -18,11 +38,28 @@ export async function summit_logs_create(summitLog: SummitLog) {
|
||||
}
|
||||
|
||||
export async function summit_logs_update(summitLog: SummitLog) {
|
||||
const r = await fetch('/api/v1/summit-log/' + summitLog.id, {
|
||||
summitLog.author = pb.authStore.model!.id
|
||||
|
||||
let r = await fetch('/api/v1/summit-log/' + summitLog.id, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(summitLog),
|
||||
})
|
||||
|
||||
if (!r.ok) {
|
||||
throw new ClientResponseError(await r.json())
|
||||
}
|
||||
|
||||
|
||||
if (summitLog._gpx) {
|
||||
const formData = new FormData()
|
||||
|
||||
formData.append("gpx", summitLog._gpx);
|
||||
r = await fetch(`/api/v1/summit-log/${summitLog.id!}/file`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
})
|
||||
}
|
||||
|
||||
if (r.ok) {
|
||||
return await r.json();
|
||||
} else {
|
||||
|
||||
@@ -152,6 +152,16 @@ export async function trails_show(id: string, loadGPX?: boolean, f: (url: Reques
|
||||
response.expand = {};
|
||||
}
|
||||
response.expand.gpx_data = gpxData;
|
||||
|
||||
|
||||
for (const log of response.expand.summit_logs) {
|
||||
const gpxData: string = await fetchGPX(log, f);
|
||||
|
||||
if (!log.expand) {
|
||||
log.expand = {};
|
||||
}
|
||||
log.expand.gpx_data = gpxData;
|
||||
}
|
||||
}
|
||||
|
||||
response.expand.waypoints = response.expand.waypoints || [];
|
||||
@@ -349,8 +359,8 @@ export async function trails_upload(file: File, f: (url: RequestInfo | URL, conf
|
||||
const fd = new FormData()
|
||||
|
||||
fd.append("name", file.name),
|
||||
fd.append("file", file)
|
||||
|
||||
fd.append("file", file)
|
||||
|
||||
const r = await f('/api/v1/trail/upload', {
|
||||
method: 'PUT',
|
||||
body: fd
|
||||
@@ -363,7 +373,7 @@ export async function trails_upload(file: File, f: (url: RequestInfo | URL, conf
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchGPX(trail: Trail, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch) {
|
||||
export async function fetchGPX(trail: { gpx: string } & Record<string, any>, f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch) {
|
||||
if (!trail.gpx) {
|
||||
return "";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user