From b2746502f09a09a4d254412b826df3c05cd478e4 Mon Sep 17 00:00:00 2001 From: filesite Date: Tue, 17 Dec 2024 21:04:36 +0800 Subject: [PATCH] files counter for all dirs improved --- .../beauty/controller/CommandController.php | 72 +++++++++++++------ 1 file changed, 50 insertions(+), 22 deletions(-) diff --git a/themes/beauty/controller/CommandController.php b/themes/beauty/controller/CommandController.php index e7673f0..0edf361 100644 --- a/themes/beauty/controller/CommandController.php +++ b/themes/beauty/controller/CommandController.php @@ -152,6 +152,26 @@ eof; Common::setCache($this->allFilesCacheKey, array()); } + protected function getParentDir($dirpath) { + $rootDir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory']; + $rootDir = realpath($rootDir); + + if (strpos($dirpath, $rootDir) !== false) { + $dirs = str_replace($rootDir, '', $dirpath); + $dirs = preg_replace('/\/$/', '', $dirs); + $arr = explode('/', $dirs); + $num = count($arr); + if ($num >= 2) { + $left = array_slice($arr, 0, $num-1); + return realpath( $rootDir . '/' . implode('/', $left) ); + }else { + return $rootDir; + } + } + + return ''; + } + //扫描媒体文件:图片、视频、音乐 //TODO: 把它们按年份、月份归类,并缓存到/runtime/cache/目录,方便前端展示读取 //把当前扫描进度保存到单独的缓存文件,便于用户随时获取 @@ -173,14 +193,16 @@ eof; $maxScanDeep = 0; //最大扫描目录级数 $dirTree = $scanner->scan($dirpath, $maxScanDeep); + //统计文件数量 + $dirId = $scanner->getId($dirpath); + $parentDir = $this->getParentDir($dirpath); + $parentId = $scanner->getId($parentDir); + $this->updateAllDirTreeCache($dirId, $parentId, $dirTree); + $scanResults = $scanner->getScanResults(); echo 'Total directories or files: ' . count($scanResults); echo "\n"; - //统计文件数量 - $dirId = $scanner->getId($dirpath); - $this->updateAllDirTreeCache($dirId, $scanResults); - $supportedImageExts = FSC::$app['config']['supportedImageExts']; $supportedVideoExts = FSC::$app['config']['supportedVideoExts']; $supportedAudioExts = FSC::$app['config']['supportedAudioExts']; @@ -412,7 +434,7 @@ eof; return true; } - protected function updateAllDirTreeCache($dirId, $dirTree) { + protected function updateAllDirTreeCache($dirId, $parentId, $dirTree) { $cacheKey = $this->allDirTreeCacheKey; $cacheData = Common::getCache($cacheKey); if (empty($cacheData)) { @@ -431,37 +453,22 @@ eof; foreach ($dirTree as $id => $item) { if (empty($item['pid'])) { echo "Ignored file no pid: {$id}\n"; - print_r($item); - echo "\n"; + //print_r($item); + //echo "\n"; continue; } - //TODO: 递归更新所有父目录 - $pid = $item['pid']; - if (!isset($cacheData[$pid]['image_total'])) { - $cacheData[$pid]['image_total'] = 0; - } - if (!isset($cacheData[$pid]['video_total'])) { - $cacheData[$pid]['video_total'] = 0; - } - if (!isset($cacheData[$pid]['audio_total'])) { - $cacheData[$pid]['audio_total'] = 0; - } - if ( !empty($item['filename']) && in_array($item['extension'], $supportedImageExts) ) { - $cacheData[$pid]['image_total'] ++; $imgNum ++; }else if ( !empty($item['filename']) && in_array($item['extension'], $supportedVideoExts) ) { - $cacheData[$pid]['video_total'] ++; $videoNum ++; }else if ( !empty($item['filename']) && in_array($item['extension'], $supportedAudioExts) ) { - $cacheData[$pid]['audio_total'] ++; $audioNum ++; } } @@ -471,6 +478,27 @@ eof; $cacheData[$dirId]['audio_total'] = $audioNum; echo "File total: {$dirId}: image {$imgNum}, video {$videoNum}, audio {$audioNum}\n"; + //更新父目录数据 + if (!empty($cacheData[$parentId])) { + if (isset($cacheData[$parentId]['image_total'])) { + $cacheData[$parentId]['image_total'] += $imgNum; + }else { + $cacheData[$parentId]['image_total'] = $imgNum; + } + + if (isset($cacheData[$parentId]['video_total'])) { + $cacheData[$parentId]['video_total'] += $videoNum; + }else { + $cacheData[$parentId]['video_total'] = $videoNum; + } + + if (isset($cacheData[$parentId]['audio_total'])) { + $cacheData[$parentId]['audio_total'] += $audioNum; + }else { + $cacheData[$parentId]['audio_total'] = $audioNum; + } + } + return Common::setCache($cacheKey, $cacheData); }