trails/apps/journal/app/routes/api.v1.uploads.ts
Ullrich Schäfer 18c3c37eaf
Implement Journal REST API v1 endpoints
Add all REST API endpoints for mobile app consumption:

Routes:
- GET /api/v1/routes — paginated list with cursor, Zod validation
- GET /api/v1/routes/:id — full detail with GPX, versions
- POST /api/v1/routes — create with Zod-validated body
- PUT /api/v1/routes/:id — update, creates new version
- DELETE /api/v1/routes/:id — returns 204

Activities:
- GET /api/v1/activities — paginated list with cursor
- GET /api/v1/activities/:id — full detail
- POST /api/v1/activities — create with GPX stat extraction
- DELETE /api/v1/activities/:id — returns 204

Supporting:
- POST /api/v1/routes/compute — BRouter proxy
- POST /api/v1/uploads — presigned upload URL generation

Device management:
- GET /api/v1/auth/devices — list connected devices with isCurrent
- DELETE /api/v1/auth/devices/:id — revoke device token
- Store device_name on token exchange

Infrastructure:
- requireApiUser() guard — returns 401 with structured error
- apiError() helper for consistent error responses
- All endpoints use Zod schemas from @trails-cool/api for validation

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 00:57:27 +02:00

31 lines
1.3 KiB
TypeScript

import type { Route } from "./+types/api.v1.uploads";
import { requireApiUser, apiError } from "~/lib/api-guard.server";
import { PresignedUploadRequestSchema, ERROR_CODES } from "@trails-cool/api";
import { randomUUID } from "node:crypto";
const S3_ENDPOINT = process.env.S3_ENDPOINT ?? "http://localhost:3902";
const S3_BUCKET = process.env.S3_BUCKET ?? "trails-cool";
const S3_PUBLIC_URL = process.env.S3_PUBLIC_URL ?? S3_ENDPOINT;
/** POST /api/v1/uploads — generate presigned upload URL */
export async function action({ request }: Route.ActionArgs) {
if (request.method !== "POST") return new Response(null, { status: 405 });
await requireApiUser(request);
const body = await request.json().catch(() => null);
const parsed = PresignedUploadRequestSchema.safeParse(body);
if (!parsed.success) {
return apiError(400, ERROR_CODES.VALIDATION_ERROR, "Validation failed",
parsed.error.issues.map((i) => ({ field: i.path.join("."), message: i.message })));
}
const { filename, resourceType, resourceId } = parsed.data;
const key = `${resourceType}/${resourceId}/${randomUUID()}-${filename}`;
// Return the upload URL and the final public URL
return Response.json({
uploadUrl: `${S3_ENDPOINT}/${S3_BUCKET}/${key}`,
publicUrl: `${S3_PUBLIC_URL}/${S3_BUCKET}/${key}`,
key,
});
}