Browse Source

add function to save file number of dirs

master
filesite 1 month ago
parent
commit
9517acc9cd
  1. 1
      README.md
  2. 81
      themes/beauty/controller/CommandController.php

1
README.md

@ -223,6 +223,7 @@ https://git.filesite.io/filesite/machete/archive/master.zip @@ -223,6 +223,7 @@ https://git.filesite.io/filesite/machete/archive/master.zip
补充说明:
由于按年、月归类文件功能需要在服务器端执行php脚本来扫描图片、视频等,手动部署后需要进入项目根目录并执行以下命令扫描所有图片、视频:
```
php bin/command.php mainBot

81
themes/beauty/controller/CommandController.php

@ -10,8 +10,10 @@ Class CommandController extends Controller { @@ -10,8 +10,10 @@ Class CommandController extends Controller {
protected $logPrefix = '[MainBot]';
protected $scanedDirCacheKey = 'MainBotScanedDirs';
protected $dateIndexCacheKey = 'MainBotDateIndex'; //索引数据的key单独缓存,缓存key为此{cacheKey}_keys
protected $dirCounterCacheKey = 'MainBotDirCounter'; //缓存所有目录包含的文件数量
protected $noOriginalCtimeFilesCacheKey = 'MainBotNoOriginalCtimeFiles';
protected $allFilesCacheKey = 'MainBotAllFiles';
protected $allDirTreeCacheKey = 'MainBotAllDirTree';
public function actionIndex() {
$commands = <<<eof
@ -128,6 +130,8 @@ eof; @@ -128,6 +130,8 @@ eof;
$this->saveNoOriginalCtimeFilesIntoFile();
//缓存所有文件id跟文件信息,便于根据id列表来渲染,并按id首字母分子目录存放,以支持大量文件的场景
$this->saveAllFilesIntoCacheFile();
//缓存所有目录的文件数量
$this->saveDirCounter();
//TODO: 保存所有目录下文件数量统计
//按年、月保存文件数据,以便按年、月显示
@ -168,6 +172,8 @@ eof; @@ -168,6 +172,8 @@ eof;
$maxScanDeep = 0; //最大扫描目录级数
$dirTree = $scanner->scan($dirpath, $maxScanDeep);
$this->updateAllDirTreeCache($dirTree);
$scanResults = $scanner->getScanResults();
echo 'Total directories or files: ' . count($scanResults);
echo "\n";
@ -403,13 +409,86 @@ eof; @@ -403,13 +409,86 @@ eof;
return true;
}
protected function updateAllDirTreeCache($dirTree) {
$cacheKey = $this->allDirTreeCacheKey;
$cacheData = Common::getCache($cacheKey);
if (empty($cacheData)) {
$cacheData = array();
}
$cacheData = array_merge($cacheData, $dirTree);
$pid = '';
$imgNum = $videoNum = $audioNum = 0;
$supportedImageExts = FSC::$app['config']['supportedImageExts'];
$supportedVideoExts = FSC::$app['config']['supportedVideoExts'];
$supportedAudioExts = FSC::$app['config']['supportedAudioExts'];
foreach ($dirTree as $id => $item) {
if (empty($item['pid'])) {
break;
}
$pid = $item['pid'];
if (
!empty($item['filename']) && in_array($item['extension'], $supportedImageExts)
) {
$imgNum ++;
}else if (
!empty($item['filename']) && in_array($item['extension'], $supportedVideoExts)
) {
$videoNum ++;
}else if (
!empty($item['filename']) && in_array($item['extension'], $supportedAudioExts)
) {
$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;
}
return Common::setCache($cacheKey, $cacheData);
}
//汇总每个目录下图片、视频、音乐文件数量
/**
* 数据格式:
* {dirid: {image: 10, video: 20, audio: 0}, ...}
*/
protected function updateDirCounter() {
protected function saveDirCounter() {
$cacheKey = $this->allDirTreeCacheKey;
$cacheData = Common::getCache($cacheKey);
if (empty($cacheData)) {
return false;
}
$dirCounter = array();
foreach ($cacheData as $id => $item) {
if ( !empty($item['directory']) && isset($item['image_total']) ) {
$dirCounter[$id] = array(
'image_total' => $item['image_total'],
'video_total' => $item['video_total'],
'audio_total' => $item['audio_total'],
);
}
}
$saveKey = $this->dirCounterCacheKey;
$cacheDir = 'index';
Common::saveCacheToFile($saveKey, $dirCounter, $cacheDir);
}
//归类没有original_ctime的图片、视频文件

Loading…
Cancel
Save