Skip to content

Commit b96f0ba

Browse files
Replace filenamify with safeFilenamify to fix wrong filename handling
1 parent 846f719 commit b96f0ba

11 files changed

Lines changed: 54 additions & 58 deletions

File tree

tests/mocks/handlers/series/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import filenamify from "filenamify";
21
import fs from "fs";
32
import path from "path";
43
import { http, HttpHandler, HttpResponse } from "msw";
5-
import { getArchiveDataDir } from "../../scripts/utils.mjs";
4+
import { getArchiveDataDir, safeFilenamify } from "../../scripts/utils.mjs";
65

76
export default http.all(
87
"https://archiveofourown.org/series/:series_id",
@@ -11,7 +10,7 @@ export default http.all(
1110
path.resolve(
1211
getArchiveDataDir(),
1312
"series",
14-
filenamify(params.series_id as string),
13+
safeFilenamify(params.series_id as string),
1514
"index.html"
1615
)
1716
);

tests/mocks/handlers/tags/feed.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { fileURLToPath } from "url";
2-
import filenamify from "filenamify";
31
import fs from "fs";
42
import path from "path";
53
import { http, HttpHandler, HttpResponse } from "msw";
6-
import { getArchiveDataDir } from "../../scripts/utils.mjs";
4+
import { getArchiveDataDir, safeFilenamify } from "../../scripts/utils.mjs";
75

86
export default http.all(
97
"https://archiveofourown.org/tags/:name/feed.atom",
@@ -12,7 +10,7 @@ export default http.all(
1210
path.resolve(
1311
getArchiveDataDir(),
1412
"tags",
15-
filenamify(params.name as string),
13+
safeFilenamify(params.name as string),
1614
"feed.atom"
1715
)
1816
);

tests/mocks/handlers/tags/name.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { fileURLToPath } from "url";
2-
import filenamify from "filenamify";
31
import fs from "fs";
42
import path from "path";
53
import { http, HttpHandler, HttpResponse } from "msw";
6-
import { getArchiveDataDir } from "../../scripts/utils.mjs";
4+
import { getArchiveDataDir, safeFilenamify } from "../../scripts/utils.mjs";
75

86
export default http.all(
97
"https://archiveofourown.org/tags/:name",
@@ -12,7 +10,7 @@ export default http.all(
1210
path.resolve(
1311
getArchiveDataDir(),
1412
"tags",
15-
filenamify(params.name as string),
13+
safeFilenamify(params.name as string),
1614
"index.html"
1715
)
1816
);

tests/mocks/handlers/tags/works.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { fileURLToPath } from "url";
2-
import filenamify from "filenamify";
31
import fs from "fs";
42
import path from "path";
53
import { http, HttpHandler, HttpResponse } from "msw";
6-
import { getArchiveDataDir } from "../../scripts/utils.mjs";
4+
import { getArchiveDataDir, safeFilenamify } from "../../scripts/utils.mjs";
75

86
export default http.all(
97
"https://archiveofourown.org/tags/:name/works",
@@ -12,7 +10,7 @@ export default http.all(
1210
path.resolve(
1311
getArchiveDataDir(),
1412
"tags",
15-
filenamify(params.name as string),
13+
safeFilenamify(params.name as string),
1614
"works.html"
1715
)
1816
);

tests/mocks/handlers/users/profile.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { fileURLToPath } from "url";
2-
import filenamify from "filenamify";
31
import fs from "fs";
42
import path from "path";
53
import { http, HttpHandler, HttpResponse } from "msw";
6-
import { getArchiveDataDir } from "../../scripts/utils.mjs";
4+
import { getArchiveDataDir, safeFilenamify } from "../../scripts/utils.mjs";
75

86
export default http.all(
97
"https://archiveofourown.org/users/:name/profile",
@@ -12,7 +10,7 @@ export default http.all(
1210
path.resolve(
1311
getArchiveDataDir(),
1412
"users",
15-
filenamify(params.name as string),
13+
safeFilenamify(params.name as string),
1614
"profile",
1715
"index.html"
1816
)

tests/mocks/handlers/works/chapter.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { fileURLToPath } from "url";
2-
import filenamify from "filenamify";
31
import fs from "fs";
42
import path from "path";
53
import { http, HttpHandler, HttpResponse } from "msw";
6-
import { getArchiveDataDir } from "../../scripts/utils.mjs";
4+
import { getArchiveDataDir, safeFilenamify } from "../../scripts/utils.mjs";
75

86
export default http.all(
97
"https://archiveofourown.org/works/:work_id/chapters/:chapter_id",
@@ -12,9 +10,9 @@ export default http.all(
1210
path.resolve(
1311
getArchiveDataDir(),
1412
"works",
15-
filenamify(params.work_id as string),
13+
safeFilenamify(params.work_id as string),
1614
"chapters",
17-
`${filenamify((params.chapter_id as string) || "index")}.html`
15+
`${safeFilenamify((params.chapter_id as string) || "index")}.html`
1816
)
1917
);
2018

tests/mocks/handlers/works/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import { fileURLToPath } from "url";
2-
import filenamify from "filenamify";
31
import fs from "fs";
42
import path from "path";
53
import { http, HttpHandler, HttpResponse } from "msw";
6-
import { getArchiveDataDir } from "../../scripts/utils.mjs";
4+
import { getArchiveDataDir, safeFilenamify } from "../../scripts/utils.mjs";
75

86
export default [
97
http.all("https://archiveofourown.org/works/:work_id", ({ params }) => {
108
const html = fs.readFileSync(
119
path.resolve(
1210
getArchiveDataDir(),
1311
"works",
14-
filenamify(params.work_id as string),
12+
safeFilenamify(params.work_id as string),
1513
"index.html"
1614
)
1715
);
@@ -24,7 +22,7 @@ export default [
2422
path.resolve(
2523
getArchiveDataDir("superlove"),
2624
"works",
27-
filenamify(params.work_id as string),
25+
safeFilenamify(params.work_id as string),
2826
"index.html"
2927
)
3028
);

tests/mocks/handlers/works/navigate.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import { fileURLToPath } from "url";
2-
import filenamify from "filenamify";
31
import fs from "fs";
42
import path from "path";
53
import { http, HttpHandler, HttpResponse } from "msw";
6-
import { getArchiveDataDir } from "../../scripts/utils.mjs";
4+
import { getArchiveDataDir, safeFilenamify } from "../../scripts/utils.mjs";
75

86
export default http.all(
97
"https://archiveofourown.org/works/:work_id/navigate",
@@ -12,7 +10,7 @@ export default http.all(
1210
path.resolve(
1311
getArchiveDataDir(),
1412
"works",
15-
filenamify(params.work_id as string),
13+
safeFilenamify(params.work_id as string),
1614
"navigate.html"
1715
)
1816
);

tests/mocks/scripts/encode-mock-files.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import dirTree from "directory-tree";
22
import path, { dirname } from "path";
33
import { fileURLToPath } from "url";
44
import { renameSync } from "fs";
5-
import filenamify from "filenamify";
5+
import { safeFilenamify } from "./utils.mjs";
66

77
const hasInvalidFileCharacters = (filename: string) => {
8-
return filenamify(filename) != filename;
8+
return safeFilenamify(filename) != filename;
99
};
1010

1111
const recursivelyRenameDirectories = (folder: ReturnType<typeof dirTree>) => {
@@ -26,12 +26,12 @@ const recursivelyRenameDirectories = (folder: ReturnType<typeof dirTree>) => {
2626
console.log(
2727
`${path.join(containingDir, folder.name)} => ${path.join(
2828
containingDir,
29-
filenamify(folder.name)
29+
safeFilenamify(folder.name)
3030
)}`
3131
);
3232
renameSync(
3333
path.join(containingDir, folder.name),
34-
path.join(containingDir, filenamify(folder.name))
34+
path.join(containingDir, safeFilenamify(folder.name))
3535
);
3636
}
3737
};

tests/mocks/scripts/redownload-articles.mts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const KNOWN_404 = [
77
]
88

99
import {
10+
decodeFilename,
1011
delay,
1112
downloadWithRetry,
1213
recursivelyGetFiles,
@@ -16,16 +17,6 @@ import {
1617
Http404Error,
1718
} from "./utils.mts";
1819

19-
// Function to decode an encoded filename
20-
// TODO: write a better description
21-
function decodeFilename(encodedName: string): string {
22-
return encodedName
23-
.replace(/!d!/g, ".") // Period
24-
.replace(/!a!/g, "&") // Ampersand
25-
.replace(/!s!/g, "/") // Forward slash
26-
.replace(/\*a\*/g, "&"); // Alternative ampersand encoding
27-
}
28-
2920
function getUrlFromPath(
3021
relativePath: string,
3122
archive: "ao3" | "superlove"

0 commit comments

Comments
 (0)