Browse Source

file counter update

master
filesite 1 month ago
parent
commit
39cce6cac9
  1. 55
      themes/beauty/controller/CommandController.php
  2. 9
      themes/beauty/controller/ListController.php
  3. 5
      themes/beauty/controller/SiteController.php
  4. 6
      themes/beauty/views/list/bydate.php
  5. 6
      themes/beauty/views/site/index.php

55
themes/beauty/controller/CommandController.php

@ -172,12 +172,15 @@ eof;
$maxScanDeep = 0; //最大扫描目录级数 $maxScanDeep = 0; //最大扫描目录级数
$dirTree = $scanner->scan($dirpath, $maxScanDeep); $dirTree = $scanner->scan($dirpath, $maxScanDeep);
$this->updateAllDirTreeCache($dirTree);
$scanResults = $scanner->getScanResults(); $scanResults = $scanner->getScanResults();
echo 'Total directories or files: ' . count($scanResults); echo 'Total directories or files: ' . count($scanResults);
echo "\n"; echo "\n";
//统计文件数量
$dirId = $scanner->getId($dirpath);
$this->updateAllDirTreeCache($dirId, $scanResults);
$supportedImageExts = FSC::$app['config']['supportedImageExts']; $supportedImageExts = FSC::$app['config']['supportedImageExts'];
$supportedVideoExts = FSC::$app['config']['supportedVideoExts']; $supportedVideoExts = FSC::$app['config']['supportedVideoExts'];
$supportedAudioExts = FSC::$app['config']['supportedAudioExts']; $supportedAudioExts = FSC::$app['config']['supportedAudioExts'];
@ -409,7 +412,7 @@ eof;
return true; return true;
} }
protected function updateAllDirTreeCache($dirTree) { protected function updateAllDirTreeCache($dirId, $dirTree) {
$cacheKey = $this->allDirTreeCacheKey; $cacheKey = $this->allDirTreeCacheKey;
$cacheData = Common::getCache($cacheKey); $cacheData = Common::getCache($cacheKey);
if (empty($cacheData)) { if (empty($cacheData)) {
@ -417,50 +420,62 @@ eof;
} }
$cacheData = array_merge($cacheData, $dirTree); $cacheData = array_merge($cacheData, $dirTree);
if (empty($cacheData[$dirId])) {
$cacheData[$dirId] = $dirTree;
}
$pid = '';
$imgNum = $videoNum = $audioNum = 0;
$supportedImageExts = FSC::$app['config']['supportedImageExts']; $supportedImageExts = FSC::$app['config']['supportedImageExts'];
$supportedVideoExts = FSC::$app['config']['supportedVideoExts']; $supportedVideoExts = FSC::$app['config']['supportedVideoExts'];
$supportedAudioExts = FSC::$app['config']['supportedAudioExts']; $supportedAudioExts = FSC::$app['config']['supportedAudioExts'];
$imgNum = $videoNum = $audioNum = 0;
foreach ($dirTree as $id => $item) { foreach ($dirTree as $id => $item) {
if (empty($item['pid'])) { if (empty($item['pid'])) {
break; echo "Ignored file no pid: {$id}\n";
print_r($item);
echo "\n";
continue;
} }
//TODO: 递归更新所有父目录
$pid = $item['pid']; $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 ( if (
!empty($item['filename']) && in_array($item['extension'], $supportedImageExts) !empty($item['filename']) && in_array($item['extension'], $supportedImageExts)
) { ) {
$cacheData[$pid]['image_total'] ++;
$imgNum ++; $imgNum ++;
}else if ( }else if (
!empty($item['filename']) && in_array($item['extension'], $supportedVideoExts) !empty($item['filename']) && in_array($item['extension'], $supportedVideoExts)
) { ) {
$cacheData[$pid]['video_total'] ++;
$videoNum ++; $videoNum ++;
}else if ( }else if (
!empty($item['filename']) && in_array($item['extension'], $supportedAudioExts) !empty($item['filename']) && in_array($item['extension'], $supportedAudioExts)
) { ) {
$cacheData[$pid]['audio_total'] ++;
$audioNum ++; $audioNum ++;
} }
} }
if (!empty($pid) && !empty($cacheData[$pid])) { $cacheData[$dirId]['image_total'] = $imgNum;
if (!isset($cacheData[$pid]['image_total'])) { $cacheData[$dirId]['video_total'] = $videoNum;
$cacheData[$pid]['image_total'] = 0; $cacheData[$dirId]['audio_total'] = $audioNum;
} echo "File total: {$dirId}: image {$imgNum}, video {$videoNum}, audio {$audioNum}\n";
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; return Common::setCache($cacheKey, $cacheData);
$cacheData[$pid]['video_total'] += $videoNum;
$cacheData[$pid]['audio_total'] += $audioNum;
} }
return Common::setCache($cacheKey, $cacheData); protected function getAllFilesTotalInSubDirs() {
} }
//汇总每个目录下图片、视频、音乐文件数量 //汇总每个目录下图片、视频、音乐文件数量
@ -477,7 +492,7 @@ eof;
$dirCounter = array(); $dirCounter = array();
foreach ($cacheData as $id => $item) { foreach ($cacheData as $id => $item) {
if ( !empty($item['directory']) && isset($item['image_total']) ) { if ( isset($item['image_total']) ) {
$dirCounter[$id] = array( $dirCounter[$id] = array(
'image_total' => $item['image_total'], 'image_total' => $item['image_total'],
'video_total' => $item['video_total'], 'video_total' => $item['video_total'],

9
themes/beauty/controller/ListController.php

@ -10,6 +10,7 @@ require_once __DIR__ . '/../../../plugins/Html.php';
Class ListController extends Controller { Class ListController extends Controller {
protected $dateIndexCacheKey = 'MainBotDateIndex'; //索引数据的key单独缓存,缓存key为此{cacheKey}_keys protected $dateIndexCacheKey = 'MainBotDateIndex'; //索引数据的key单独缓存,缓存key为此{cacheKey}_keys
protected $allFilesCacheKey = 'MainBotAllFiles'; protected $allFilesCacheKey = 'MainBotAllFiles';
protected $dirCounterCacheKey = 'MainBotDirCounter'; //缓存所有目录包含的文件数量
protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles'; protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles';
public function actionIndex() { public function actionIndex() {
@ -304,12 +305,14 @@ Class ListController extends Controller {
//从缓存文件获取按年份、月份归类的索引数据 //从缓存文件获取按年份、月份归类的索引数据
$cacheDataByDate = Common::getCacheFromFile($this->dateIndexCacheKey . '_keys', 86400*365, 'index'); $cacheDataByDate = Common::getCacheFromFile($this->dateIndexCacheKey . '_keys', 86400*365, 'index');
//从缓存文件获取所有目录文件数量数据
$dirCounters = Common::getCacheFromFile($this->dirCounterCacheKey, 86400*365, 'index');
$viewName = '//site/index'; //共享视图 $viewName = '//site/index'; //共享视图
$params = compact( $params = compact(
'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme', 'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme',
'mp3File', 'page', 'pageSize', 'cacheDataId', 'copyright', 'showType', 'isAdminIp', 'allFiles', 'mp3File', 'page', 'pageSize', 'cacheDataId', 'copyright', 'showType', 'isAdminIp', 'allFiles',
'cacheDataByDate' 'cacheDataByDate', 'dirCounters'
); );
return $this->render($viewName, $params, $pageTitle); return $this->render($viewName, $params, $pageTitle);
} }
@ -606,6 +609,8 @@ Class ListController extends Controller {
return $this->renderJson(compact('page', 'pageSize', 'audios')); return $this->renderJson(compact('page', 'pageSize', 'audios'));
} }
//从缓存文件获取所有目录文件数量数据
$dirCounters = Common::getCacheFromFile($this->dirCounterCacheKey, 86400*365, 'index');
$pageTitlePrefix = "{$intYear}年的"; $pageTitlePrefix = "{$intYear}年的";
if (!empty($para_month)) { if (!empty($para_month)) {
@ -622,7 +627,7 @@ Class ListController extends Controller {
'allFiles', 'allFiles',
'cacheData', 'cacheData',
'cacheData_keys', 'monthsByType', 'cacheData_keys', 'monthsByType',
'para_year', 'para_month' 'para_year', 'para_month', 'dirCounters'
); );
return $this->render($viewName, $params, $pageTitle); return $this->render($viewName, $params, $pageTitle);
} }

5
themes/beauty/controller/SiteController.php

@ -10,6 +10,7 @@ require_once __DIR__ . '/../../../plugins/Html.php';
Class SiteController extends Controller { Class SiteController extends Controller {
protected $dateIndexCacheKey = 'MainBotDateIndex'; //索引数据的key单独缓存,缓存key为此{cacheKey}_keys protected $dateIndexCacheKey = 'MainBotDateIndex'; //索引数据的key单独缓存,缓存key为此{cacheKey}_keys
protected $allFilesCacheKey = 'MainBotAllFiles'; protected $allFilesCacheKey = 'MainBotAllFiles';
protected $dirCounterCacheKey = 'MainBotDirCounter'; //缓存所有目录包含的文件数量
protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles'; protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles';
public function actionIndex() { public function actionIndex() {
@ -263,13 +264,15 @@ Class SiteController extends Controller {
//从缓存文件获取按年份、月份归类的索引数据 //从缓存文件获取按年份、月份归类的索引数据
$cacheDataByDate = Common::getCacheFromFile($this->dateIndexCacheKey . '_keys', 86400*365, 'index'); $cacheDataByDate = Common::getCacheFromFile($this->dateIndexCacheKey . '_keys', 86400*365, 'index');
//从缓存文件获取所有目录文件数量数据
$dirCounters = Common::getCacheFromFile($this->dirCounterCacheKey, 86400*365, 'index');
$viewName = 'index'; $viewName = 'index';
$params = compact( $params = compact(
'page', 'pageSize', 'cacheDataId', 'showType', 'page', 'pageSize', 'cacheDataId', 'showType',
'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'mp3File', 'copyright', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'mp3File', 'copyright',
'alertWarning', 'isAdminIp', 'allFiles', 'alertWarning', 'isAdminIp', 'allFiles',
'cacheDataByDate' 'cacheDataByDate', 'dirCounters'
); );
return $this->render($viewName, $params, $pageTitle); return $this->render($viewName, $params, $pageTitle);
} }

6
themes/beauty/views/list/bydate.php

@ -86,10 +86,16 @@ eof;
<?php <?php
if (!empty($viewData['menus'])) { //只显示第一级目录 if (!empty($viewData['menus'])) { //只显示第一级目录
foreach ($viewData['menus'] as $index => $item) { 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'];
}
echo <<<eof echo <<<eof
<li><a href="{$item['path']}"> <li><a href="{$item['path']}">
<img src="/img/beauty/folder.svg" alt="directories" width="17" class="menu-icon"> <img src="/img/beauty/folder.svg" alt="directories" width="17" class="menu-icon">
{$item['directory']} {$item['directory']}
<small class="badge">{$fileTotal}</small>
</a></li> </a></li>
eof; eof;
} }

6
themes/beauty/views/site/index.php

@ -97,11 +97,17 @@ eof;
$breadcrumbs = !empty($viewData['breadcrumbs']) ? $viewData['breadcrumbs'] : []; $breadcrumbs = !empty($viewData['breadcrumbs']) ? $viewData['breadcrumbs'] : [];
if (!empty($viewData['menus'])) { //只显示第一级目录 if (!empty($viewData['menus'])) { //只显示第一级目录
foreach ($viewData['menus'] as $index => $item) { 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' : ''; $selected = $item['id'] == $selectedId || (!empty($breadcrumbs) && $item['id'] == $breadcrumbs[0]['id']) ? 'active' : '';
echo <<<eof echo <<<eof
<li class="{$selected}"><a href="{$item['path']}"> <li class="{$selected}"><a href="{$item['path']}">
<img src="/img/beauty/folder.svg" alt="directories" width="17" class="menu-icon"> <img src="/img/beauty/folder.svg" alt="directories" width="17" class="menu-icon">
{$item['directory']} {$item['directory']}
<small class="badge">{$fileTotal}</small>
</a></li> </a></li>
eof; eof;
} }

Loading…
Cancel
Save