Skip to content

Commit

Permalink
refactor(plugins): merge doc-to-txt and docx-to-txt plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed May 25, 2023
1 parent 01ba28e commit 04f8455
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 162 deletions.
4 changes: 2 additions & 2 deletions src/plugins/doc-to-txt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const WordExtractor = require("word-extractor");
/**
* @author Frazer Smith
* @description Pre-handler plugin that uses Word-Extractor to convert Buffer containing
* DOC file in `req.body` to TXT.
* a DOC or DOCX file in `req.body` to TXT.
* `req` object is decorated with `conversionResults.body` holding the converted document.
* @param {object} server - Fastify instance.
*/
Expand All @@ -29,7 +29,7 @@ async function plugin(server) {
res.type("text/plain; charset=utf-8");
} catch {
/**
* Word-Extractor will throw if the .doc file provided
* Word-Extractor will throw if the .doc or .docx file provided
* by client is malformed, thus client error code
*/
throw server.httpErrors.badRequest();
Expand Down
60 changes: 42 additions & 18 deletions src/plugins/doc-to-txt/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ describe("DOC-to-TXT conversion plugin", () => {
{ parseAs: "buffer" },
async (_req, payload) => payload
);
server.addContentTypeParser(
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
{ parseAs: "buffer" },
async (_req, payload) => payload
);

await server.register(sensible).register(plugin);

Expand All @@ -31,16 +36,29 @@ describe("DOC-to-TXT conversion plugin", () => {
await server.close();
});

it("Converts DOC file to TXT", async () => {
const response = await server.inject({
method: "POST",
url: "/",
body: await fs.readFile(
"./test_resources/test_files/valid_doc.doc"
),
it.each([
{
testName: "DOC file to TXT",
headers: {
"content-type": "application/msword",
},
readFile: "./test_resources/test_files/valid_doc.doc",
},
{
testName: "DOCX file to TXT",
headers: {
"content-type":
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
},
readFile: "./test_resources/test_files/valid_docx.docx",
},
])("Converts $testName", async ({ headers, readFile }) => {
const response = await server.inject({
method: "POST",
url: "/",
// eslint-disable-next-line security/detect-non-literal-fs-filename
body: await fs.readFile(readFile),
headers,
});

const { body } = JSON.parse(response.payload);
Expand All @@ -66,22 +84,28 @@ describe("DOC-to-TXT conversion plugin", () => {
{ testName: "is missing" },
{
testName: "is not a valid DOC file",
readFile: true,
headers: {
"content-type": "application/msword",
},
readFile: "./test_resources/test_files/invalid_doc.doc",
},
{
testName: "is not a valid DOCX file",
headers: {
"content-type":
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
},
readFile: "./test_resources/test_files/invalid_docx.docx",
},
])(
"Returns HTTP status code 400 if DOC file $testName",
async ({ readFile }) => {
"Returns HTTP status code 400 if file $testName",
async ({ headers, readFile }) => {
const response = await server.inject({
method: "POST",
url: "/",
headers: {
"content-type": "application/msword",
},
body: readFile
? await fs.readFile(
"./test_resources/test_files/invalid_doc.doc"
)
: undefined,
headers,
// eslint-disable-next-line security/detect-non-literal-fs-filename
body: readFile ? await fs.readFile(readFile) : undefined,
});

expect(JSON.parse(response.payload)).toEqual({
Expand Down
44 changes: 0 additions & 44 deletions src/plugins/docx-to-txt/index.js

This file was deleted.

97 changes: 0 additions & 97 deletions src/plugins/docx-to-txt/plugin.test.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/routes/docx/txt/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { fromBuffer } = require("file-type");

// Import plugins
const cors = require("@fastify/cors");
const docxToTxt = require("../../../plugins/docx-to-txt");
const docxToTxt = require("../../../plugins/doc-to-txt");

const { docxToTxtPostSchema } = require("./schema");

Expand Down

0 comments on commit 04f8455

Please sign in to comment.