fix: getStorageInfo快速估算(3s)+后台全量遍历校准; quark-cleanup去kps依赖; QuarkDriver支持回调
This commit is contained in:
@@ -329,7 +329,16 @@ export async function refreshAllStorageInfo(): Promise<void> {
|
||||
try {
|
||||
const { QuarkDriver } = require('./drivers/quark.driver');
|
||||
const driver = new QuarkDriver({ cookie: decrypt(cfg.cookie!), nickname: cfg.nickname });
|
||||
const storage = await driver.getStorageInfo();
|
||||
const storage = await driver.getStorageInfo(
|
||||
decrypt(cfg.cookie!),
|
||||
(fullUsed: string, total: string) => {
|
||||
const db = getDb();
|
||||
db.prepare(
|
||||
`UPDATE cloud_configs SET storage_used = ?, storage_total = ? WHERE id = ?`
|
||||
).run(fullUsed, total, cfg.id);
|
||||
console.log(`[Storage] Background calibration done for quark#${cfg.id}: ${fullUsed} / ${total}`);
|
||||
}
|
||||
);
|
||||
if (storage.totalBytes > 0 || storage.usedBytes > 0) {
|
||||
const db = getDb();
|
||||
db.prepare(
|
||||
|
||||
@@ -21,13 +21,7 @@ const storageCache: { bytes: number; hourBlock: number } = { bytes: 0, hourBlock
|
||||
*/
|
||||
export async function getStorageInfoQuick(cookie: string, fallbackTotal?: string): Promise<{ total: string; totalBytes: number; used: string; usedBytes: number }> {
|
||||
try {
|
||||
const mparam = getMparam(cookie);
|
||||
const params = new URLSearchParams({
|
||||
...getCommonParams(),
|
||||
kps: mparam.kps || '',
|
||||
sign: mparam.sign || '',
|
||||
vcode: mparam.vcode || '',
|
||||
});
|
||||
const params = new URLSearchParams(getCommonParams());
|
||||
const capResponse = await fetch(`${BASE_URL}/1/clouddrive/capacity/detail?${params.toString()}`, {
|
||||
headers: getHeaders(cookie),
|
||||
signal: AbortSignal.timeout(10000),
|
||||
@@ -87,14 +81,22 @@ export async function getStorageInfoQuick(cookie: string, fallbackTotal?: string
|
||||
/**
|
||||
* Get storage info with used space calculation.
|
||||
*/
|
||||
export async function getStorageInfo(cookie: string): Promise<{ used: string; total: string; usedBytes: number; totalBytes: number }> {
|
||||
/**
|
||||
* Fast estimation (root-level files only) + background full traversal.
|
||||
* First call returns quickly; full traversal runs async and updates DB later.
|
||||
* `onBackgroundComplete` is called when traversal finishes.
|
||||
*/
|
||||
export async function getStorageInfo(
|
||||
cookie: string,
|
||||
onBackgroundComplete?: (used: string, total: string) => void
|
||||
): Promise<{ used: string; total: string; usedBytes: number; totalBytes: number }> {
|
||||
try {
|
||||
let totalBytes = 0;
|
||||
const params = new URLSearchParams(getCommonParams());
|
||||
const response = await fetch(`${BASE_URL}/1/clouddrive/capacity/detail?${params.toString()}`, {
|
||||
headers: getHeaders(cookie),
|
||||
signal: AbortSignal.timeout(10000),
|
||||
});
|
||||
let totalBytes = 0;
|
||||
if (response.ok) {
|
||||
const data = await response.json() as any;
|
||||
if (data.status === 200 && data.data) {
|
||||
@@ -106,17 +108,32 @@ export async function getStorageInfo(cookie: string): Promise<{ used: string; to
|
||||
}
|
||||
}
|
||||
|
||||
const usedBytes = await calculateUsedSpace(cookie);
|
||||
const totalFormatted = totalBytes > 0 ? formatBytes(totalBytes) : '-';
|
||||
|
||||
if (totalBytes > 0 || usedBytes > 0) {
|
||||
return {
|
||||
total: totalBytes > 0 ? formatBytes(totalBytes) : '-',
|
||||
used: formatBytes(usedBytes),
|
||||
usedBytes,
|
||||
totalBytes: totalBytes > 0 ? totalBytes : 0,
|
||||
};
|
||||
// Quick estimation: sum root-level files only
|
||||
let quickUsed = 0;
|
||||
try {
|
||||
const rootFiles = await listRootDir(cookie);
|
||||
for (const f of rootFiles) {
|
||||
quickUsed += f.size || 0;
|
||||
}
|
||||
return { used: '0 B', total: '-', usedBytes: 0, totalBytes: 0 };
|
||||
} catch {}
|
||||
|
||||
// Budget full traversal in background (no await)
|
||||
calculateUsedSpace(cookie).then(fullUsed => {
|
||||
if (onBackgroundComplete) {
|
||||
onBackgroundComplete(formatBytes(fullUsed), totalFormatted);
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error('[Storage] Background full traversal failed:', err.message);
|
||||
});
|
||||
|
||||
return {
|
||||
total: totalFormatted,
|
||||
totalBytes,
|
||||
used: formatBytes(quickUsed),
|
||||
usedBytes: quickUsed,
|
||||
};
|
||||
} catch {
|
||||
return { used: '-', total: '-', usedBytes: 0, totalBytes: 0 };
|
||||
}
|
||||
|
||||
@@ -89,8 +89,8 @@ export class QuarkDriver {
|
||||
return getStorageInfoQuick(this.config.cookie);
|
||||
}
|
||||
|
||||
async getStorageInfo() {
|
||||
return getStorageInfo(this.config.cookie);
|
||||
async getStorageInfo(onBackgroundComplete?: (used: string, total: string) => void) {
|
||||
return getStorageInfo(this.config.cookie, onBackgroundComplete);
|
||||
}
|
||||
|
||||
async calculateUsedSpace(): Promise<number> {
|
||||
|
||||
Reference in New Issue
Block a user