Skip to content

Commit b031159

Browse files
authored
fix: file loading fails when path contains URL-encoded characters (#192)
1 parent 9faa7b4 commit b031159

5 files changed

Lines changed: 25 additions & 10 deletions

File tree

‎src/base/paths.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ export function basename(path: string | URL) {
1818
return path.split("/").at(-1)!;
1919
}
2020

21-
export function extension(path: string) {
22-
return path.split(".").at(-1) ?? "";
21+
export function extension(path: string): string {
22+
const res = path.split(".");
23+
if (res.length <= 1) {
24+
return "";
25+
}
26+
return res.at(-1)!;
2327
}
2428

2529
/**

‎src/kicanvas/services/codeberg-vfs.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export class CodebergFileSystem extends FileSystemBase {
6565
it.type === "file" &&
6666
CodebergFileSystem.is_kicad_file(it.name)
6767
) {
68-
const file_path = based_on(base_dir, it.path);
68+
const path = decodeURI(it.path);
69+
const file_path = based_on(base_dir, path);
6970

7071
this.download_urls.set(file_path, new URL(it.git_url));
7172

@@ -74,9 +75,12 @@ export class CodebergFileSystem extends FileSystemBase {
7475
path: file_path,
7576
});
7677
} else if (it.type === "dir") {
78+
const path = decodeURI(it.path);
79+
const dir_path = based_on(base_dir, path);
80+
7781
result.push({
7882
type: "directory",
79-
path: based_on(base_dir, it.path),
83+
path: dir_path,
8084
});
8185
}
8286
}

‎src/kicanvas/services/codeberg.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class Codeberg {
4141
return null;
4242
}
4343

44-
const path_parts = url.pathname.split("/");
44+
const path_parts = url.pathname.split("/").map((s) => decodeURI(s));
4545

4646
if (path_parts.length < 3) {
4747
return null;

‎src/kicanvas/services/github-vfs.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ export class GitHubFileSystem extends FileSystemBase {
7373
const result: FileEntry[] = [];
7474
for (const it of contents) {
7575
if (it.type === "file" && GitHubFileSystem.is_kicad_file(it.name)) {
76-
const file_path = based_on(base_dir, it.path);
76+
const path = decodeURI(it.path);
77+
const file_path = based_on(base_dir, path);
7778

7879
this.download_urls.set(file_path, new URL(it.download_url));
7980

@@ -82,9 +83,12 @@ export class GitHubFileSystem extends FileSystemBase {
8283
path: file_path,
8384
});
8485
} else if (it.type === "dir") {
86+
const path = decodeURI(it.path);
87+
const dir_path = based_on(base_dir, path);
88+
8589
result.push({
8690
type: "directory",
87-
path: based_on(base_dir, it.path),
91+
path: dir_path,
8892
});
8993
}
9094
}
@@ -110,12 +114,15 @@ export class GitHubFileSystem extends FileSystemBase {
110114
// If it's one file just load one file.
111115
let single_file = false;
112116
if (info.type === "blob") {
113-
if (["kicad_sch", "kicad_pcb"].includes(extension(info.path!))) {
117+
const ext_name = extension(info.path!);
118+
if (["kicad_sch", "kicad_pcb"].includes(ext_name)) {
114119
single_file = true;
115120
} else {
116121
// Link to non-kicad file, try using the containing directory.
117122
info.type = "tree";
118-
info.path = dirname(info.path!);
123+
if (ext_name.length !== 0) {
124+
info.path = dirname(info.path!);
125+
}
119126
}
120127
}
121128

‎src/kicanvas/services/github.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class GitHub {
5555
return null;
5656
}
5757

58-
const path_parts = url.pathname.split("/");
58+
const path_parts = url.pathname.split("/").map((s) => decodeURI(s));
5959

6060
if (path_parts.length < 3) {
6161
return null;

0 commit comments

Comments
 (0)