From f8dc4c2c2a8b0539055419d01d26357f92476510 Mon Sep 17 00:00:00 2001 From: filesite Date: Wed, 20 Nov 2024 16:40:59 +0800 Subject: [PATCH] main bot scan stats save --- .../beauty/controller/CommandController.php | 72 +++++++++++++++++-- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/themes/beauty/controller/CommandController.php b/themes/beauty/controller/CommandController.php index 97c05dd..4be36ec 100644 --- a/themes/beauty/controller/CommandController.php +++ b/themes/beauty/controller/CommandController.php @@ -7,6 +7,7 @@ require_once __DIR__ . '/../../../plugins/Parsedown.php'; require_once __DIR__ . '/../../../plugins/Common.php'; Class CommandController extends Controller { + protected $logPrefix = '[MainBot]'; public function actionIndex() { $commands = <<logPrefix; echo "{$botLogPrefix} Main bot started @{$thisTime}\n"; @@ -120,6 +121,9 @@ eof; } + //扫描媒体文件:图片、视频、音乐 + //TODO: 把它们按年份、月份归类,并缓存到/runtime/cache/目录,方便前端展示读取 + //把当前扫描进度保存到单独的缓存文件,便于用户随时获取 protected function scanMediaFiles($dirpath = '') { $rootDir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory']; if (empty($dirpath)) { @@ -140,10 +144,13 @@ eof; $supportedImageExts = FSC::$app['config']['supportedImageExts']; $supportedVideoExts = FSC::$app['config']['supportedVideoExts']; $supportedAudioExts = FSC::$app['config']['supportedAudioExts']; - + $cacheKey = 'MainBotScanedDirs'; if (!empty($scanResults)) { + $scanIndex = 0; + $scanTotal = count($scanResults); + foreach ($scanResults as $id => $item) { $hadScanedDirs = Common::getCache($cacheKey); @@ -153,22 +160,77 @@ eof; && in_array($item['extension'], $supportedImageExts) && !in_array($item['extension'], $scanner->exifSupportFileExtensions) ) { - echo "Image file no original_ctime: {$item['filename']}.{$item['extension']}, {$item['realpath']}\n"; + //echo "Image file no original_ctime: {$item['filename']}.{$item['extension']}, {$item['realpath']}\n"; }else if ( !empty($item['filename']) && empty($item['original_ctime']) && (in_array($item['extension'], $supportedVideoExts) || in_array($item['extension'], $supportedAudioExts)) ) { - echo "Video or audio file no original_ctime: {$item['filename']}.{$item['extension']}, {$item['realpath']}\n"; + //echo "Video or audio file no original_ctime: {$item['filename']}.{$item['extension']}, {$item['realpath']}\n"; }else if (!empty($item['directory']) && empty($hadScanedDirs[$id])) { //if it's directory $hadScanedDirs[$id] = true; Common::setCache($cacheKey, $hadScanedDirs); - sleep(1); + //sleep(1); $this->scanMediaFiles($item['realpath']); } + + $scanIndex ++; + $stats = $this->updateScanStats($dirpath, $scanTotal, $scanIndex); } + + sleep(1); } } + //更新扫描进度 + protected function updateScanStats($dirpath, $total, $index) { + if (empty($total)) {return false;} + + $stats = array( + 'currentDir' => $dirpath, + 'total' => $total, + 'current' => $index, + 'percent' => round($index/$total, 2) * 100, + ); + + $botLogPrefix = $this->logPrefix; + $cacheDir = __DIR__ . '/../../../runtime/cache/'; + $statsFile = "{$cacheDir}stats_scan.json"; + if (!is_dir($cacheDir)) { + mkdir($cacheDir, 0777, true); + } + + $rootDir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory']; + if ($dirpath == $rootDir) { + echo "{$botLogPrefix} {$dirpath} scan has finished {$stats['percent']}%, total {$stats['total']}, current {$stats['current']}\n"; + + //保存进度文件 + file_put_contents($statsFile, json_encode($stats) . "\n"); + }else if (file_exists($statsFile)) { //更新当前扫描目录 + $json = file_get_contents($statsFile); + if (!empty($json)) { + $jsonData = json_decode(trim($json), true); + if ($jsonData['currentDir'] != $dirpath) { + $jsonData['currentDir'] = $dirpath; + $json = json_encode($jsonData) . "\n"; + file_put_contents($statsFile, $json); + } + } + } + + return $stats; + } + + public function actionTest() { + $cacheKey = 'TestSTData'; + $time = Common::getCache($cacheKey); + if (empty($time)) { + $time = date('Y-m-d H:i:s'); + Common::setCache($cacheKey, $time); + } + + echo "Cache time {$time}\n"; + } + }