diff --git a/server/commands/documentImporter.js b/server/commands/documentImporter.js index efa615c48..4410914f7 100644 --- a/server/commands/documentImporter.js +++ b/server/commands/documentImporter.js @@ -5,6 +5,7 @@ import mammoth from "mammoth"; import TurndownService from "turndown"; import uuid from "uuid"; import parseTitle from "../../shared/utils/parseTitle"; +import { InvalidRequestError } from "../errors"; import { Attachment, Event, User } from "../models"; import dataURItoBuffer from "../utils/dataURItoBuffer"; import parseImages from "../utils/parseImages"; @@ -66,6 +67,9 @@ export default async function documentImporter({ ip: string, }): Promise<{ text: string, title: string }> { const fileInfo = importMapping.filter((item) => item.type === file.type)[0]; + if (!fileInfo) { + throw new InvalidRequestError(`File type ${file.type} not supported`); + } let title = file.name.replace(/\.[^/.]+$/, ""); let text = await fileInfo.getMarkdown(file); diff --git a/server/commands/documentImporter.test.js b/server/commands/documentImporter.test.js index 2cac52725..66714eeea 100644 --- a/server/commands/documentImporter.test.js +++ b/server/commands/documentImporter.test.js @@ -74,4 +74,27 @@ describe("documentImporter", () => { 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 = "markdown.md"; + const file = new File({ + name, + type: "executable/zip", + path: path.resolve(__dirname, "..", "test", "fixtures", name), + }); + + let error; + try { + await documentImporter({ + user, + file, + ip, + }); + } catch (err) { + error = err.message; + } + + expect(error).toEqual("File type executable/zip not supported"); + }); });