Skip to content
Snippets Groups Projects
Commit 3e8057a8 authored by Marcus Schiesser's avatar Marcus Schiesser
Browse files

improve saveDocument

parent 12ed570a
No related branches found
No related tags found
No related merge requests found
import fs from "fs"; import fs from "node:fs";
import path from "node:path";
import { getExtractors } from "../../engine/loader"; import { getExtractors } from "../../engine/loader";
const MIME_TYPE_TO_EXT: Record<string, string> = { const MIME_TYPE_TO_EXT: Record<string, string> = {
...@@ -19,7 +20,8 @@ export async function storeAndParseFile( ...@@ -19,7 +20,8 @@ export async function storeAndParseFile(
if (!fileExt) throw new Error(`Unsupported document type: ${mimeType}`); if (!fileExt) throw new Error(`Unsupported document type: ${mimeType}`);
const documents = await loadDocuments(fileBuffer, mimeType); const documents = await loadDocuments(fileBuffer, mimeType);
await saveDocument(filename, fileBuffer); const filepath = path.join(UPLOADED_FOLDER, filename);
await saveDocument(filepath, fileBuffer);
for (const document of documents) { for (const document of documents) {
document.metadata = { document.metadata = {
...document.metadata, ...document.metadata,
...@@ -41,19 +43,31 @@ async function loadDocuments(fileBuffer: Buffer, mimeType: string) { ...@@ -41,19 +43,31 @@ async function loadDocuments(fileBuffer: Buffer, mimeType: string) {
return await reader.loadDataAsContent(fileBuffer); return await reader.loadDataAsContent(fileBuffer);
} }
export async function saveDocument(filename: string, fileBuffer: Buffer) { // Save document to file server and return the file url
const filepath = `${UPLOADED_FOLDER}/${filename}`; export async function saveDocument(filepath: string, content: string | Buffer) {
const fileurl = `${process.env.FILESERVER_URL_PREFIX}/${filepath}`; if (path.isAbsolute(filepath)) {
throw new Error("Absolute file paths are not allowed.");
}
const fileName = path.basename(filepath);
if (!/^[a-zA-Z0-9_.-]+$/.test(fileName)) {
throw new Error(
"File name is not allowed to contain any special characters.",
);
}
if (!process.env.FILESERVER_URL_PREFIX) {
throw new Error("FILESERVER_URL_PREFIX environment variable is not set.");
}
if (!fs.existsSync(UPLOADED_FOLDER)) { const dirPath = path.dirname(filepath);
fs.mkdirSync(UPLOADED_FOLDER, { recursive: true }); await fs.promises.mkdir(dirPath, { recursive: true });
if (typeof content === "string") {
await fs.promises.writeFile(filepath, content, "utf-8");
} else {
await fs.promises.writeFile(filepath, content);
} }
await fs.promises.writeFile(filepath, fileBuffer);
const fileurl = `${process.env.FILESERVER_URL_PREFIX}/${filepath}`;
console.log(`Saved document file to ${filepath}.\nURL: ${fileurl}`); console.log(`Saved document to ${filepath}. Reachable at URL: ${fileurl}`);
return { return fileurl;
filename,
filepath,
fileurl,
};
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment