Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VirtualHotBar committed May 15, 2024
1 parent 845238e commit cfbe993
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 26 deletions.
4 changes: 3 additions & 1 deletion src/controller/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { reupMount } from "./storage/mount/mount"
import { reupStorage } from "./storage/storage"
import { listenWindow, windowsHide } from "./window"
import { NMConfig } from "../type/config"
import { randomString, restartSelf } from "../utils/utils"
import { randomString, restartSelf, sleep } from "../utils/utils"
import { t } from "i18next"
import { startRclone, stopRclone } from "../utils/rclone/process"
import { getOsInfo } from "../utils/tauri/osInfo"
Expand Down Expand Up @@ -85,6 +85,7 @@ async function reupRcloneVersion() {
async function reupAlistVersion() {
let version = await alist_api_get('/api/admin/setting/get', { key: 'version' })
if (version.code !== 200) {
await sleep(500)
await reupAlistVersion()
return
}
Expand All @@ -98,6 +99,7 @@ function main() {

async function exit(isRestartSelf: boolean = false) {
try {
await saveNmConfig()
await stopRclone()
await stopAlist()
await saveNmConfig()
Expand Down
4 changes: 2 additions & 2 deletions src/controller/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { hooks } from "../../services/hook"
import { rcloneInfo } from "../../services/rclone"
import { FileInfo, StorageList, StorageSpace } from "../../type/rclone/rcloneInfo"
import { ParametersType } from "../../type/defaults"
import { rclone_api_post, rcloneApiHeaders } from "../../utils/rclone/request"
import { getRcloneApiHeaders, rclone_api_post } from "../../utils/rclone/request"
import { alist_api_get, alist_api_post } from "../../utils/alist/request"
import { formatPath } from "../../utils/utils"
import { alistInfo } from "../../services/alist"
Expand Down Expand Up @@ -311,7 +311,7 @@ const uploadFileRequest = (option: RequestOptions, storageName: string, path: st
xhr.onerror = () => onError(xhr);

xhr.open('POST', `${rcloneInfo.endpoint.url}/operations/uploadfile?fs=${convertStoragePath(storageName,undefined,undefined,undefined,true)}&remote=${convertStoragePath(storageName,path,true,true,undefined)}`, true);
xhr.setRequestHeader('Authorization', `Bearer ${rcloneApiHeaders.Authorization}`);
xhr.setRequestHeader('Authorization', `Bearer ${getRcloneApiHeaders().Authorization}`);
xhr.send(formData);
};

Expand Down
1 change: 0 additions & 1 deletion src/page/storage/explorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { useTranslation } from 'react-i18next';
import { copyDir, copyFile, delDir, delFile, filterHideStorage, formatPathRclone, getFileList, mkDir, moveDir, moveFile, uploadFileRequest } from '../../controller/storage/storage';
import { FileInfo } from '../../type/rclone/rcloneInfo';
import { formatSize, getURLSearchParam } from '../../utils/utils';
import { rcloneApiHeaders } from '../../utils/rclone/request';
import { RequestOptions } from '@arco-design/web-react/es/Upload';
import { NoData_module } from '../other/noData';
import { clipListItem } from '../../type/page/storage/explorer';
Expand Down
4 changes: 2 additions & 2 deletions src/services/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ let nmConfig: NMConfig = {
},
framework: {
rclone: {
user: process.env.NODE_ENV != 'development' ? randomString(32) : '',
password: process.env.NODE_ENV != 'development' ? randomString(128) : '',
user: randomString(32),
password: randomString(128),
},
alist: {
user: 'admin',
Expand Down
4 changes: 2 additions & 2 deletions src/utils/alist/process.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { invoke } from "@tauri-apps/api";
import { Command } from "@tauri-apps/api/shell";
import { rcloneInfo } from "../../services/rclone";
import { formatPath, randomString } from "../utils";
import { formatPath, randomString, sleep } from "../utils";
import { alistInfo } from "../../services/alist";
import { homeDir } from "@tauri-apps/api/path";
import { nmConfig, osInfo, roConfig } from "../../services/config";
Expand Down Expand Up @@ -45,7 +45,7 @@ async function startAlist() {
alistInfo.process.child = await alistInfo.process.command.spawn()

while (true) {
await setTimeout(() => { }, 1500);
await sleep(500)
if (await alist_api_ping()&&alistInfo.process.log.includes('start HTTP server')) {
break;
}
Expand Down
8 changes: 4 additions & 4 deletions src/utils/rclone/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { invoke } from "@tauri-apps/api";
import { Command } from "@tauri-apps/api/shell";
import { rcloneInfo } from "../../services/rclone";
import { rclone_api_noop, rclone_api_post } from "./request";
import { formatPath, randomString } from "../utils";
import { formatPath, randomString, sleep } from "../utils";
import { alistInfo } from "../../services/alist";
import { delStorage } from "../../controller/storage/storage";
import { nmConfig, osInfo, roConfig } from "../../services/config";
Expand All @@ -16,7 +16,7 @@ async function startRclone() {
await stopRclone()
}

/* if (process.env.NODE_ENV != 'development') {
/*if (process.env.NODE_ENV != 'development') {
rcloneInfo.endpoint.auth.user = randomString(32)
rcloneInfo.endpoint.auth.pass = randomString(128)
} */
Expand All @@ -31,7 +31,7 @@ async function startRclone() {
`--rc-user=${nmConfig.framework.rclone.user}`,
`--rc-pass=${nmConfig.framework.rclone.password}`,
'--rc-allow-origin=' + window.location.origin || '*',
'--config=' + rcloneDataDir() + '/rclone.conf',
'--config=' +formatPath( rcloneDataDir() + '/rclone.conf', osInfo.osType === 'Windows_NT'),
];

if (nmConfig.framework.rclone.user === '') {
Expand All @@ -52,7 +52,7 @@ async function startRclone() {
rcloneInfo.process.child = await rcloneInfo.process.command.spawn()

while (true) {
await setTimeout(() => { }, 1000);
await sleep(500)
if (await rclone_api_noop()/* &&rcloneInfo.process.log.includes('Serving remote control on') */) {
break;
}
Expand Down
22 changes: 8 additions & 14 deletions src/utils/rclone/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,27 @@ import { Message } from "@arco-design/web-react";
import { rcloneInfo } from "../../services/rclone";
import { nmConfig } from "../../services/config";

let rcloneApiHeaders = {
Authorization: `Basic ${btoa(`${nmConfig.framework.rclone.user}:${nmConfig.framework.rclone.password}`)}`,
'Content-Type': 'application/json'
let getRcloneApiHeaders = () => {
return {
Authorization: `Basic ${btoa(`${nmConfig.framework.rclone.user}:${nmConfig.framework.rclone.password}`)}`,
'Content-Type': 'application/json'
}
};

async function rclone_api_noop(): Promise<boolean> {
try {
return await fetch(rcloneInfo.endpoint.url + '/rc/noop', { method: 'POST' }).then(data => data.ok)
return await fetch(rcloneInfo.endpoint.url + '/rc/noop', { method: 'POST', headers: { Authorization: getRcloneApiHeaders().Authorization } }).then(data => data.ok)
} catch (e) {
console.log(e)
return false;
}
}

function rclone_api_post(path: string, bodyData: object = {}, ignoreError?: boolean) {
// 以 base64 编码的方式来设置账密字符串
const base64Credentials = btoa(`${nmConfig.framework.rclone.user}:${nmConfig.framework.rclone.password}`);

// 定义请求头部,包括授权头部
rcloneApiHeaders = {
Authorization: `Basic ${base64Credentials}`,
'Content-Type': 'application/json'
};

return fetch(rcloneInfo.endpoint.url + path, {
method: 'POST',
headers: rcloneApiHeaders,
headers: getRcloneApiHeaders(),
body: JSON.stringify(bodyData)
}).then((response) => {
if (!response.ok && !ignoreError) {
Expand Down Expand Up @@ -70,4 +64,4 @@ async function printError(error: Response) {
})
} */

export { rclone_api_post, rcloneApiHeaders, rclone_api_noop }
export { rclone_api_post, getRcloneApiHeaders, rclone_api_noop }
5 changes: 5 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,9 @@ export async function showPathInExplorer(path: string, isDir?: boolean) {
return false
}

}


export async function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

0 comments on commit cfbe993

Please sign in to comment.