From cf965bcfee0f14e26eadb3598fb400d813859e63 Mon Sep 17 00:00:00 2001 From: admin <362324317@qq.com> Date: Fri, 15 May 2026 18:31:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20getStorageInfo=E5=BF=AB=E9=80=9F?= =?UTF-8?q?=E4=BC=B0=E7=AE=97(3s)+=E5=90=8E=E5=8F=B0=E5=85=A8=E9=87=8F?= =?UTF-8?q?=E9=81=8D=E5=8E=86=E6=A0=A1=E5=87=86;=20quark-cleanup=E5=8E=BBk?= =?UTF-8?q?ps=E4=BE=9D=E8=B5=96;=20QuarkDriver=E6=94=AF=E6=8C=81=E5=9B=9E?= =?UTF-8?q?=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/backend/src/cloud/cloud.service.ts | 11 +++- .../src/cloud/drivers/quark-cleanup.ts | 57 ++++++++++++------- .../backend/src/cloud/drivers/quark.driver.ts | 6 +- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/packages/backend/src/cloud/cloud.service.ts b/packages/backend/src/cloud/cloud.service.ts index 56c87eb..802963f 100644 --- a/packages/backend/src/cloud/cloud.service.ts +++ b/packages/backend/src/cloud/cloud.service.ts @@ -329,7 +329,16 @@ export async function refreshAllStorageInfo(): Promise { 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( diff --git a/packages/backend/src/cloud/drivers/quark-cleanup.ts b/packages/backend/src/cloud/drivers/quark-cleanup.ts index ad46849..0556588 100644 --- a/packages/backend/src/cloud/drivers/quark-cleanup.ts +++ b/packages/backend/src/cloud/drivers/quark-cleanup.ts @@ -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, - }; - } - return { used: '0 B', total: '-', usedBytes: 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; + } + } 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 }; } @@ -306,4 +323,4 @@ export async function cleanupBySpaceThreshold( } catch (err: any) { return { trashed: 0, errors: [err.message] }; } -} +} \ No newline at end of file diff --git a/packages/backend/src/cloud/drivers/quark.driver.ts b/packages/backend/src/cloud/drivers/quark.driver.ts index 39afa2d..93d0318 100755 --- a/packages/backend/src/cloud/drivers/quark.driver.ts +++ b/packages/backend/src/cloud/drivers/quark.driver.ts @@ -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 { @@ -119,4 +119,4 @@ export class QuarkDriver { async cleanupBySpaceThreshold(thresholdPercent: number, deletePercent: number) { return cleanupBySpaceThreshold(this.config.cookie, thresholdPercent, deletePercent); } -} +} \ No newline at end of file