From 39cce6cac9f92c56a07bc528285e93ccb076b9c2 Mon Sep 17 00:00:00 2001 From: filesite Date: Wed, 11 Dec 2024 23:21:04 +0800 Subject: [PATCH] file counter update --- .../beauty/controller/CommandController.php | 59 ++++++++++++------- themes/beauty/controller/ListController.php | 9 ++- themes/beauty/controller/SiteController.php | 5 +- themes/beauty/views/list/bydate.php | 6 ++ themes/beauty/views/site/index.php | 6 ++ 5 files changed, 60 insertions(+), 25 deletions(-) diff --git a/themes/beauty/controller/CommandController.php b/themes/beauty/controller/CommandController.php index ddff87e..e7673f0 100644 --- a/themes/beauty/controller/CommandController.php +++ b/themes/beauty/controller/CommandController.php @@ -172,12 +172,15 @@ eof; $maxScanDeep = 0; //最大扫描目录级数 $dirTree = $scanner->scan($dirpath, $maxScanDeep); - $this->updateAllDirTreeCache($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']; @@ -409,7 +412,7 @@ eof; return true; } - protected function updateAllDirTreeCache($dirTree) { + protected function updateAllDirTreeCache($dirId, $dirTree) { $cacheKey = $this->allDirTreeCacheKey; $cacheData = Common::getCache($cacheKey); if (empty($cacheData)) { @@ -417,52 +420,64 @@ eof; } $cacheData = array_merge($cacheData, $dirTree); - - $pid = ''; - $imgNum = $videoNum = $audioNum = 0; + if (empty($cacheData[$dirId])) { + $cacheData[$dirId] = $dirTree; + } + $supportedImageExts = FSC::$app['config']['supportedImageExts']; $supportedVideoExts = FSC::$app['config']['supportedVideoExts']; $supportedAudioExts = FSC::$app['config']['supportedAudioExts']; + $imgNum = $videoNum = $audioNum = 0; foreach ($dirTree as $id => $item) { if (empty($item['pid'])) { - break; + echo "Ignored file no pid: {$id}\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 ++; } } - if (!empty($pid) && !empty($cacheData[$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; - } - - $cacheData[$pid]['image_total'] += $imgNum; - $cacheData[$pid]['video_total'] += $videoNum; - $cacheData[$pid]['audio_total'] += $audioNum; - } + $cacheData[$dirId]['image_total'] = $imgNum; + $cacheData[$dirId]['video_total'] = $videoNum; + $cacheData[$dirId]['audio_total'] = $audioNum; + echo "File total: {$dirId}: image {$imgNum}, video {$videoNum}, audio {$audioNum}\n"; return Common::setCache($cacheKey, $cacheData); } + protected function getAllFilesTotalInSubDirs() { + + } + //汇总每个目录下图片、视频、音乐文件数量 /** * 数据格式: @@ -477,7 +492,7 @@ eof; $dirCounter = array(); foreach ($cacheData as $id => $item) { - if ( !empty($item['directory']) && isset($item['image_total']) ) { + if ( isset($item['image_total']) ) { $dirCounter[$id] = array( 'image_total' => $item['image_total'], 'video_total' => $item['video_total'], diff --git a/themes/beauty/controller/ListController.php b/themes/beauty/controller/ListController.php index 4e254ca..f7a75c3 100644 --- a/themes/beauty/controller/ListController.php +++ b/themes/beauty/controller/ListController.php @@ -10,6 +10,7 @@ require_once __DIR__ . '/../../../plugins/Html.php'; Class ListController extends Controller { protected $dateIndexCacheKey = 'MainBotDateIndex'; //索引数据的key单独缓存,缓存key为此{cacheKey}_keys protected $allFilesCacheKey = 'MainBotAllFiles'; + protected $dirCounterCacheKey = 'MainBotDirCounter'; //缓存所有目录包含的文件数量 protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles'; public function actionIndex() { @@ -304,12 +305,14 @@ Class ListController extends Controller { //从缓存文件获取按年份、月份归类的索引数据 $cacheDataByDate = Common::getCacheFromFile($this->dateIndexCacheKey . '_keys', 86400*365, 'index'); + //从缓存文件获取所有目录文件数量数据 + $dirCounters = Common::getCacheFromFile($this->dirCounterCacheKey, 86400*365, 'index'); $viewName = '//site/index'; //共享视图 $params = compact( 'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme', 'mp3File', 'page', 'pageSize', 'cacheDataId', 'copyright', 'showType', 'isAdminIp', 'allFiles', - 'cacheDataByDate' + 'cacheDataByDate', 'dirCounters' ); return $this->render($viewName, $params, $pageTitle); } @@ -606,6 +609,8 @@ Class ListController extends Controller { return $this->renderJson(compact('page', 'pageSize', 'audios')); } + //从缓存文件获取所有目录文件数量数据 + $dirCounters = Common::getCacheFromFile($this->dirCounterCacheKey, 86400*365, 'index'); $pageTitlePrefix = "{$intYear}年的"; if (!empty($para_month)) { @@ -622,7 +627,7 @@ Class ListController extends Controller { 'allFiles', 'cacheData', 'cacheData_keys', 'monthsByType', - 'para_year', 'para_month' + 'para_year', 'para_month', 'dirCounters' ); return $this->render($viewName, $params, $pageTitle); } diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index b03a983..32661cd 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -10,6 +10,7 @@ require_once __DIR__ . '/../../../plugins/Html.php'; Class SiteController extends Controller { protected $dateIndexCacheKey = 'MainBotDateIndex'; //索引数据的key单独缓存,缓存key为此{cacheKey}_keys protected $allFilesCacheKey = 'MainBotAllFiles'; + protected $dirCounterCacheKey = 'MainBotDirCounter'; //缓存所有目录包含的文件数量 protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles'; public function actionIndex() { @@ -263,13 +264,15 @@ Class SiteController extends Controller { //从缓存文件获取按年份、月份归类的索引数据 $cacheDataByDate = Common::getCacheFromFile($this->dateIndexCacheKey . '_keys', 86400*365, 'index'); + //从缓存文件获取所有目录文件数量数据 + $dirCounters = Common::getCacheFromFile($this->dirCounterCacheKey, 86400*365, 'index'); $viewName = 'index'; $params = compact( 'page', 'pageSize', 'cacheDataId', 'showType', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'mp3File', 'copyright', 'alertWarning', 'isAdminIp', 'allFiles', - 'cacheDataByDate' + 'cacheDataByDate', 'dirCounters' ); return $this->render($viewName, $params, $pageTitle); } diff --git a/themes/beauty/views/list/bydate.php b/themes/beauty/views/list/bydate.php index 6bf9f20..73816a0 100644 --- a/themes/beauty/views/list/bydate.php +++ b/themes/beauty/views/list/bydate.php @@ -86,10 +86,16 @@ eof; $item) { + $fileTotal = 0; + if ( !empty($viewData['dirCounters']) && !empty($viewData['dirCounters'][$item['id']]) ) { + $dirTotal = $viewData['dirCounters'][$item['id']]; + $fileTotal = $dirTotal['image_total'] + $dirTotal['video_total'] + $dirTotal['audio_total']; + } echo << directories {$item['directory']} + {$fileTotal} eof; } diff --git a/themes/beauty/views/site/index.php b/themes/beauty/views/site/index.php index 06a5a2c..e460715 100644 --- a/themes/beauty/views/site/index.php +++ b/themes/beauty/views/site/index.php @@ -97,11 +97,17 @@ eof; $breadcrumbs = !empty($viewData['breadcrumbs']) ? $viewData['breadcrumbs'] : []; if (!empty($viewData['menus'])) { //只显示第一级目录 foreach ($viewData['menus'] as $index => $item) { + $fileTotal = 0; + if ( !empty($viewData['dirCounters']) && !empty($viewData['dirCounters'][$item['id']]) ) { + $dirTotal = $viewData['dirCounters'][$item['id']]; + $fileTotal = $dirTotal['image_total'] + $dirTotal['video_total'] + $dirTotal['audio_total']; + } $selected = $item['id'] == $selectedId || (!empty($breadcrumbs) && $item['id'] == $breadcrumbs[0]['id']) ? 'active' : ''; echo << directories {$item['directory']} + {$fileTotal} eof; }