This commit is contained in:
2025-04-15 00:52:23 +08:00
parent b76a685988
commit c7763227d0
24 changed files with 748 additions and 384 deletions

View File

@ -1,7 +1,4 @@
/* eslint-disable camelcase */
/* eslint-disable import/prefer-default-export */
import { ExtServer } from "@App/app/const";
import { api } from "@App/pkg/axios";
import { ExtServer, ExtServerApi } from "@App/app/const";
import { WarpTokenError } from "./error";
type NetDiskType = "baidu" | "onedrive";
@ -11,8 +8,8 @@ export function GetNetDiskToken(netDiskType: NetDiskType): Promise<{
msg: string;
data: { token: { access_token: string; refresh_token: string } };
}> {
return api
.get(`/auth/net-disk/token?netDiskType=${netDiskType}`)
return fetch(ExtServerApi + `auth/net-disk/token?netDiskType=${netDiskType}`)
.then((resp) => resp.json())
.then((resp) => {
return resp.data;
});
@ -26,11 +23,17 @@ export function RefreshToken(
msg: string;
data: { token: { access_token: string; refresh_token: string } };
}> {
return api
.post(`/auth/net-disk/token/refresh?netDiskType=${netDiskType}`, {
return fetch(ExtServerApi + `auth/net-disk/token/refresh?netDiskType=${netDiskType}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
netDiskType,
refreshToken,
})
}),
})
.then((resp) => resp.json())
.then((resp) => {
return resp.data;
});
@ -38,9 +41,7 @@ export function RefreshToken(
export function NetDisk(netDiskType: NetDiskType) {
return new Promise<void>((resolve) => {
const loginWindow = window.open(
`${ExtServer}api/v1/auth/net-disk?netDiskType=${netDiskType}`
);
const loginWindow = window.open(`${ExtServer}api/v1/auth/net-disk?netDiskType=${netDiskType}`);
const t = setInterval(() => {
try {
if (loginWindow!.closed) {

View File

@ -1,5 +1,3 @@
/* eslint-disable no-unused-vars */
import IoC from "@App/app/ioc";
import { SystemConfig } from "@App/pkg/config/config";
import { AuthVerify } from "../auth";
import FileSystem, { File, FileReader, FileWriter } from "../filesystem";
@ -11,12 +9,9 @@ export default class BaiduFileSystem implements FileSystem {
path: string;
systemConfig: SystemConfig;
constructor(path?: string, accessToken?: string) {
this.path = path || "/apps";
this.accessToken = accessToken;
this.systemConfig = IoC.instance(SystemConfig) as SystemConfig;
}
async verify(): Promise<void> {
@ -31,15 +26,11 @@ export default class BaiduFileSystem implements FileSystem {
}
openDir(path: string): Promise<FileSystem> {
return Promise.resolve(
new BaiduFileSystem(joinPath(this.path, path), this.accessToken)
);
return Promise.resolve(new BaiduFileSystem(joinPath(this.path, path), this.accessToken));
}
create(path: string): Promise<FileWriter> {
return Promise.resolve(
new BaiduFileWriter(this, joinPath(this.path, path))
);
return Promise.resolve(new BaiduFileWriter(this, joinPath(this.path, path)));
}
createDir(dir: string): Promise<void> {
@ -51,15 +42,12 @@ export default class BaiduFileSystem implements FileSystem {
urlencoded.append("rtype", "3");
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");
return this.request(
`https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=${this.accessToken}`,
{
method: "POST",
headers: myHeaders,
body: urlencoded,
redirect: "follow",
}
).then((data) => {
return this.request(`https://pan.baidu.com/rest/2.0/xpan/file?method=create&access_token=${this.accessToken}`, {
method: "POST",
headers: myHeaders,
body: urlencoded,
redirect: "follow",
}).then((data) => {
if (data.errno) {
throw new Error(JSON.stringify(data));
}
@ -71,9 +59,23 @@ export default class BaiduFileSystem implements FileSystem {
request(url: string, config?: RequestInit) {
config = config || {};
const headers = <Headers>config.headers || new Headers();
// 利用GM函数的匿名实现不发送cookie,因为某些情况cookie会导致-6错误
headers.append(`${this.systemConfig.scriptCatFlag}-gm-xhr`, "true");
headers.append(`${this.systemConfig.scriptCatFlag}-anonymous`, "true");
// 处理请求匿名不发送cookie
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [100],
addRules: [
{
id: 100,
action: {
type: chrome.declarativeNetRequest.RuleActionType.MODIFY_HEADERS,
responseHeaders: [{ operation: chrome.declarativeNetRequest.HeaderOperation.REMOVE, header: "cookie" }],
},
condition: {
urlFilter: url,
resourceTypes: [chrome.declarativeNetRequest.ResourceType.XMLHTTPREQUEST],
},
},
],
});
config.headers = headers;
return fetch(url, config)
.then((data) => data.json())
@ -92,6 +94,11 @@ export default class BaiduFileSystem implements FileSystem {
});
}
return data;
})
.finally(() => {
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [100],
});
});
}
@ -103,9 +110,7 @@ export default class BaiduFileSystem implements FileSystem {
`https://pan.baidu.com/rest/2.0/xpan/file?method=filemanager&access_token=${this.accessToken}&opera=delete`,
{
method: "POST",
body: `async=0&filelist=${encodeURIComponent(
JSON.stringify(filelist)
)}`,
body: `async=0&filelist=${encodeURIComponent(JSON.stringify(filelist))}`,
headers: myHeaders,
}
).then((data) => {
@ -145,10 +150,6 @@ export default class BaiduFileSystem implements FileSystem {
}
getDirUrl(): Promise<string> {
return Promise.resolve(
`https://pan.baidu.com/disk/main#/index?category=all&path=${encodeURIComponent(
this.path
)}`
);
return Promise.resolve(`https://pan.baidu.com/disk/main#/index?category=all&path=${encodeURIComponent(this.path)}`);
}
}

View File

@ -1,6 +1,3 @@
/* eslint-disable no-unused-vars */
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";
@ -11,12 +8,9 @@ export default class OneDriveFileSystem implements FileSystem {
path: string;
systemConfig: SystemConfig;
constructor(path?: string, accessToken?: string) {
this.path = path || "/";
this.accessToken = accessToken;
this.systemConfig = IoC.instance(SystemConfig) as SystemConfig;
}
async verify(): Promise<void> {
@ -33,15 +27,11 @@ export default class OneDriveFileSystem implements FileSystem {
if (path.startsWith("ScriptCat")) {
path = path.substring(9);
}
return Promise.resolve(
new OneDriveFileSystem(joinPath(this.path, path), this.accessToken)
);
return Promise.resolve(new OneDriveFileSystem(joinPath(this.path, path), this.accessToken));
}
create(path: string): Promise<FileWriter> {
return Promise.resolve(
new OneDriveFileWriter(this, joinPath(this.path, path))
);
return Promise.resolve(new OneDriveFileWriter(this, joinPath(this.path, path)));
}
createDir(dir: string): Promise<void> {
@ -65,18 +55,15 @@ export default class OneDriveFileSystem implements FileSystem {
if (parent !== "") {
parent = `:${parent}:`;
}
return this.request(
`https://graph.microsoft.com/v1.0/me/drive/special/approot${parent}/children`,
{
method: "POST",
headers: myHeaders,
body: JSON.stringify({
name: dirs[dirs.length - 1],
folder: {},
"@microsoft.graph.conflictBehavior": "replace",
}),
}
).then((data: any) => {
return this.request(`https://graph.microsoft.com/v1.0/me/drive/special/approot${parent}/children`, {
method: "POST",
headers: myHeaders,
body: JSON.stringify({
name: dirs[dirs.length - 1],
folder: {},
"@microsoft.graph.conflictBehavior": "replace",
}),
}).then((data: any) => {
if (data.errno) {
throw new Error(JSON.stringify(data));
}
@ -84,7 +71,6 @@ export default class OneDriveFileSystem implements FileSystem {
});
}
// eslint-disable-next-line no-undef
request(url: string, config?: RequestInit, nothen?: boolean) {
config = config || {};
const headers = <Headers>config.headers || new Headers();
@ -121,10 +107,7 @@ export default class OneDriveFileSystem implements FileSystem {
delete(path: string): Promise<void> {
return this.request(
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${joinPath(
this.path,
path
)}`,
`https://graph.microsoft.com/v1.0/me/drive/special/approot:${joinPath(this.path, path)}`,
{
method: "DELETE",
},
@ -144,9 +127,7 @@ export default class OneDriveFileSystem implements FileSystem {
} else {
path = `:${path}:`;
}
return this.request(
`https://graph.microsoft.com/v1.0/me/drive/special/approot${path}/children`
).then((data) => {
return this.request(`https://graph.microsoft.com/v1.0/me/drive/special/approot${path}/children`).then((data) => {
const list: File[] = [];
data.value.forEach((val: any) => {
list.push({

View File

@ -1,6 +1,4 @@
/* eslint-disable max-classes-per-file */
/* eslint-disable import/prefer-default-export */
import { WebDAVClient } from "webdav/web";
import { WebDAVClient } from "webdav";
import { FileReader, FileWriter } from "../filesystem";
export class WebDAVFileReader implements FileReader {
@ -42,10 +40,7 @@ export class WebDAVFileWriter implements FileWriter {
async write(content: string | Blob): Promise<void> {
let resp;
if (content instanceof Blob) {
resp = await this.client.putFileContents(
this.path,
await content.arrayBuffer()
);
resp = await this.client.putFileContents(this.path, await content.arrayBuffer());
} else {
resp = await this.client.putFileContents(this.path, content);
}

View File

@ -1,4 +1,4 @@
import { AuthType, createClient, FileStat, WebDAVClient } from "webdav/web";
import { AuthType, createClient, FileStat, WebDAVClient } from "webdav";
import FileSystem, { File, FileReader, FileWriter } from "../filesystem";
import { joinPath } from "../utils";
import { WebDAVFileReader, WebDAVFileWriter } from "./rw";
@ -11,12 +11,7 @@ export default class WebDAVFileSystem implements FileSystem {
basePath: string = "/";
constructor(
authType: AuthType | WebDAVClient,
url?: string,
username?: string,
password?: string
) {
constructor(authType: AuthType | WebDAVClient, url?: string, username?: string, password?: string) {
if (typeof authType === "object") {
this.client = authType;
this.basePath = joinPath(url || "");
@ -44,28 +39,20 @@ export default class WebDAVFileSystem implements FileSystem {
}
open(file: File): Promise<FileReader> {
return Promise.resolve(
new WebDAVFileReader(this.client, joinPath(file.path, file.name))
);
return Promise.resolve(new WebDAVFileReader(this.client, joinPath(file.path, file.name)));
}
openDir(path: string): Promise<FileSystem> {
return Promise.resolve(
new WebDAVFileSystem(this.client, joinPath(this.basePath, path), this.url)
);
return Promise.resolve(new WebDAVFileSystem(this.client, joinPath(this.basePath, path), this.url));
}
create(path: string): Promise<FileWriter> {
return Promise.resolve(
new WebDAVFileWriter(this.client, joinPath(this.basePath, path))
);
return Promise.resolve(new WebDAVFileWriter(this.client, joinPath(this.basePath, path)));
}
async createDir(path: string): Promise<void> {
try {
return Promise.resolve(
await this.client.createDirectory(joinPath(this.basePath, path))
);
return Promise.resolve(await this.client.createDirectory(joinPath(this.basePath, path)));
} catch (e: any) {
// 如果是405错误,则忽略
if (e.message.includes("405")) {
@ -80,9 +67,7 @@ export default class WebDAVFileSystem implements FileSystem {
}
async list(): Promise<File[]> {
const dir = (await this.client.getDirectoryContents(
this.basePath
)) as FileStat[];
const dir = (await this.client.getDirectoryContents(this.basePath)) as FileStat[];
const ret: File[] = [];
dir.forEach((item: FileStat) => {
if (item.type !== "file") {