Skip to content

Commit

Permalink
🐛 修复文件系统斜杠问题
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Dec 18, 2022
1 parent 9ac21dc commit 3a66818
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 52 deletions.
16 changes: 8 additions & 8 deletions pkg/filesystem/baidu/baidu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import IoC from "@App/app/ioc";
import { SystemConfig } from "@App/pkg/config/config";
import { AuthVerify } from "../auth";
import FileSystem, { File, FileReader, FileWriter } from "../filesystem";
import { joinPath } from "../utils";
import { BaiduFileReader, BaiduFileWriter } from "./rw";

export default class BaiduFileSystem implements FileSystem {
Expand All @@ -14,9 +15,6 @@ export default class BaiduFileSystem implements FileSystem {

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

openDir(path: string): Promise<FileSystem> {
return Promise.resolve(
new BaiduFileSystem(`${this.path}${path}`, this.accessToken)
new BaiduFileSystem(joinPath(this.path, path), this.accessToken)
);
}

create(path: string): Promise<FileWriter> {
return Promise.resolve(new BaiduFileWriter(this, `${this.path}${path}`));
return Promise.resolve(
new BaiduFileWriter(this, joinPath(this.path, path))
);
}

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

delete(path: string): Promise<void> {
const filelist = [`${this.path}/${path}`];
const filelist = [joinPath(this.path, path)];
return this.request(
`https://pan.baidu.com/rest/2.0/xpan/file?method=filemanager&access_token=${this.accessToken}&opera=delete`,
{
Expand Down Expand Up @@ -128,7 +128,7 @@ export default class BaiduFileSystem implements FileSystem {
list.push({
fsid: val.fs_id,
name: val.server_filename,
path: val.path.substring(0, val.path.length - val.server_filename),
path: this.path,
size: val.size,
digest: val.md5,
createtime: val.server_ctime * 1000,
Expand Down
21 changes: 9 additions & 12 deletions pkg/filesystem/onedrive/onedrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import IoC from "@App/app/ioc";
import { SystemConfig } from "@App/pkg/config/config";
import { AuthVerify } from "../auth";
import FileSystem, { File, FileReader, FileWriter } from "../filesystem";
import { joinPath } from "../utils";
import { OneDriveFileReader, OneDriveFileWriter } from "./rw";

export default class OneDriveFileSystem implements FileSystem {
Expand All @@ -13,13 +14,7 @@ export default class OneDriveFileSystem implements FileSystem {
systemConfig: SystemConfig;

constructor(path?: string, accessToken?: string) {
this.path = path || "";
if (!this.path.startsWith("/")) {
this.path = `/${this.path}`;
}
if (this.path === "/") {
this.path = "";
}
this.path = path || "/";
this.accessToken = accessToken;
this.systemConfig = IoC.instance(SystemConfig) as SystemConfig;
}
Expand All @@ -39,13 +34,13 @@ export default class OneDriveFileSystem implements FileSystem {
path = path.substring(9);
}
return Promise.resolve(
new OneDriveFileSystem(`${this.path}${path}`, this.accessToken)
new OneDriveFileSystem(joinPath(this.path, path), this.accessToken)
);
}

create(path: string): Promise<FileWriter> {
return Promise.resolve(
new OneDriveFileWriter(this, `${this.path}/${path}`)
new OneDriveFileWriter(this, joinPath(this.path, path))
);
}

Expand All @@ -59,7 +54,7 @@ export default class OneDriveFileSystem implements FileSystem {
if (!dir) {
return Promise.resolve();
}
dir = dir ? `${this.path}/${dir}` : this.path;
dir = joinPath(this.path, dir);
const dirs = dir.split("/");
let parent = "";
if (dirs.length > 2) {
Expand Down Expand Up @@ -111,7 +106,10 @@ export default class OneDriveFileSystem implements FileSystem {

delete(path: string): Promise<void> {
return this.request(
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${this.path}${path}`,
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${joinPath(
this.path,
path
)}`,
{
method: "DELETE",
},
Expand Down Expand Up @@ -139,7 +137,6 @@ export default class OneDriveFileSystem implements FileSystem {
updatetime: new Date(val.lastModifiedDateTime).getTime(),
});
});
console.log(list);
return list;
});
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/filesystem/onedrive/rw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { calculateMd5 } from "@App/pkg/utils/utils";
import { MD5 } from "crypto-js";
import { File, FileReader, FileWriter } from "../filesystem";
import { joinPath } from "../utils";
import OneDriveFileSystem from "./onedrive";

export class OneDriveFileReader implements FileReader {
Expand All @@ -17,7 +18,10 @@ export class OneDriveFileReader implements FileReader {

async read(type?: "string" | "blob"): Promise<string | Blob> {
const data = await this.fs.request(
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${this.file.path}/${this.file.name}:/content`,
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${joinPath(
this.file.path,
this.file.name
)}:/content`,
{},
true
);
Expand Down
18 changes: 18 additions & 0 deletions pkg/filesystem/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable import/prefer-default-export */

export function joinPath(...paths: string[]): string {
let path = "";
paths.forEach((value) => {
if (!value) {
return;
}
if (!value.startsWith("/")) {
value = `/${value}`;
}
if (value.endsWith("/")) {
value = value.substring(0, value.length - 1);
}
path += value;
});
return path;
}
32 changes: 12 additions & 20 deletions pkg/filesystem/webdav/webdav.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { AuthType, createClient, FileStat, WebDAVClient } from "webdav/web";
import FileSystem, { File, FileReader, FileWriter } from "../filesystem";
import { joinPath } from "../utils";
import { WebDAVFileReader, WebDAVFileWriter } from "./rw";

export default class WebDAVFileSystem implements FileSystem {
client: WebDAVClient;

basePath: string = "";
basePath: string = "/";

constructor(
authType: AuthType | WebDAVClient,
Expand All @@ -15,10 +16,7 @@ export default class WebDAVFileSystem implements FileSystem {
) {
if (typeof authType === "object") {
this.client = authType;
this.basePath = url || "";
if (!this.basePath.endsWith("/")) {
this.basePath += "/";
}
this.basePath = joinPath(url || "");
} else {
this.client = createClient(url!, {
authType,
Expand All @@ -34,37 +32,34 @@ export default class WebDAVFileSystem implements FileSystem {
}

open(file: File): Promise<FileReader> {
const path = file.name;
return Promise.resolve(
new WebDAVFileReader(this.client, this.getPath(path))
new WebDAVFileReader(this.client, joinPath(file.path, file.name))
);
}

openDir(path: string): Promise<FileSystem> {
return Promise.resolve(new WebDAVFileSystem(this.client, path));
return Promise.resolve(
new WebDAVFileSystem(this.client, joinPath(this.basePath, path))
);
}

create(path: string): Promise<FileWriter> {
return Promise.resolve(
new WebDAVFileWriter(this.client, this.getPath(path))
new WebDAVFileWriter(this.client, joinPath(this.basePath, path))
);
}

createDir(path: string): Promise<void> {
return this.client.createDirectory(this.getPath(path));
return this.client.createDirectory(joinPath(this.basePath, path));
}

async delete(path: string): Promise<void> {
return this.client.deleteFile(this.getPath(path));
}

getPath(path: string): string {
return this.basePath + path;
return this.client.deleteFile(joinPath(this.basePath, path));
}

async list(): Promise<File[]> {
const dir = (await this.client.getDirectoryContents(
this.getPath(this.basePath)
this.basePath
)) as FileStat[];
const ret: File[] = [];
dir.forEach((item: FileStat) => {
Expand All @@ -73,10 +68,7 @@ export default class WebDAVFileSystem implements FileSystem {
}
ret.push({
name: item.basename,
path: item.filename.substring(
0,
item.filename.length - item.basename.length
),
path: this.basePath,
digest: item.etag || "",
size: item.size,
createtime: new Date(item.lastmod).getTime(),
Expand Down
5 changes: 1 addition & 4 deletions src/app/const.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
export const ExtVersion = "0.11.0-beta.3";

export const ExtServer =
process.env.NODE_ENV === "development"
? "http://localhost:8080/"
: "https://ext.scriptcat.org/";
export const ExtServer = "https://ext.scriptcat.org/";

export const ExternalWhitelist = [
"greasyfork.org",
Expand Down
17 changes: 10 additions & 7 deletions src/runtime/background/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import IoC from "@App/app/ioc";
import { SystemConfig } from "@App/pkg/config/config";
import FileSystemFactory from "@Pkg/filesystem/factory";
import FileSystem from "@Pkg/filesystem/filesystem";
import { joinPath } from "@Pkg/filesystem/utils";
import PermissionVerify, {
ConfirmParam,
IPermissionVerify,
Expand Down Expand Up @@ -788,7 +789,6 @@ export default class GMApi {
});
}

// TODO: GM_registerMenuCommand
@PermissionVerify.API()
GM_registerMenuCommand(request: Request, channel: Channel) {
GMApi.hook.trigger("registerMenu", request, channel);
Expand Down Expand Up @@ -842,16 +842,19 @@ export default class GMApi {
config.params[config.filesystem]
);
await FileSystemFactory.mkdirAll(fs, baseDir);
fs = await fs.openDir(baseDir);
} catch (e: any) {
return channel.throw({ code: 2, error: e.message });
}
switch (action) {
case "list":
fs.list(`${baseDir}/${details.path}`)
fs.list()
.then((list) => {
list.forEach((file) => {
(<any>file).absPath = file.path;
file.path = file.path.substring(baseDir.length + 1);
file.path = joinPath(
file.path.substring(file.path.indexOf(baseDir) + baseDir.length)
);
});
channel.send({ action: "onload", data: list });
channel.disChannel();
Expand All @@ -862,7 +865,7 @@ export default class GMApi {
break;
case "upload":
// eslint-disable-next-line no-case-declarations
const w = await fs.create(`${baseDir}/${details.path}`);
const w = await fs.create(details.path);
w.write(await (await fetch(<string>details.data)).blob())
.then(() => {
channel.send({ action: "onload", data: true });
Expand All @@ -875,12 +878,12 @@ export default class GMApi {
case "download":
// eslint-disable-next-line no-case-declarations, no-undef
const info = <CATType.FileStorageFileInfo>details.file;
fs = await fs.openDir(`${baseDir}${info.path}`);
fs = await fs.openDir(`${info.path}`);
// eslint-disable-next-line no-case-declarations
const r = await fs.open({
fsid: (<any>info).fsid,
name: info.name,
path: `${baseDir}/${info.path}`,
path: info.absPath,
size: info.size,
digest: info.digest,
createtime: info.createtime,
Expand All @@ -900,7 +903,7 @@ export default class GMApi {
});
break;
case "delete":
fs.delete(`${baseDir}/${details.path}`)
fs.delete(`${details.path}`)
.then(() => {
channel.send({ action: "onload", data: true });
channel.disChannel();
Expand Down

0 comments on commit 3a66818

Please sign in to comment.