fix: Unknown mimetype should fallback to Markdown parsing if markdown extension (#1678)

closes #1675
This commit is contained in:
Tom Moor 2020-11-26 21:16:56 -08:00 committed by GitHub
parent ac349b40f5
commit 6eda1cc0d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 2 deletions

View File

@ -1,5 +1,6 @@
// @flow
import fs from "fs";
import path from "path";
import File from "formidable/lib/file";
import { strikethrough, tables } from "joplin-turndown-plugin-gfm";
import mammoth from "mammoth";
@ -139,7 +140,16 @@ export default async function documentImporter({
file: File,
ip: string,
}): Promise<{ text: string, title: string }> {
const fileInfo = importMapping.filter((item) => item.type === file.type)[0];
const fileInfo = importMapping.filter((item) => {
if (item.type === file.type) {
return true;
}
if (item.type === "text/markdown" && path.extname(file.name) === ".md") {
return true;
}
return false;
})[0];
if (!fileInfo) {
throw new InvalidRequestError(`File type ${file.type} not supported`);
}

View File

@ -94,9 +94,28 @@ describe("documentImporter", () => {
expect(response.title).toEqual("Heading 1");
});
it("should error with unknown file type", async () => {
it("should fallback to extension if mimetype unknown", async () => {
const user = await buildUser();
const name = "markdown.md";
const file = new File({
name,
type: "application/octet-stream",
path: path.resolve(__dirname, "..", "test", "fixtures", name),
});
const response = await documentImporter({
user,
file,
ip,
});
expect(response.text).toContain("This is a test paragraph");
expect(response.title).toEqual("Heading 1");
});
it("should error with unknown file type", async () => {
const user = await buildUser();
const name = "files.zip";
const file = new File({
name,
type: "executable/zip",