KI-AGENT: Interne MinIO-Links durch Backend-Downloads ersetzen
This commit is contained in:
@@ -177,11 +177,49 @@ export default async function exportRoutes(server: FastifyInstance) {
|
||||
.from(generatedexports)
|
||||
.where(eq(generatedexports.tenantId, req.user.tenant_id))
|
||||
|
||||
console.log(data)
|
||||
reply.send(data)
|
||||
reply.send(data.map(item => ({
|
||||
...item,
|
||||
url: `/api/exports/${item.id}/download`,
|
||||
})))
|
||||
|
||||
})
|
||||
|
||||
server.get("/exports/:id/download", async (req, reply) => {
|
||||
const { id } = req.params as { id: string }
|
||||
const exportId = Number(id)
|
||||
if (!Number.isSafeInteger(exportId)) {
|
||||
return reply.code(404).send({ error: "Export not found" })
|
||||
}
|
||||
|
||||
const [item] = await server.db
|
||||
.select()
|
||||
.from(generatedexports)
|
||||
.where(eq(generatedexports.id, exportId))
|
||||
|
||||
if (
|
||||
!item ||
|
||||
item.tenantId !== req.user.tenant_id ||
|
||||
item.validUntil.getTime() <= Date.now()
|
||||
) {
|
||||
return reply.code(404).send({ error: "Export not found" })
|
||||
}
|
||||
|
||||
try {
|
||||
const { Body, ContentType } = await s3.send(new GetObjectCommand({
|
||||
Bucket: secrets.S3_BUCKET,
|
||||
Key: item.filePath,
|
||||
}))
|
||||
const filename = item.filePath.split("/").pop() || `export-${item.id}`
|
||||
|
||||
reply.header("Content-Type", ContentType || "application/octet-stream")
|
||||
reply.header("Content-Disposition", `attachment; filename="${filename}"`)
|
||||
return reply.send(Body as any)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return reply.code(500).send({ error: "Could not download export" })
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user