mirror of
https://github.com/mgeeky/decode-spam-headers.git
synced 2026-02-22 05:23:31 +01:00
MAESTRO: extract headers from dropped EML
This commit is contained in:
@@ -97,7 +97,9 @@ describe("FileDropZone", () => {
|
||||
|
||||
it("reads dropped EML/TXT file content", () => {
|
||||
const handleContent = vi.fn();
|
||||
const restore = mockFileReader("Header from file");
|
||||
const restore = mockFileReader(
|
||||
"From: sender@example.com\r\nSubject: Hello\r\n\r\nBody: should be ignored",
|
||||
);
|
||||
const { container } = render(<FileDropZone onFileContent={handleContent} />);
|
||||
const dropZone = getDropZone(container);
|
||||
const file = new File(["Header from file"], "sample.eml", { type: "message/rfc822" });
|
||||
@@ -108,7 +110,7 @@ describe("FileDropZone", () => {
|
||||
|
||||
restore();
|
||||
|
||||
expect(handleContent).toHaveBeenCalledWith("Header from file");
|
||||
expect(handleContent).toHaveBeenCalledWith("From: sender@example.com\nSubject: Hello");
|
||||
});
|
||||
|
||||
it("rejects unsupported file types with feedback", () => {
|
||||
|
||||
@@ -58,6 +58,28 @@ const getFirstFile = (transfer: DataTransfer | null): File | null => {
|
||||
return null;
|
||||
};
|
||||
|
||||
const normalizeLineEndings = (value: string): string =>
|
||||
value.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
||||
|
||||
const extractHeaderBlock = (content: string): string => {
|
||||
const normalized = normalizeLineEndings(content);
|
||||
const lines = normalized.split("\n");
|
||||
const headerLines: string[] = [];
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.trim() === "") {
|
||||
break;
|
||||
}
|
||||
headerLines.push(line);
|
||||
}
|
||||
|
||||
if (headerLines.length === 0) {
|
||||
return normalized.trim();
|
||||
}
|
||||
|
||||
return headerLines.join("\n").trimEnd();
|
||||
};
|
||||
|
||||
export default function FileDropZone({ onFileContent }: FileDropZoneProps) {
|
||||
const [isDragging, setIsDragging] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
@@ -80,7 +102,7 @@ export default function FileDropZone({ onFileContent }: FileDropZoneProps) {
|
||||
reader.onload = () => {
|
||||
const result = reader.result;
|
||||
const content = typeof result === "string" ? result : "";
|
||||
onFileContent(content);
|
||||
onFileContent(extractHeaderBlock(content));
|
||||
};
|
||||
reader.onerror = () => {
|
||||
setError("Unable to read the dropped file.");
|
||||
|
||||
Reference in New Issue
Block a user