This commit is contained in:
2026-03-16 20:46:26 +01:00
parent 52c182cb5f
commit 8a08147265
36 changed files with 51386 additions and 237 deletions

View File

@@ -6,6 +6,7 @@ import { secrets } from "./secrets"
import { files } from "../../db/schema"
import { eq } from "drizzle-orm"
import { FastifyInstance } from "fastify"
import { storeExtractedTextForFile } from "./documentText"
export const saveFile = async (
server: FastifyInstance,
@@ -17,6 +18,13 @@ export const saveFile = async (
other: Record<string, any> = {}
) => {
try {
const {
filename: providedFilename,
filesize: _providedFilesize,
mimeType: providedMimeType,
...dbFields
} = other
// ---------------------------------------------------
// 1⃣ FILE ENTRY ANLEGEN
// ---------------------------------------------------
@@ -26,7 +34,7 @@ export const saveFile = async (
tenant,
folder,
type,
...other
...dbFields
})
.returning()
@@ -38,13 +46,13 @@ export const saveFile = async (
// Name ermitteln (Fallback Logik)
// Wenn attachment ein Buffer ist, muss der Name in 'other' stehen oder generiert werden
const filename = attachment.filename || other.filename || `${created.id}.pdf`
const filename = attachment.filename || providedFilename || `${created.id}.pdf`
// ---------------------------------------------------
// 2⃣ BODY & CONTENT TYPE ERMITTELN
// ---------------------------------------------------
let body: Buffer | Uint8Array | string
let contentType = type || "application/octet-stream"
let contentType = providedMimeType || "application/octet-stream"
if (Buffer.isBuffer(attachment)) {
// FALL 1: RAW BUFFER (von finishManualGeneration)
@@ -83,13 +91,26 @@ export const saveFile = async (
// ---------------------------------------------------
await server.db
.update(files)
.set({ path: key })
.set({
path: key,
mimeType: contentType,
name: filename,
size: body.length
})
.where(eq(files.id, created.id))
await storeExtractedTextForFile(
server,
created.id,
Buffer.isBuffer(body) ? body : Buffer.from(body),
contentType,
filename
)
console.log(`File saved: ${key}`)
return { id: created.id, key }
} catch (err) {
console.error("saveFile error:", err)
return null
}
}
}