Skip to content

Commit

Permalink
fix: Unknown mimetype should fallback to Markdown parsing if markdown…
Browse files Browse the repository at this point in the history
… extension (outline#1678)

closes outline#1675
  • Loading branch information
tommoor authored Nov 27, 2020
1 parent ac349b4 commit 6eda1cc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
12 changes: 11 additions & 1 deletion server/commands/documentImporter.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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`);
}
Expand Down
21 changes: 20 additions & 1 deletion server/commands/documentImporter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 6eda1cc

Please sign in to comment.