From 317c36972fb09a9a3e66d3b17b002572e7fa91f2 Mon Sep 17 00:00:00 2001 From: filesite Date: Thu, 14 Nov 2024 19:16:38 +0800 Subject: [PATCH] mainbot update --- lib/DirScanner.php | 6 +- plugins/Common.php | 10 +++ .../beauty/controller/CommandController.php | 73 +++++++++++++++++-- 3 files changed, 81 insertions(+), 8 deletions(-) diff --git a/lib/DirScanner.php b/lib/DirScanner.php index ba86c6c..b937420 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -59,12 +59,12 @@ Class DirScanner { 'm3u8', //视频 ); - protected $exifSupportFileExtensions = array( + public $exifSupportFileExtensions = array( 'jpg', //图片 'jpeg', //图片 ); - protected $iptcSupportFileExtensions = array( + public $iptcSupportFileExtensions = array( 'jpg', //图片 'jpeg', //图片 ); @@ -277,7 +277,7 @@ Class DirScanner { try { if (function_exists('exif_read_data')) { - $exif = exif_read_data($realpath, 0, true); + $exif = @exif_read_data($realpath, 0, true); if(!empty($exif['EXIF']['DateTimeOriginal'])) { $createTime = strtotime($exif['EXIF']['DateTimeOriginal']); } diff --git a/plugins/Common.php b/plugins/Common.php index 8abf43d..5cba232 100644 --- a/plugins/Common.php +++ b/plugins/Common.php @@ -3,6 +3,8 @@ * 常用的公用方法 */ Class Common { + public static $cache = array(); + public static function cleanSpecialChars($str) { if (empty($str)) {return $str;} @@ -783,4 +785,12 @@ Class Common { return !empty($newArr) ? $newArr : $sorted; } + public static function setCache($key, $val) { + self::$cache[$key] = $val; + } + + public static function getCache($key) { + return !empty(self::$cache[$key]) ? self::$cache[$key] : null; + } + } \ No newline at end of file diff --git a/themes/beauty/controller/CommandController.php b/themes/beauty/controller/CommandController.php index 78ee062..97c05dd 100644 --- a/themes/beauty/controller/CommandController.php +++ b/themes/beauty/controller/CommandController.php @@ -2,6 +2,10 @@ /** * Command Controller */ +require_once __DIR__ . '/../../../lib/DirScanner.php'; +require_once __DIR__ . '/../../../plugins/Parsedown.php'; +require_once __DIR__ . '/../../../plugins/Common.php'; + Class CommandController extends Controller { public function actionIndex() { @@ -96,16 +100,75 @@ eof; //服务器端机器人程序,负责图片、视频文件拍摄时间等信息扫描,并缓存结果供前端使用 public function actionMainBot() { $thisTime = date('Y-m-d H:i:s'); - echo "[MainBot] Main bot started @{$thisTime}\n"; + $botLogPrefix = '[MainBot]'; + echo "{$botLogPrefix} Main bot started @{$thisTime}\n"; + + + $menus = array(); //菜单,一级目录 + $htmlReadme = array(); //Readme.md 内容,底部网站详细介绍 + $htmlCateReadme = ''; //当前目录下的Readme.md 内容 + $menus_sorted = array(); //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序 + $this->scanMediaFiles(); - while (true) { - $time = date('Y-m-d H:i:s'); - echo "[MainBot] {$time}\n"; + //while (true) { + //$time = date('Y-m-d H:i:s'); + //echo "{$botLogPrefix} {$time}\n"; + + //sleep(3); + //} + + } - sleep(3); + protected function scanMediaFiles($dirpath = '') { + $rootDir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory']; + if (empty($dirpath)) { + $dirpath = $rootDir; } + echo "\n\n== Scanning directory {$dirpath} ...\n\n"; + $scanner = new DirScanner(); + $scanner->setWebRoot(FSC::$app['config']['content_directory']); + $scanner->setRootDir($dirpath); + + $maxScanDeep = 0; //最大扫描目录级数 + $dirTree = $scanner->scan($dirpath, $maxScanDeep); + $scanResults = $scanner->getScanResults(); + echo 'Total directories or files: ' . count($scanResults); + echo "\n\n"; + + $supportedImageExts = FSC::$app['config']['supportedImageExts']; + $supportedVideoExts = FSC::$app['config']['supportedVideoExts']; + $supportedAudioExts = FSC::$app['config']['supportedAudioExts']; + + $cacheKey = 'MainBotScanedDirs'; + + if (!empty($scanResults)) { + foreach ($scanResults as $id => $item) { + $hadScanedDirs = Common::getCache($cacheKey); + + if ( + !empty($item['filename']) + && empty($item['original_ctime']) + && in_array($item['extension'], $supportedImageExts) + && !in_array($item['extension'], $scanner->exifSupportFileExtensions) + ) { + 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"; + }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']); + } + } + } } }