import type { IncomingMessage, ServerResponse } from "node:http"; type Photo = { id: string; name: string; latitude: number | null; longitude: number | null; capturedAt: string | null; thumbUrl: string; fullUrl: string; source: "demo" | "nextcloud"; }; type ShareListingEntry = { href: string; name: string; lastModified: string | null; contentType: string; isCollection: boolean; }; function resolveDavUrlFromShareUrl(shareUrl: string): string { const url = new URL(shareUrl); const publicShare = url.pathname.match(/\/s\/([^/?#]+)/); if (publicShare) { return `${url.origin}/public.php/dav/files/${publicShare[1]}/`; } const davShare = url.pathname.match(/\/public\.php\/dav\/files\/([^/?#]+)\/?/); if (davShare) { return `${url.origin}/public.php/dav/files/${davShare[1]}/`; } throw new Error("Please enter a public Nextcloud share link."); } async function proxyUpstream(url: string, init?: RequestInit) { const upstream = await fetch(url, init); const body = await upstream.arrayBuffer(); return { body: Buffer.from(body), contentType: upstream.headers.get("content-type") ?? "application/octet-stream", status: upstream.status }; } function htmlPage(): string { return ` mapy-mg

mapy-mg

Browser-based photo geolocation on OpenStreetMap.

Client-side processing, no server-side storage
Hover for thumbnail · click for fullscreen · route sorted by time

Timeline

No photos imported yet.
`; } export function createRequestHandler() { return async function handleRequest(req: IncomingMessage, res: ServerResponse) { const url = new URL(req.url ?? "/", "http://localhost"); if (url.pathname === "/health") { res.statusCode = 200; res.setHeader("content-type", "application/json; charset=utf-8"); res.end(JSON.stringify({ status: "ok" })); return; } if (url.pathname === "/api/nextcloud/list") { const share = url.searchParams.get("share"); if (!share) { res.statusCode = 400; res.setHeader("content-type", "application/json; charset=utf-8"); res.end(JSON.stringify({ error: "missing `share` parameter" })); return; } try { const davUrl = resolveDavUrlFromShareUrl(share); const xml = '' + '' + "" + "" + "" + "" + "" + "" + "" + ""; const upstream = await proxyUpstream(davUrl, { method: "PROPFIND", headers: { Depth: "1", "Content-Type": "application/xml; charset=utf-8", Accept: "application/xml, text/xml" }, body: xml }); res.statusCode = upstream.status; res.setHeader("content-type", upstream.contentType); res.end(upstream.body); return; } catch (error) { res.statusCode = 502; res.setHeader("content-type", "application/json; charset=utf-8"); res.end( JSON.stringify({ error: error instanceof Error ? error.message : "upstream request failed" }) ); return; } } if (url.pathname === "/api/nextcloud/blob") { const target = url.searchParams.get("url"); if (!target) { res.statusCode = 400; res.setHeader("content-type", "application/json; charset=utf-8"); res.end(JSON.stringify({ error: "missing `url` parameter" })); return; } try { const upstream = await proxyUpstream(target); res.statusCode = upstream.status; res.setHeader("content-type", upstream.contentType); res.end(upstream.body); return; } catch (error) { res.statusCode = 502; res.setHeader("content-type", "application/json; charset=utf-8"); res.end( JSON.stringify({ error: error instanceof Error ? error.message : "upstream request failed" }) ); return; } } res.statusCode = 200; res.setHeader("content-type", "text/html; charset=utf-8"); res.end(htmlPage()); }; }