Skip to content

Commit 3a66818

Browse files
committed
🐛 修复文件系统斜杠问题
1 parent 9ac21dc commit 3a66818

File tree

7 files changed

+63
-52
lines changed

7 files changed

+63
-52
lines changed

pkg/filesystem/baidu/baidu.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import IoC from "@App/app/ioc";
33
import { SystemConfig } from "@App/pkg/config/config";
44
import { AuthVerify } from "../auth";
55
import FileSystem, { File, FileReader, FileWriter } from "../filesystem";
6+
import { joinPath } from "../utils";
67
import { BaiduFileReader, BaiduFileWriter } from "./rw";
78

89
export default class BaiduFileSystem implements FileSystem {
@@ -14,9 +15,6 @@ export default class BaiduFileSystem implements FileSystem {
1415

1516
constructor(path?: string, accessToken?: string) {
1617
this.path = path || "/apps";
17-
if (!this.path.endsWith("/")) {
18-
this.path += "/";
19-
}
2018
this.accessToken = accessToken;
2119
this.systemConfig = IoC.instance(SystemConfig) as SystemConfig;
2220
}
@@ -34,16 +32,18 @@ export default class BaiduFileSystem implements FileSystem {
3432

3533
openDir(path: string): Promise<FileSystem> {
3634
return Promise.resolve(
37-
new BaiduFileSystem(`${this.path}${path}`, this.accessToken)
35+
new BaiduFileSystem(joinPath(this.path, path), this.accessToken)
3836
);
3937
}
4038

4139
create(path: string): Promise<FileWriter> {
42-
return Promise.resolve(new BaiduFileWriter(this, `${this.path}${path}`));
40+
return Promise.resolve(
41+
new BaiduFileWriter(this, joinPath(this.path, path))
42+
);
4343
}
4444

4545
createDir(dir: string): Promise<void> {
46-
dir = dir ? `${this.path}/${dir}` : this.path;
46+
dir = joinPath(this.path, dir);
4747
const urlencoded = new URLSearchParams();
4848
urlencoded.append("path", dir);
4949
urlencoded.append("size", "0");
@@ -94,7 +94,7 @@ export default class BaiduFileSystem implements FileSystem {
9494
}
9595

9696
delete(path: string): Promise<void> {
97-
const filelist = [`${this.path}/${path}`];
97+
const filelist = [joinPath(this.path, path)];
9898
return this.request(
9999
`https://pan.baidu.com/rest/2.0/xpan/file?method=filemanager&access_token=${this.accessToken}&opera=delete`,
100100
{
@@ -128,7 +128,7 @@ export default class BaiduFileSystem implements FileSystem {
128128
list.push({
129129
fsid: val.fs_id,
130130
name: val.server_filename,
131-
path: val.path.substring(0, val.path.length - val.server_filename),
131+
path: this.path,
132132
size: val.size,
133133
digest: val.md5,
134134
createtime: val.server_ctime * 1000,

pkg/filesystem/onedrive/onedrive.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import IoC from "@App/app/ioc";
33
import { SystemConfig } from "@App/pkg/config/config";
44
import { AuthVerify } from "../auth";
55
import FileSystem, { File, FileReader, FileWriter } from "../filesystem";
6+
import { joinPath } from "../utils";
67
import { OneDriveFileReader, OneDriveFileWriter } from "./rw";
78

89
export default class OneDriveFileSystem implements FileSystem {
@@ -13,13 +14,7 @@ export default class OneDriveFileSystem implements FileSystem {
1314
systemConfig: SystemConfig;
1415

1516
constructor(path?: string, accessToken?: string) {
16-
this.path = path || "";
17-
if (!this.path.startsWith("/")) {
18-
this.path = `/${this.path}`;
19-
}
20-
if (this.path === "/") {
21-
this.path = "";
22-
}
17+
this.path = path || "/";
2318
this.accessToken = accessToken;
2419
this.systemConfig = IoC.instance(SystemConfig) as SystemConfig;
2520
}
@@ -39,13 +34,13 @@ export default class OneDriveFileSystem implements FileSystem {
3934
path = path.substring(9);
4035
}
4136
return Promise.resolve(
42-
new OneDriveFileSystem(`${this.path}${path}`, this.accessToken)
37+
new OneDriveFileSystem(joinPath(this.path, path), this.accessToken)
4338
);
4439
}
4540

4641
create(path: string): Promise<FileWriter> {
4742
return Promise.resolve(
48-
new OneDriveFileWriter(this, `${this.path}/${path}`)
43+
new OneDriveFileWriter(this, joinPath(this.path, path))
4944
);
5045
}
5146

@@ -59,7 +54,7 @@ export default class OneDriveFileSystem implements FileSystem {
5954
if (!dir) {
6055
return Promise.resolve();
6156
}
62-
dir = dir ? `${this.path}/${dir}` : this.path;
57+
dir = joinPath(this.path, dir);
6358
const dirs = dir.split("/");
6459
let parent = "";
6560
if (dirs.length > 2) {
@@ -111,7 +106,10 @@ export default class OneDriveFileSystem implements FileSystem {
111106

112107
delete(path: string): Promise<void> {
113108
return this.request(
114-
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${this.path}${path}`,
109+
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${joinPath(
110+
this.path,
111+
path
112+
)}`,
115113
{
116114
method: "DELETE",
117115
},
@@ -139,7 +137,6 @@ export default class OneDriveFileSystem implements FileSystem {
139137
updatetime: new Date(val.lastModifiedDateTime).getTime(),
140138
});
141139
});
142-
console.log(list);
143140
return list;
144141
});
145142
}

pkg/filesystem/onedrive/rw.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { calculateMd5 } from "@App/pkg/utils/utils";
44
import { MD5 } from "crypto-js";
55
import { File, FileReader, FileWriter } from "../filesystem";
6+
import { joinPath } from "../utils";
67
import OneDriveFileSystem from "./onedrive";
78

89
export class OneDriveFileReader implements FileReader {
@@ -17,7 +18,10 @@ export class OneDriveFileReader implements FileReader {
1718

1819
async read(type?: "string" | "blob"): Promise<string | Blob> {
1920
const data = await this.fs.request(
20-
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${this.file.path}/${this.file.name}:/content`,
21+
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${joinPath(
22+
this.file.path,
23+
this.file.name
24+
)}:/content`,
2125
{},
2226
true
2327
);

pkg/filesystem/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* eslint-disable import/prefer-default-export */
2+
3+
export function joinPath(...paths: string[]): string {
4+
let path = "";
5+
paths.forEach((value) => {
6+
if (!value) {
7+
return;
8+
}
9+
if (!value.startsWith("/")) {
10+
value = `/${value}`;
11+
}
12+
if (value.endsWith("/")) {
13+
value = value.substring(0, value.length - 1);
14+
}
15+
path += value;
16+
});
17+
return path;
18+
}

pkg/filesystem/webdav/webdav.ts

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { AuthType, createClient, FileStat, WebDAVClient } from "webdav/web";
22
import FileSystem, { File, FileReader, FileWriter } from "../filesystem";
3+
import { joinPath } from "../utils";
34
import { WebDAVFileReader, WebDAVFileWriter } from "./rw";
45

56
export default class WebDAVFileSystem implements FileSystem {
67
client: WebDAVClient;
78

8-
basePath: string = "";
9+
basePath: string = "/";
910

1011
constructor(
1112
authType: AuthType | WebDAVClient,
@@ -15,10 +16,7 @@ export default class WebDAVFileSystem implements FileSystem {
1516
) {
1617
if (typeof authType === "object") {
1718
this.client = authType;
18-
this.basePath = url || "";
19-
if (!this.basePath.endsWith("/")) {
20-
this.basePath += "/";
21-
}
19+
this.basePath = joinPath(url || "");
2220
} else {
2321
this.client = createClient(url!, {
2422
authType,
@@ -34,37 +32,34 @@ export default class WebDAVFileSystem implements FileSystem {
3432
}
3533

3634
open(file: File): Promise<FileReader> {
37-
const path = file.name;
3835
return Promise.resolve(
39-
new WebDAVFileReader(this.client, this.getPath(path))
36+
new WebDAVFileReader(this.client, joinPath(file.path, file.name))
4037
);
4138
}
4239

4340
openDir(path: string): Promise<FileSystem> {
44-
return Promise.resolve(new WebDAVFileSystem(this.client, path));
41+
return Promise.resolve(
42+
new WebDAVFileSystem(this.client, joinPath(this.basePath, path))
43+
);
4544
}
4645

4746
create(path: string): Promise<FileWriter> {
4847
return Promise.resolve(
49-
new WebDAVFileWriter(this.client, this.getPath(path))
48+
new WebDAVFileWriter(this.client, joinPath(this.basePath, path))
5049
);
5150
}
5251

5352
createDir(path: string): Promise<void> {
54-
return this.client.createDirectory(this.getPath(path));
53+
return this.client.createDirectory(joinPath(this.basePath, path));
5554
}
5655

5756
async delete(path: string): Promise<void> {
58-
return this.client.deleteFile(this.getPath(path));
59-
}
60-
61-
getPath(path: string): string {
62-
return this.basePath + path;
57+
return this.client.deleteFile(joinPath(this.basePath, path));
6358
}
6459

6560
async list(): Promise<File[]> {
6661
const dir = (await this.client.getDirectoryContents(
67-
this.getPath(this.basePath)
62+
this.basePath
6863
)) as FileStat[];
6964
const ret: File[] = [];
7065
dir.forEach((item: FileStat) => {
@@ -73,10 +68,7 @@ export default class WebDAVFileSystem implements FileSystem {
7368
}
7469
ret.push({
7570
name: item.basename,
76-
path: item.filename.substring(
77-
0,
78-
item.filename.length - item.basename.length
79-
),
71+
path: this.basePath,
8072
digest: item.etag || "",
8173
size: item.size,
8274
createtime: new Date(item.lastmod).getTime(),

src/app/const.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
export const ExtVersion = "0.11.0-beta.3";
22

3-
export const ExtServer =
4-
process.env.NODE_ENV === "development"
5-
? "http://localhost:8080/"
6-
: "https://ext.scriptcat.org/";
3+
export const ExtServer = "https://ext.scriptcat.org/";
74

85
export const ExternalWhitelist = [
96
"greasyfork.org",

src/runtime/background/gm_api.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import IoC from "@App/app/ioc";
1515
import { SystemConfig } from "@App/pkg/config/config";
1616
import FileSystemFactory from "@Pkg/filesystem/factory";
1717
import FileSystem from "@Pkg/filesystem/filesystem";
18+
import { joinPath } from "@Pkg/filesystem/utils";
1819
import PermissionVerify, {
1920
ConfirmParam,
2021
IPermissionVerify,
@@ -788,7 +789,6 @@ export default class GMApi {
788789
});
789790
}
790791

791-
// TODO: GM_registerMenuCommand
792792
@PermissionVerify.API()
793793
GM_registerMenuCommand(request: Request, channel: Channel) {
794794
GMApi.hook.trigger("registerMenu", request, channel);
@@ -842,16 +842,19 @@ export default class GMApi {
842842
config.params[config.filesystem]
843843
);
844844
await FileSystemFactory.mkdirAll(fs, baseDir);
845+
fs = await fs.openDir(baseDir);
845846
} catch (e: any) {
846847
return channel.throw({ code: 2, error: e.message });
847848
}
848849
switch (action) {
849850
case "list":
850-
fs.list(`${baseDir}/${details.path}`)
851+
fs.list()
851852
.then((list) => {
852853
list.forEach((file) => {
853854
(<any>file).absPath = file.path;
854-
file.path = file.path.substring(baseDir.length + 1);
855+
file.path = joinPath(
856+
file.path.substring(file.path.indexOf(baseDir) + baseDir.length)
857+
);
855858
});
856859
channel.send({ action: "onload", data: list });
857860
channel.disChannel();
@@ -862,7 +865,7 @@ export default class GMApi {
862865
break;
863866
case "upload":
864867
// eslint-disable-next-line no-case-declarations
865-
const w = await fs.create(`${baseDir}/${details.path}`);
868+
const w = await fs.create(details.path);
866869
w.write(await (await fetch(<string>details.data)).blob())
867870
.then(() => {
868871
channel.send({ action: "onload", data: true });
@@ -875,12 +878,12 @@ export default class GMApi {
875878
case "download":
876879
// eslint-disable-next-line no-case-declarations, no-undef
877880
const info = <CATType.FileStorageFileInfo>details.file;
878-
fs = await fs.openDir(`${baseDir}${info.path}`);
881+
fs = await fs.openDir(`${info.path}`);
879882
// eslint-disable-next-line no-case-declarations
880883
const r = await fs.open({
881884
fsid: (<any>info).fsid,
882885
name: info.name,
883-
path: `${baseDir}/${info.path}`,
886+
path: info.absPath,
884887
size: info.size,
885888
digest: info.digest,
886889
createtime: info.createtime,
@@ -900,7 +903,7 @@ export default class GMApi {
900903
});
901904
break;
902905
case "delete":
903-
fs.delete(`${baseDir}/${details.path}`)
906+
fs.delete(`${details.path}`)
904907
.then(() => {
905908
channel.send({ action: "onload", data: true });
906909
channel.disChannel();

0 commit comments

Comments
 (0)