Fixed Files Loading
This commit is contained in:
@@ -95,6 +95,33 @@ export default async function fileRoutes(server: FastifyInstance) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
server.get("/files/:id?", async (req, reply) => {
|
||||||
|
const { id } = req.params as { id?: string }
|
||||||
|
|
||||||
|
if(id) {
|
||||||
|
try {
|
||||||
|
const {data,error} = await server.supabase.from("files").select("*").eq("id", id).single()
|
||||||
|
|
||||||
|
return {...data}
|
||||||
|
} catch (err) {
|
||||||
|
req.log.error(err);
|
||||||
|
reply.code(500).send({ error: "Could not generate presigned URL" });
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const {data:supabaseFileEntries,error} = await server.supabase.from("files").select("*, createddocument(*, customer(*))").eq("tenant",req.user.tenant_id)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return { files: supabaseFileEntries }
|
||||||
|
} catch (err) {
|
||||||
|
req.log.error(err)
|
||||||
|
reply.code(500).send({ error: "Could not generate presigned URLs" })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
server.post("/files/download/:id?", async (req, reply) => {
|
server.post("/files/download/:id?", async (req, reply) => {
|
||||||
const { id } = req.params as { id?: string }
|
const { id } = req.params as { id?: string }
|
||||||
|
|
||||||
@@ -207,15 +234,25 @@ export default async function fileRoutes(server: FastifyInstance) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const {data:supabaseFileEntries,error} = await server.supabase.from("files").select("*, createddocument(*, customer(*))").in("id", ids)
|
const {data:supabaseFileEntries,error} = await server.supabase.from("files").select("*, createddocument(*, customer(*))").eq("tenant",req.user.tenant_id).is("archived",false)
|
||||||
|
|
||||||
|
console.log(error)
|
||||||
|
|
||||||
|
let filteredFiles = supabaseFileEntries.filter(i => ids.includes(i.id))
|
||||||
|
filteredFiles = filteredFiles.filter(i => i.path)
|
||||||
|
|
||||||
|
console.log(filteredFiles.filter(i => !i.path))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const urls = await Promise.all(
|
let urls = await Promise.all(
|
||||||
ids.map(async (id) => {
|
ids.map(async (id) => {
|
||||||
|
let file = filteredFiles.find(i => i.id === id)
|
||||||
|
|
||||||
let key = supabaseFileEntries.find(i => i.id === id).path
|
if(!file) return
|
||||||
|
|
||||||
|
let key = file.path
|
||||||
|
if(!key) console.log(file)
|
||||||
|
|
||||||
const command = new GetObjectCommand({
|
const command = new GetObjectCommand({
|
||||||
Bucket: process.env.S3_BUCKET || "FEDEO",
|
Bucket: process.env.S3_BUCKET || "FEDEO",
|
||||||
@@ -224,13 +261,16 @@ export default async function fileRoutes(server: FastifyInstance) {
|
|||||||
|
|
||||||
const url = await getSignedUrl(s3, command, { expiresIn: 900 }) // 15 min gültig
|
const url = await getSignedUrl(s3, command, { expiresIn: 900 }) // 15 min gültig
|
||||||
|
|
||||||
return {...supabaseFileEntries.find(i => i.id === id), url}
|
|
||||||
|
return {...filteredFiles.find(i => i.id === id), url}
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
urls = urls.filter(i => i)
|
||||||
|
|
||||||
return { files: urls }
|
return { files: urls }
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
req.log.error(err)
|
console.log(err)
|
||||||
reply.code(500).send({ error: "Could not generate presigned URLs" })
|
reply.code(500).send({ error: "Could not generate presigned URLs" })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user