initial version of streba 2

This commit is contained in:
Vincent van der Wal
2026-07-22 09:24:18 +02:00
commit 196f30521c
31 changed files with 5056 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
import { fail } from '@sveltejs/kit';
import { db, listPeaks, markPeakClimbed } from '$lib/server/db';
import { matchPeaks, parseGpx } from '$lib/server/gpx';
import type { Actions } from './$types';
export const actions: Actions = {
default: async ({ request }) => {
const form = await request.formData();
const files = form.getAll('gpx').filter((f): f is File => f instanceof File && f.size > 0);
if (files.length === 0) return fail(400, { error: 'No GPX files selected.' });
const uploaded: { id: number; name: string; newPeaks: string[] }[] = [];
const errors: string[] = [];
const insert = db.prepare(`
INSERT INTO activities
(name, type, date, distance_m, duration_s, moving_s,
elev_gain_m, elev_loss_m, elev_min_m, elev_max_m, points, bounds)
VALUES
(@name, @type, @date, @distance_m, @duration_s, @moving_s,
@elev_gain_m, @elev_loss_m, @elev_min_m, @elev_max_m, @points, @bounds)
`);
for (const file of files) {
try {
const gpx = parseGpx(await file.text());
const result = insert.run({
name: gpx.name ?? file.name.replace(/\.gpx$/i, ''),
type: gpx.type ?? 'outdoor',
date: gpx.date,
distance_m: gpx.distance_m,
duration_s: gpx.duration_s,
moving_s: gpx.moving_s,
elev_gain_m: gpx.elev_gain_m,
elev_loss_m: gpx.elev_loss_m,
elev_min_m: gpx.elev_min_m,
elev_max_m: gpx.elev_max_m,
points: JSON.stringify(gpx.points),
bounds: JSON.stringify(gpx.bounds)
});
const activityId = Number(result.lastInsertRowid);
const unclimbed = listPeaks().filter((p) => !p.climbed_at);
const bagged = matchPeaks(gpx.points, unclimbed);
for (const peak of bagged) markPeakClimbed(peak.id, activityId, gpx.date);
uploaded.push({
id: activityId,
name: gpx.name ?? file.name,
newPeaks: bagged.map((p) => p.name)
});
} catch (err) {
errors.push(`${file.name}: ${err instanceof Error ? err.message : 'could not parse'}`);
}
}
if (uploaded.length === 0) return fail(422, { error: errors.join(' ') });
return { uploaded, errors };
}
};