2026-01-10 23:08:56 +08:00
|
|
|
import { NextResponse } from "next/server";
|
2026-01-17 10:12:53 +08:00
|
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
|
|
|
|
import { api } from "@/lib/convex/api";
|
2026-01-18 19:01:31 +08:00
|
|
|
import { HttpError } from "@/lib/auth/authContext";
|
|
|
|
|
import { getAuthedConvexClient } from "@/lib/convex/route";
|
2026-01-10 23:08:56 +08:00
|
|
|
|
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
|
|
|
|
export async function GET(request: Request) {
|
2026-01-17 10:12:53 +08:00
|
|
|
if (isConvexEnabled()) {
|
|
|
|
|
let auth;
|
2026-01-18 19:01:31 +08:00
|
|
|
let client;
|
2026-01-17 10:12:53 +08:00
|
|
|
try {
|
2026-01-18 19:01:31 +08:00
|
|
|
const authed = await getAuthedConvexClient();
|
|
|
|
|
auth = authed.auth;
|
|
|
|
|
client = authed.client;
|
2026-01-17 10:12:53 +08:00
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
|
return NextResponse.json({ error: "未登录" }, { status: err.status });
|
|
|
|
|
}
|
|
|
|
|
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
|
|
|
const assetId = searchParams.get("assetId");
|
|
|
|
|
|
|
|
|
|
if (!assetId) {
|
|
|
|
|
return NextResponse.json({ error: "缺少 assetId" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const asset = await client.query(api.mediaAssets.getById, { userId: auth.userId, id: assetId });
|
|
|
|
|
if (!asset) {
|
|
|
|
|
return NextResponse.json({ error: "资源不存在" }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const refreshed = await client.mutation(api.mediaAssets.refreshUrl, { userId: auth.userId, id: assetId });
|
|
|
|
|
const signedUrl = (refreshed as { signedUrl?: string | null } | null)?.signedUrl ?? null;
|
|
|
|
|
if (!signedUrl) {
|
|
|
|
|
return NextResponse.json({ error: "生成签名链接失败" }, { status: 500 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NextResponse.json({
|
|
|
|
|
signedUrl,
|
|
|
|
|
asset: {
|
|
|
|
|
id: asset.id,
|
|
|
|
|
file_name: asset.file_name,
|
|
|
|
|
mime_type: asset.mime_type,
|
|
|
|
|
file_size: asset.file_size,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 19:01:31 +08:00
|
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
2026-01-10 23:08:56 +08:00
|
|
|
}
|