Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/services/src/file/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ const detectMimeTypeFromSignature = async (file: File): Promise<string> => {
}
};

const PLAIN_TEXT_MIME_MAP: Record<string, string> = {
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
Expand All @@ -103,8 +115,14 @@ const validateAndDetectFileType = async (file: File): Promise<string> => {
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 || "";
};

/**
Expand Down