fix: Document title with slashes produces folders in exported zip file

closes #2036
This commit is contained in:
Tom Moor
2021-04-17 19:30:31 -07:00
parent 03d90b3f15
commit e9f083feb8
6 changed files with 68 additions and 3 deletions

View File

@ -2,6 +2,14 @@
import path from "path";
import fs from "fs-extra";
export function serializeFilename(text: string): string {
return text.replace(/\//g, "%2F").replace(/\\/g, "%5C");
}
export function deserializeFilename(text: string): string {
return text.replace(/%2F/g, "/").replace(/%5C/g, "\\");
}
export function requireDirectory<T>(dirName: string): [T, string][] {
return fs
.readdirSync(dirName)

34
server/utils/fs.test.js Normal file
View File

@ -0,0 +1,34 @@
// @flow
import { serializeFilename, deserializeFilename } from "./fs";
describe("serializeFilename", () => {
it("should serialize forward slashes", () => {
expect(serializeFilename(`/`)).toBe("%2F");
expect(serializeFilename(`this / and / this`)).toBe(
"this %2F and %2F this"
);
});
it("should serialize back slashes", () => {
expect(serializeFilename(`\\`)).toBe("%5C");
expect(serializeFilename(`this \\ and \\ this`)).toBe(
"this %5C and %5C this"
);
});
});
describe("deserializeFilename", () => {
it("should deserialize forward slashes", () => {
expect(deserializeFilename("%2F")).toBe("/");
expect(deserializeFilename("this %2F and %2F this")).toBe(
`this / and / this`
);
});
it("should deserialize back slashes", () => {
expect(deserializeFilename("%5C")).toBe(`\\`);
expect(deserializeFilename("this %5C and %5C this")).toBe(
`this \\ and \\ this`
);
});
});

View File

@ -4,6 +4,7 @@ import * as Sentry from "@sentry/node";
import JSZip from "jszip";
import tmp from "tmp";
import { Attachment, Collection, Document } from "../models";
import { serializeFilename } from "./fs";
import { getFileByKey } from "./s3";
async function addToArchive(zip, documents) {
@ -23,7 +24,9 @@ async function addToArchive(zip, documents) {
text = text.replace(attachment.redirectUrl, encodeURI(attachment.key));
}
zip.file(`${document.title || "Untitled"}.md`, text, {
const title = serializeFilename(document.title) || "Untitled";
zip.file(`${title}.md`, text, {
date: document.updatedAt,
comment: JSON.stringify({
pinned: document.pinned,
@ -33,7 +36,7 @@ async function addToArchive(zip, documents) {
});
if (doc.children && doc.children.length) {
const folder = zip.folder(document.title);
const folder = zip.folder(title);
await addToArchive(folder, doc.children);
}
}