diff --git a/packages/services/src/file/helper.ts b/packages/services/src/file/helper.ts index b8e96283986..8c896822453 100644 --- a/packages/services/src/file/helper.ts +++ b/packages/services/src/file/helper.ts @@ -81,6 +81,18 @@ const detectMimeTypeFromSignature = async (file: File): Promise => { } }; +const PLAIN_TEXT_MIME_MAP: Record = { + md: "text/markdown", + markdown: "text/markdown", + txt: "text/plain", + csv: "text/csv", + json: "application/json", + css: "text/css", + js: "text/javascript", + sql: "application/x-sql", + xml: "text/xml", +}; + /** * @description Validate and detect the MIME type of a file using signature detection * Also performs basic security checks on filename @@ -103,8 +115,14 @@ const validateAndDetectFileType = async (file: File): Promise => { console.warn("Error detecting file type from signature:", _error); } - // fallback for unknown files - return ""; + // Fallback 1: Static extension lookup for whitelisted plain-text files + const extension = file.name.split(".").pop()?.toLowerCase() || ""; + if (Object.hasOwn(PLAIN_TEXT_MIME_MAP, extension)) { + return PLAIN_TEXT_MIME_MAP[extension]; + } + + // Fallback 2: Generic browser/OS fallback + return file.type || ""; }; /**