Skip to content

Commit

Permalink
fix(plugins/rtf-to-html): rtf images dumped into cwd (#1287)
Browse files Browse the repository at this point in the history
* fix(plugins/rtf-to-html): rtf images dumped into cwd

* chore(plugins/rtf-to-html): duplicate word

* test(plugins/rtf-to-html): cover image removal

* test: test with images across board
  • Loading branch information
Fdawgs authored Mar 2, 2023
1 parent 15f3866 commit d089f68
Show file tree
Hide file tree
Showing 7 changed files with 16,444 additions and 24 deletions.
27 changes: 27 additions & 0 deletions src/plugins/rtf-to-html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ async function plugin(server, options) {
element.innerHTML = id;
dom.window.document.head.prepend(element);

/**
* UnRTF < v0.20.4 ignores `noPictures` option and
* generates img tags whilst placing images in cwd.
*
* UnRTF generates the image name i.e. `pict001.wmf`, `pict002.wmf` and so on.
* This means files can be safely removed from the cwd without running the
* risk of removing any important files such as `package.json`, should an image
* ever have the same name.
*/
const images = dom.window.document.querySelectorAll("img");
if (images.length > 0) {
await Promise.all(
Array.from(images).map((image) => {
const { src } = image;
image.remove();
/**
* `rm()` used instead of `unlink()` because concurrent requests may create duplicate files,
* which could cause `unlink()` to throw an ENOENT error if the file has already been removed
* by another request's call of this hook.
*/
return fs.rm(path.joinSafe(process.cwd(), src), {
force: true,
});
})
);
}

/**
* `fixUtf8` function replaces most common incorrectly converted
* Windows-1252 to UTF-8 results with HTML equivalents.
Expand Down
9 changes: 7 additions & 2 deletions src/plugins/rtf-to-html/plugin.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable security/detect-non-literal-fs-filename */
const fs = require("fs");
const Fastify = require("fastify");
const { JSDOM } = require("jsdom");
const isHtml = require("is-html");
const sensible = require("@fastify/sensible");
const plugin = require(".");
Expand All @@ -21,7 +22,7 @@ describe("RTF-to-HTML conversion plugin", () => {
config = await getConfig();
config.unrtf.tempDir = "./src/temp-test-rtf-to-html/";

server = Fastify();
server = Fastify({ bodyLimit: 10485760 });

server.addContentTypeParser(
"application/rtf",
Expand Down Expand Up @@ -60,12 +61,16 @@ describe("RTF-to-HTML conversion plugin", () => {
});

response = JSON.parse(response.payload);
const dom = new JSDOM(response);

expect(response.body).toEqual(
expect.stringContaining("Ask not what your country can do for you")
expect.stringContaining(
"Etiam vehicula luctus fermentum. In vel metus congue, pulvinar lectus vel, fermentum dui."
)
);
expect(response.body).not.toEqual(expect.stringMatching(artifacts));
expect(isHtml(response.body)).toBe(true);
expect(dom.window.document.querySelectorAll("img")).toHaveLength(0);
expect(response.docLocation).toEqual(
expect.objectContaining({
directory: expect.any(String),
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/rtf-to-txt/plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("RTF-to-TXT conversion plugin", () => {
config = await getConfig();
config.unrtf.tempDir = "./src/temp-test-rtf-to-txt/";

server = Fastify();
server = Fastify({ bodyLimit: 10485760 });

server.addContentTypeParser(
"application/rtf",
Expand Down Expand Up @@ -55,7 +55,9 @@ describe("RTF-to-TXT conversion plugin", () => {
response = JSON.parse(response.payload);

expect(response.body).toEqual(
expect.stringContaining("Ask not what your country can do for you")
expect.stringContaining(
"Etiam vehicula luctus fermentum. In vel metus congue, pulvinar lectus vel, fermentum dui."
)
);
expect(isHtml(response.body)).toBe(false);
expect(response.docLocation).toEqual(
Expand Down
4 changes: 2 additions & 2 deletions src/routes/rtf/html/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("RTF-to-HTML route", () => {
beforeAll(async () => {
config = await getConfig();

server = Fastify();
server = Fastify({ bodyLimit: 10485760 });
await server
.register(accepts)
.register(sensible)
Expand Down Expand Up @@ -60,7 +60,7 @@ describe("RTF-to-HTML route", () => {
.then((response) => {
expect(response.payload).toEqual(
expect.stringContaining(
"Ask not what your country can do for you"
"Etiam vehicula luctus fermentum. In vel metus congue, pulvinar lectus vel, fermentum dui."
)
);
expect(isHtml(response.payload)).toBe(true);
Expand Down
6 changes: 4 additions & 2 deletions src/routes/rtf/txt/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("RTF-to-TXT route", () => {
beforeAll(async () => {
config = await getConfig();

server = Fastify();
server = Fastify({ bodyLimit: 10485760 });
await server
.register(accepts)
.register(sensible)
Expand All @@ -41,7 +41,9 @@ describe("RTF-to-TXT route", () => {
});

expect(response.payload).toEqual(
expect.stringContaining("Ask not what your country can do for you")
expect.stringContaining(
"Etiam vehicula luctus fermentum. In vel metus congue, pulvinar lectus vel, fermentum dui."
)
);
expect(isHtml(response.payload)).toBe(false);
expect(response.headers).toMatchObject({
Expand Down
6 changes: 3 additions & 3 deletions src/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe("Server deployment", () => {
});
config = await getConfig();

server = Fastify({ pluginTimeout: 30000 });
server = Fastify({ bodyLimit: 10485760, pluginTimeout: 30000 });
await server.register(startServer, config).ready();
});

Expand Down Expand Up @@ -306,7 +306,7 @@ describe("Server deployment", () => {

expect(response.payload).toEqual(
expect.stringContaining(
"Ask not what your country can do for you"
"Etiam vehicula luctus fermentum. In vel metus congue, pulvinar lectus vel, fermentum dui."
)
);
expect(isHtml(response.payload)).toBe(true);
Expand All @@ -331,7 +331,7 @@ describe("Server deployment", () => {

expect(response.payload).toEqual(
expect.stringContaining(
"Ask not what your country can do for you"
"Etiam vehicula luctus fermentum. In vel metus congue, pulvinar lectus vel, fermentum dui."
)
);
expect(isHtml(response.payload)).toBe(false);
Expand Down
Loading

0 comments on commit d089f68

Please sign in to comment.