route.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // app/api/files/route.js
  2. import { NextResponse } from "next/server";
  3. import { listFiles } from "@/lib/storage";
  4. import { getSession } from "@/lib/auth/session";
  5. import { canAccessBranch } from "@/lib/auth/permissions";
  6. /**
  7. * GET /api/files?branch=&year=&month=&day=
  8. */
  9. export async function GET(request) {
  10. const session = await getSession();
  11. if (!session) {
  12. return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  13. }
  14. const { searchParams } = new URL(request.url);
  15. const branch = searchParams.get("branch");
  16. const year = searchParams.get("year");
  17. const month = searchParams.get("month");
  18. const day = searchParams.get("day");
  19. console.log("[/api/files] query:", { branch, year, month, day });
  20. if (!branch || !year || !month || !day) {
  21. return NextResponse.json(
  22. { error: "branch, year, month, day sind erforderlich" },
  23. { status: 400 }
  24. );
  25. }
  26. if (!canAccessBranch(session, branch)) {
  27. return NextResponse.json({ error: "Forbidden" }, { status: 403 });
  28. }
  29. try {
  30. const files = await listFiles(branch, year, month, day);
  31. return NextResponse.json({ branch, year, month, day, files });
  32. } catch (error) {
  33. console.error("[/api/files] Error:", error);
  34. return NextResponse.json(
  35. { error: "Fehler beim Lesen der Dateien: " + error.message },
  36. { status: 500 }
  37. );
  38. }
  39. }