48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import { LoaderFunctionArgs } from "@remix-run/node";
|
|
import { postgrestGet } from "~/api/postgrest-client";
|
|
|
|
/**
|
|
* 文档下载路由 - 处理文档下载请求
|
|
* 通过重定向到带有授权的连接来允许下载文件
|
|
*/
|
|
export async function loader({ request }: LoaderFunctionArgs) {
|
|
try {
|
|
// 获取文件路径参数
|
|
const url = new URL(request.url);
|
|
const filePath = url.searchParams.get("path");
|
|
|
|
if (!filePath) {
|
|
return new Response("缺少文件路径参数", { status: 400 });
|
|
}
|
|
|
|
// 调用Minio API获取带有授权的预签名URL
|
|
// 这里假设后端有一个生成预签名URL的API
|
|
const response = await postgrestGet<{ presignedUrl: string }>(
|
|
'/minio/presign',
|
|
{
|
|
filter: {
|
|
'object_path': `eq.${filePath}`,
|
|
'expires_in': 'eq.300' // 5分钟有效期
|
|
}
|
|
}
|
|
);
|
|
|
|
if (response.error) {
|
|
console.error("获取文件下载链接失败:", response.error);
|
|
return new Response("获取文件下载链接失败", { status: 500 });
|
|
}
|
|
|
|
if (!response.data?.presignedUrl) {
|
|
return new Response("无法获取文件下载链接", { status: 404 });
|
|
}
|
|
|
|
// 重定向到预签名URL,这样浏览器就能直接下载文件
|
|
return Response.redirect(response.data.presignedUrl);
|
|
} catch (error) {
|
|
console.error("文件下载处理失败:", error);
|
|
return new Response(
|
|
"文件下载处理失败: " + (error instanceof Error ? error.message : "未知错误"),
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|