From 477953422b323b35056c403bfad01078fa894a39 Mon Sep 17 00:00:00 2001 From: filesite Date: Wed, 20 Nov 2024 18:28:12 +0800 Subject: [PATCH] date index function ready --- .../beauty/controller/CommandController.php | 98 +++++++++++++++++-- 1 file changed, 88 insertions(+), 10 deletions(-) diff --git a/themes/beauty/controller/CommandController.php b/themes/beauty/controller/CommandController.php index 4be36ec..bb5776b 100644 --- a/themes/beauty/controller/CommandController.php +++ b/themes/beauty/controller/CommandController.php @@ -8,6 +8,8 @@ require_once __DIR__ . '/../../../plugins/Common.php'; Class CommandController extends Controller { protected $logPrefix = '[MainBot]'; + protected $scanedDirCacheKey = 'MainBotScanedDirs'; + protected $dateIndexCacheKey = 'MainBotDateIndex'; public function actionIndex() { $commands = <<logPrefix; @@ -110,7 +118,11 @@ eof; $htmlCateReadme = ''; //当前目录下的Readme.md 内容 $menus_sorted = array(); //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序 + + //执行一次扫描任务 + $this->cleanScanCaches(); $this->scanMediaFiles(); + $this->saveDateIndexIntoCacheFile(); //while (true) { //$time = date('Y-m-d H:i:s'); @@ -118,7 +130,12 @@ eof; //sleep(3); //} + } + //清空内存中的临时缓存数据 + protected function cleanScanCaches() { + Common::setCache($this->scanedDirCacheKey, array()); + Common::setCache($this->dateIndexCacheKey, array()); } //扫描媒体文件:图片、视频、音乐 @@ -130,7 +147,7 @@ eof; $dirpath = $rootDir; } - echo "\n\n== Scanning directory {$dirpath} ...\n\n"; + echo "\n\n== Scanning directory {$dirpath} ...\n"; $scanner = new DirScanner(); $scanner->setWebRoot(FSC::$app['config']['content_directory']); $scanner->setRootDir($dirpath); @@ -139,13 +156,12 @@ eof; $dirTree = $scanner->scan($dirpath, $maxScanDeep); $scanResults = $scanner->getScanResults(); echo 'Total directories or files: ' . count($scanResults); - echo "\n\n"; + echo "\n"; $supportedImageExts = FSC::$app['config']['supportedImageExts']; $supportedVideoExts = FSC::$app['config']['supportedVideoExts']; $supportedAudioExts = FSC::$app['config']['supportedAudioExts']; - - $cacheKey = 'MainBotScanedDirs'; + $cacheKey = $this->scanedDirCacheKey; if (!empty($scanResults)) { $scanIndex = 0; @@ -160,22 +176,28 @@ 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); $this->scanMediaFiles($item['realpath']); } $scanIndex ++; + + //更新年份、月份时间索引 + if (!empty($item['filename'])) { + $this->updateDateIndex($scanner, $item); + } + + //更新扫描进度 $stats = $this->updateScanStats($dirpath, $scanTotal, $scanIndex); } @@ -191,7 +213,7 @@ eof; 'currentDir' => $dirpath, 'total' => $total, 'current' => $index, - 'percent' => round($index/$total, 2) * 100, + 'percent' => floor($index*100/$total), ); $botLogPrefix = $this->logPrefix; @@ -203,7 +225,7 @@ eof; $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"; + echo "{$botLogPrefix} Scan has finished {$stats['percent']}%, total {$stats['total']}, current {$stats['current']}\n"; //保存进度文件 file_put_contents($statsFile, json_encode($stats) . "\n"); @@ -222,6 +244,62 @@ eof; return $stats; } + //建立年份、月份时间索引 + /** + * 数据格式: + * {"y2024": {"m1": [id1, id2, ...], "m10": [id1, id2, ...]}} + */ + protected function updateDateIndex($scanner, $file) { + $ctime = !empty($file['original_ctime']) ? $file['original_ctime'] : $file['fstat']['ctime']; + + $cacheKey = $this->dateIndexCacheKey; + $cacheData = Common::getCache($cacheKey); + if (empty($cacheData)) { + $cacheData = array(); + } + + $year = 'y' . date('Y', $ctime); + $month = 'm' . date('m', $ctime); + if (empty($cacheData[$year])) { + $cacheData[$year] = array(); + } + if (empty($cacheData[$year][$month])) { + $cacheData[$year][$month] = array(); + } + + array_push($cacheData[$year][$month], $file['id']); + + return Common::setCache($cacheKey, $cacheData); + } + + protected function saveDateIndexIntoCacheFile() { + $cacheKey = $this->dateIndexCacheKey; + $cacheData = Common::getCache($cacheKey); + if (empty($cacheData)) { + return false; + } + + return Common::saveCacheToFile($cacheKey, $cacheData); + } + + //汇总每个目录下图片、视频、音乐文件数量 + /** + * 数据格式: + * {dirid: {image: 10, video: 20, audio: 0}, ...} + */ + protected function updateDirCounter() { + + } + + //归类没有original_ctime的图片、视频文件 + /** + * 数据格式: + * [id1, id2, ...] + */ + protected function saveNoOriginalCtimeFiles() { + + } + public function actionTest() { $cacheKey = 'TestSTData'; $time = Common::getCache($cacheKey);