diff --git a/VERSION b/VERSION index f9a4b5f..dfdc368 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.20 +0.3.21 diff --git a/source_clean/VERSION b/source_clean/VERSION index f9a4b5f..dfdc368 100644 --- a/source_clean/VERSION +++ b/source_clean/VERSION @@ -1 +1 @@ -0.3.20 +0.3.21 diff --git a/source_clean/src/cloud/cloud.service.ts b/source_clean/src/cloud/cloud.service.ts index 9e02068..0bee9c5 100644 --- a/source_clean/src/cloud/cloud.service.ts +++ b/source_clean/src/cloud/cloud.service.ts @@ -327,23 +327,63 @@ export function cleanupOldSaveRecords(): void { // ── Storage Refresh ─────────────────────────────────────────────── +/** + * Refresh storage info for all active cloud configs that have a getStorageInfo method. + * Supports quark and baidu drivers. + */ export async function refreshAllStorageInfo(): Promise { - const configs = getActiveCloudConfigs().filter(c => c.cloud_type === 'quark' && c.cookie); + const configs = getActiveCloudConfigs().filter(c => c.cookie); if (configs.length === 0) return; + // Driver mapping: cloud_type → { module, class } + const DRIVER_REGISTRY: Record = { + quark: { module: './drivers/quark.driver', cls: 'QuarkDriver' }, + baidu: { module: './drivers/baidu.driver', cls: 'BaiduDriver' }, + }; + for (const cfg of configs) { + const entry = DRIVER_REGISTRY[cfg.cloud_type]; + if (!entry) continue; // no getStorageInfo support for this cloud type + try { - const { QuarkDriver } = require('./drivers/quark.driver'); - const driver = new QuarkDriver({ cookie: cfg.cookie, nickname: cfg.nickname }); - const storage = await driver.getStorageInfo(); - if (storage.totalBytes > 0 || storage.usedBytes > 0) { + const mod = require(entry.module); + const Driver = mod[entry.cls]; + if (!Driver) continue; + + const driver = new Driver({ cookie: cfg.cookie, nickname: cfg.nickname }); + + // Try getStorageInfo first (quark cleanup API), fallback to getStorageInfoQuick + let storage: any; + try { + storage = await driver.getStorageInfo(); + } catch { + if (typeof driver.getStorageInfoQuick === 'function') { + storage = await driver.getStorageInfoQuick(); + } else { + continue; + } + } + + if (!storage) continue; + + // Get formatted strings — some drivers return {used, total, usedBytes, totalBytes} + const used = storage.used || '计算中...'; + const total = storage.total || '-'; + + // Only update if we got meaningful data + const hasRealData = + (storage.totalBytes > 0 || storage.usedBytes > 0) || // quark returns these + (used !== '-' && used !== '0 B' && used !== '计算中...'); // baidu check + + if (hasRealData) { const db = getDb(); db.prepare( `UPDATE cloud_configs SET storage_used = ?, storage_total = ? WHERE id = ?` - ).run(storage.used, storage.total, cfg.id); + ).run(used, total, cfg.id); + console.log(`[Storage] Refreshed ${cfg.cloud_type}#${cfg.id}: ${used} / ${total}`); } } catch (err: any) { - console.error(`[Storage] Failed to refresh quark#${cfg.id}:`, err.message); + console.error(`[Storage] Failed to refresh ${cfg.cloud_type}#${cfg.id}:`, err.message); } } } diff --git a/source_clean/src/main.ts b/source_clean/src/main.ts index 7e91d81..178677f 100755 --- a/source_clean/src/main.ts +++ b/source_clean/src/main.ts @@ -178,10 +178,28 @@ async function start(): Promise { setInterval(() => { checkAndRunScheduledCleanup().catch(err => console.error('[Cleanup] Scheduler error:', err.message)); }, CLEANUP_INTERVAL); setTimeout(() => { checkAndRunScheduledCleanup().catch(err => console.error('[Cleanup] Initial check error:', err.message)); }, 30000); - // Storage info refresh scheduler — every 60 minutes - const STORAGE_REFRESH_INTERVAL = 60 * 60 * 1000; - setInterval(() => { refreshAllStorageInfo().catch(err => console.error('[Storage] Refresh error:', err.message)); }, STORAGE_REFRESH_INTERVAL); + // Storage info refresh scheduler — dynamic interval from system_configs.storage_refresh_interval + // Uses setTimeout chain so interval changes take effect without restart + let storageRefreshTimer: NodeJS.Timeout | null = null; + const scheduleStorageRefresh = () => { + if (storageRefreshTimer) clearTimeout(storageRefreshTimer); + let intervalMinutes = 180; // default + try { + const { getSystemConfig } = require('./admin/system-config.service'); + const val = getSystemConfig('storage_refresh_interval'); + if (val) { const n = parseInt(val, 10); if (n >= 5 && n <= 1440) intervalMinutes = n; } + } catch {} + const ms = intervalMinutes * 60 * 1000; + console.log(`[Storage] Next refresh in ${intervalMinutes} minutes`); + storageRefreshTimer = setTimeout(() => { + refreshAllStorageInfo().catch(err => console.error('[Storage] Refresh error:', err.message)); + scheduleStorageRefresh(); // re-schedule with possibly updated interval + }, ms); + }; + // First run 60s after startup setTimeout(() => { refreshAllStorageInfo().catch(err => console.error('[Storage] Initial refresh error:', err.message)); }, 60000); + // Schedule first periodic refresh (delay = configured interval) + setTimeout(() => scheduleStorageRefresh(), 60000); // Daily Report scheduler — check every 60 seconds