File size: 1,377 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { createProxyResponse, fetchBackend } from "@/server/backend-api";

// Share tokens, clip IDs, and the fixed "clips"/"file" segments are all URL-safe;
// anything else (".." traversal, encoded slashes) must not reach the backend URL.
const SAFE_PATH_SEGMENT = /^[A-Za-z0-9_-]+$/;

export async function GET(
  request: Request,
  { params }: { params: Promise<{ path: string[] }> },
) {
  const { path } = await params;
  if (path.length === 0 || !path.every((segment) => SAFE_PATH_SEGMENT.test(segment))) {
    return Response.json({ detail: "Shared result not found" }, { status: 404 });
  }
  const download = new URL(request.url).searchParams.get("download") === "true";
  const upstream = await fetchBackend(`/tasks/shared/${path.join("/")}${download ? "?download=true" : ""}`, {
    method: "GET",
    // Close the backend stream even when the client leaves before headers arrive.
    signal: request.signal,
    extraHeaders: {
      ...(request.headers.get("accept")
        ? { Accept: request.headers.get("accept") as string }
        : {}),
      ...(request.headers.get("range")
        ? { Range: request.headers.get("range") as string }
        : {}),
      ...(request.headers.get("if-range")
        ? { "If-Range": request.headers.get("if-range") as string }
        : {}),
    },
    cache: "no-store",
  });

  return createProxyResponse(upstream);
}