File size: 3,849 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import { NextResponse } from "next/server";

import { createProxyResponse, fetchBackend } from "@/server/backend-api";
import { getServerSession } from "@/server/session";

async function proxyTaskRequest(
  request: Request,
  { params }: { params: Promise<{ path: string[] }> }
) {
  const session = await getServerSession();
  if (!session?.user?.id) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  const { path } = await params;
  const incomingUrl = new URL(request.url);
  const targetPath = `/tasks/${path.join("/")}${incomingUrl.search}`;
  const body =
    request.method === "GET" || request.method === "HEAD"
      ? undefined
      : await request.text();

  const upstream = await fetchBackend(targetPath, {
    method: request.method,
    // Close the backend stream even when the client leaves before headers arrive.
    signal: request.signal,
    userId: session.user.id,
    extraHeaders: {
      ...(body && request.headers.get("content-type")
        ? { "Content-Type": request.headers.get("content-type") as string }
        : {}),
      ...(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 }
        : {}),
    },
    body,
    cache: "no-store",
  });

  // Canvas video needs a CORS-enabled response. Following a same-origin
  // redirect to object storage can produce a request without Origin, so the
  // bucket returns usable bytes but no CORS header. Keep editor assets on this
  // authenticated origin; ordinary clip downloads still use signed redirects.
  const editorMedia = path.length === 6 && path[1] === "clips" &&
    path[3] === "editor" && path[4] === "media" &&
    ["clean.mp4", "layout.mp4", "thumbnails.jpg"].includes(path[5]);
  if (request.method === "GET" && editorMedia && upstream.status === 307) {
    const location = upstream.headers.get("location");
    let target: URL;
    try {
      target = new URL(location ?? "");
      if (!["https:", "http:"].includes(target.protocol) || target.username || target.password) throw new Error("Invalid media URL");
    } catch {
      await upstream.body?.cancel();
      return NextResponse.json({ error: "Preview storage returned an invalid URL." }, { status: 502 });
    }
    await upstream.body?.cancel();
    const mediaHeaders: Record<string, string> = {};
    for (const name of ["range", "if-range", "accept"]) {
      const value = request.headers.get(name);
      if (value) mediaHeaders[name] = value;
    }
    // Location comes from the authenticated backend, never the client. Do not
    // send its signed backend-auth headers, cookies, or client credentials to S3.
    const media = await fetch(target.toString(), {
      method: "GET", headers: mediaHeaders, signal: request.signal,
      cache: "no-store", credentials: "omit",
    });
    const response = createProxyResponse(media);
    response.headers.set("cache-control", "private, no-store");
    return response;
  }
  return createProxyResponse(upstream);
}

export async function GET(
  request: Request,
  context: { params: Promise<{ path: string[] }> }
) {
  return proxyTaskRequest(request, context);
}

export async function POST(
  request: Request,
  context: { params: Promise<{ path: string[] }> }
) {
  return proxyTaskRequest(request, context);
}

export async function PATCH(
  request: Request,
  context: { params: Promise<{ path: string[] }> }
) {
  return proxyTaskRequest(request, context);
}

export async function DELETE(
  request: Request,
  context: { params: Promise<{ path: string[] }> }
) {
  return proxyTaskRequest(request, context);
}