diff --git a/source_clean/src/cloud/cleanup.service.ts b/source_clean/src/cloud/cleanup.service.ts index d746ea4..ae9d326 100755 --- a/source_clean/src/cloud/cleanup.service.ts +++ b/source_clean/src/cloud/cleanup.service.ts @@ -20,7 +20,7 @@ interface CleanupOpResult { trashed: number; errors: string[] } interface CloudCleanupDriver { /** Trash date folders (YYYY-MM-DD) older than `days`. */ - cleanupOldDateFolders(days: number): Promise; + cleanupOldDateFolders(days: number, whitelistDirs?: string[]): Promise; /** * If used space exceeds thresholdPercent% of TOTAL capacity, * delete oldest date folders until totalBytes * deletePercent/100 @@ -28,7 +28,7 @@ interface CloudCleanupDriver { * @param thresholdPercent — trigger when usage >= this % of total * @param deletePercent — free this % of total capacity */ - cleanupBySpaceThreshold(thresholdPercent: number, deletePercent: number): Promise; + cleanupBySpaceThreshold(thresholdPercent: number, deletePercent: number, whitelistDirs?: string[]): Promise; /** Permanently empty the recycle bin. */ emptyTrash(): Promise; } @@ -78,6 +78,13 @@ async function cleanupCloudFiles(days: number): Promise { const errors: string[] = []; let totalTrashed = 0; + // Read whitelist from system config + let whitelist: string[] = []; + try { + const raw = getSystemConfig('cleanup_whitelist_dirs'); + if (raw) whitelist = JSON.parse(raw); + } catch {} + for (const cfg of configs) { const driver = getDriverForCleanup(cfg); if (!driver) { @@ -85,7 +92,7 @@ async function cleanupCloudFiles(days: number): Promise { continue; } try { - const result = await driver.cleanupOldDateFolders(days); + const result = await driver.cleanupOldDateFolders(days, whitelist); totalTrashed += result.trashed; errors.push(...result.errors.map(e => `[${cfg.cloud_type}#${cfg.id}] ${e}`)); } catch (err: any) { @@ -109,6 +116,13 @@ async function cleanupAllBySpaceThreshold( const errors: string[] = []; let totalTrashed = 0; + // Read whitelist from system config + let whitelist: string[] = []; + try { + const raw = getSystemConfig('cleanup_whitelist_dirs'); + if (raw) whitelist = JSON.parse(raw); + } catch {} + for (const cfg of configs) { const driver = getDriverForCleanup(cfg); if (!driver) { @@ -116,7 +130,7 @@ async function cleanupAllBySpaceThreshold( continue; } try { - const result = await driver.cleanupBySpaceThreshold(thresholdPercent, deletePercent); + const result = await driver.cleanupBySpaceThreshold(thresholdPercent, deletePercent, whitelist); totalTrashed += result.trashed; errors.push(...result.errors.map(e => `[${cfg.cloud_type}#${cfg.id}] ${e}`)); } catch (err: any) { diff --git a/source_clean/src/cloud/drivers/baidu.driver.ts b/source_clean/src/cloud/drivers/baidu.driver.ts index 5595c61..52fc547 100644 --- a/source_clean/src/cloud/drivers/baidu.driver.ts +++ b/source_clean/src/cloud/drivers/baidu.driver.ts @@ -1105,7 +1105,7 @@ export class BaiduDriver { } } - async cleanupOldDateFolders(days: number): Promise<{ trashed: number; errors: string[] }> { + async cleanupOldDateFolders(days: number, _whitelistDirs?: string[]): Promise<{ trashed: number; errors: string[] }> { const errors: string[] = []; const cutoff = new Date(); cutoff.setDate(cutoff.getDate() - days); @@ -1131,7 +1131,7 @@ export class BaiduDriver { } } - async cleanupBySpaceThreshold(thresholdPercent: number, deletePercent: number): Promise<{ trashed: number; errors: string[] }> { + async cleanupBySpaceThreshold(thresholdPercent: number, deletePercent: number, _whitelistDirs?: string[]): Promise<{ trashed: number; errors: string[] }> { const errors: string[] = []; try { const info = await this.getUserInfo(); diff --git a/source_clean/src/main.ts b/source_clean/src/main.ts index 178677f..59f95e4 100755 --- a/source_clean/src/main.ts +++ b/source_clean/src/main.ts @@ -183,12 +183,17 @@ async function start(): Promise { let storageRefreshTimer: NodeJS.Timeout | null = null; const scheduleStorageRefresh = () => { if (storageRefreshTimer) clearTimeout(storageRefreshTimer); + // Check if auto refresh is enabled + let enabled = true; let intervalMinutes = 180; // default try { const { getSystemConfig } = require('./admin/system-config.service'); + const autoVal = getSystemConfig('cleanup_auto_refresh_storage'); + if (autoVal === 'false' || autoVal === '0') enabled = false; const val = getSystemConfig('storage_refresh_interval'); if (val) { const n = parseInt(val, 10); if (n >= 5 && n <= 1440) intervalMinutes = n; } } catch {} + if (!enabled) { console.log('[Storage] Auto refresh disabled, skipping'); return; } const ms = intervalMinutes * 60 * 1000; console.log(`[Storage] Next refresh in ${intervalMinutes} minutes`); storageRefreshTimer = setTimeout(() => { @@ -202,6 +207,35 @@ async function start(): Promise { setTimeout(() => scheduleStorageRefresh(), 60000); + // Cookie verification scheduler — periodically test cloud connections + let verifyTimer: NodeJS.Timeout | null = null; + const scheduleVerify = () => { + if (verifyTimer) clearTimeout(verifyTimer); + let enabled = false; + let intervalMinutes = 60; // default every hour + try { + const { getSystemConfig } = require('./admin/system-config.service'); + const enabledVal = getSystemConfig('cleanup_verify_enabled'); + if (enabledVal === 'true' || enabledVal === '1') enabled = true; + const intVal = getSystemConfig('cleanup_verify_interval'); + if (intVal) { const n = parseInt(intVal, 10); if (n >= 5 && n <= 1440) intervalMinutes = n; } + } catch {} + if (!enabled) return; + const ms = intervalMinutes * 60 * 1000; + verifyTimer = setTimeout(async () => { + try { + const { getActiveCloudConfigs, testCloudConnection } = require('./cloud/credential.service'); + const configs = getActiveCloudConfigs(); + for (const c of configs) { + try { await testCloudConnection(c.id); } catch {} + } + } catch (e: any) { console.error('[Verify] Error:', e.message); } + scheduleVerify(); + }, ms); + }; + // Start verification 2 minutes after startup + setTimeout(() => scheduleVerify(), 120000); + // Daily Report scheduler — check every 60 seconds const DAILY_REPORT_CHECK_INTERVAL = 60 * 1000; setInterval(() => {