fix: getStorageInfo快速估算(3s)+后台全量遍历校准; quark-cleanup去kps依赖; QuarkDriver支持回调

This commit is contained in:
2026-05-15 18:31:52 +08:00
parent dfcdddabad
commit cf965bcfee
3 changed files with 50 additions and 24 deletions

View File

@@ -329,7 +329,16 @@ export async function refreshAllStorageInfo(): Promise<void> {
try { try {
const { QuarkDriver } = require('./drivers/quark.driver'); const { QuarkDriver } = require('./drivers/quark.driver');
const driver = new QuarkDriver({ cookie: decrypt(cfg.cookie!), nickname: cfg.nickname }); 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) { if (storage.totalBytes > 0 || storage.usedBytes > 0) {
const db = getDb(); const db = getDb();
db.prepare( db.prepare(

View File

@@ -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 }> { export async function getStorageInfoQuick(cookie: string, fallbackTotal?: string): Promise<{ total: string; totalBytes: number; used: string; usedBytes: number }> {
try { try {
const mparam = getMparam(cookie); const params = new URLSearchParams(getCommonParams());
const params = new URLSearchParams({
...getCommonParams(),
kps: mparam.kps || '',
sign: mparam.sign || '',
vcode: mparam.vcode || '',
});
const capResponse = await fetch(`${BASE_URL}/1/clouddrive/capacity/detail?${params.toString()}`, { const capResponse = await fetch(`${BASE_URL}/1/clouddrive/capacity/detail?${params.toString()}`, {
headers: getHeaders(cookie), headers: getHeaders(cookie),
signal: AbortSignal.timeout(10000), signal: AbortSignal.timeout(10000),
@@ -87,14 +81,22 @@ export async function getStorageInfoQuick(cookie: string, fallbackTotal?: string
/** /**
* Get storage info with used space calculation. * 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 { try {
let totalBytes = 0;
const params = new URLSearchParams(getCommonParams()); const params = new URLSearchParams(getCommonParams());
const response = await fetch(`${BASE_URL}/1/clouddrive/capacity/detail?${params.toString()}`, { const response = await fetch(`${BASE_URL}/1/clouddrive/capacity/detail?${params.toString()}`, {
headers: getHeaders(cookie), headers: getHeaders(cookie),
signal: AbortSignal.timeout(10000), signal: AbortSignal.timeout(10000),
}); });
let totalBytes = 0;
if (response.ok) { if (response.ok) {
const data = await response.json() as any; const data = await response.json() as any;
if (data.status === 200 && data.data) { 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) { // Quick estimation: sum root-level files only
return { let quickUsed = 0;
total: totalBytes > 0 ? formatBytes(totalBytes) : '-', try {
used: formatBytes(usedBytes), const rootFiles = await listRootDir(cookie);
usedBytes, for (const f of rootFiles) {
totalBytes: totalBytes > 0 ? totalBytes : 0, quickUsed += f.size || 0;
}; }
} } catch {}
return { used: '0 B', total: '-', usedBytes: 0, totalBytes: 0 };
// 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 { } catch {
return { used: '-', total: '-', usedBytes: 0, totalBytes: 0 }; return { used: '-', total: '-', usedBytes: 0, totalBytes: 0 };
} }

View File

@@ -89,8 +89,8 @@ export class QuarkDriver {
return getStorageInfoQuick(this.config.cookie); return getStorageInfoQuick(this.config.cookie);
} }
async getStorageInfo() { async getStorageInfo(onBackgroundComplete?: (used: string, total: string) => void) {
return getStorageInfo(this.config.cookie); return getStorageInfo(this.config.cookie, onBackgroundComplete);
} }
async calculateUsedSpace(): Promise<number> { async calculateUsedSpace(): Promise<number> {