-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathresources.packets.$environmentId.$.ts
More file actions
59 lines (51 loc) · 1.45 KB
/
resources.packets.$environmentId.$.ts
File metadata and controls
59 lines (51 loc) · 1.45 KB
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
import { LoaderFunctionArgs } from "@remix-run/node";
import { basename } from "node:path";
import { z } from "zod";
import { prisma } from "~/db.server";
import { requireUserId } from "~/services/session.server";
import { generatePresignedRequest } from "~/v3/objectStore.server";
const ParamSchema = z.object({
environmentId: z.string(),
"*": z.string(),
});
export async function loader({ request, params }: LoaderFunctionArgs) {
const userId = await requireUserId(request);
const { environmentId, "*": filename } = ParamSchema.parse(params);
const environment = await prisma.runtimeEnvironment.findFirst({
where: {
id: environmentId,
organization: {
members: {
some: {
userId,
},
},
},
},
include: {
project: true,
},
});
if (!environment) {
return new Response("Not found", { status: 404 });
}
const signed = await generatePresignedRequest(
environment.project.externalRef,
environment.slug,
filename,
"GET"
);
if (!signed.success) {
return new Response(`Failed to generate presigned URL: ${signed.error}`, { status: 500 });
}
const response = await fetch(signed.request.url, {
headers: signed.request.headers,
});
return new Response(response.body, {
status: 200,
headers: {
"Content-Type": "application/octet-stream",
"Content-Disposition": `attachment; filename="${basename(filename)}"`,
},
});
}