v0.3.21: 修复存储空间自动刷新不生效

根因:
1. refreshAllStorageInfo 只过滤 quark, 百度等其他网盘永远不会刷新
2. 主调度器硬编码 60 分钟, 未读取 storage_refresh_interval 配置(用户设的180分钟)
3. 条件检查用 totalBytes/usedBytes, 百度返回对象无此字段 → 永远跳过

修复:
- refreshAllStorageInfo: 改为驱动注册表模式, 支持 quark+baidu, 兼容不同驱动返回格式
- main.ts: 用 setTimeout 链替代 setInterval, 每次动态读 storage_refresh_interval, 修改后无需重启即生效
This commit is contained in:
2026-05-17 17:26:45 +08:00
parent 0e0cad1271
commit d87bc6fd5a
4 changed files with 70 additions and 12 deletions

View File

@@ -1 +1 @@
0.3.20
0.3.21

View File

@@ -1 +1 @@
0.3.20
0.3.21

View File

@@ -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<void> {
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<string, { module: string; cls: string }> = {
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);
}
}
}

View File

@@ -178,10 +178,28 @@ async function start(): Promise<void> {
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