Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.4 KB
Raw Blame
import { NextResponse } from "next/server";
import { parseIntegerQueryParam } from "@/lib/api/queryIntegerParam";
import { listFiles } from "@/lib/db/files";
import { getApiKeyRequestScope, resolveListScope } from "@/app/api/v1/_helpers/apiKeyScope";

export async function GET(request: Request) {
  const scope = await getApiKeyRequestScope(request);
  if (scope.rejection) return scope.rejection;

  // Key → own files only; dashboard session without a key → instance-wide;
  // anonymous / unresolvable bearer → 401. Mirrors /api/v1/files (GHSA-2jm2-
  // mpx8-6523 / GHSA-m3hp-hq9g-fpmv) so this management sibling cannot leak
  // every tenant's files to an unauthenticated caller (#13882).
  const listScope = resolveListScope(scope);
  if (listScope.mode === "rejected") return listScope.response;

  try {
    const url = new URL(request.url);
    const parsedLimit = parseIntegerQueryParam(url.searchParams, "limit", 100);
    if (parsedLimit.ok === false) return parsedLimit.response;
    const limit = parsedLimit.value;
    const files =
      listScope.mode === "api_key"
        ? listFiles({ limit, apiKeyId: listScope.apiKeyId })
        : listFiles({ limit });
    return NextResponse.json({ files });
  } catch (error) {
    console.log("Error fetching files:", error);
    return NextResponse.json({ error: "Failed to fetch files" }, { status: 500 });
  }
}