diff --git a/controller/Controller.php b/controller/Controller.php index aaf2fe7..9df6a8c 100644 --- a/controller/Controller.php +++ b/controller/Controller.php @@ -311,4 +311,17 @@ Class Controller { return "{$prefix}_{$dataType}_{$maxScanDeep}_{$cateId}"; } + protected function getCurrentWebroot($realpath) { + $realpath = preg_replace('/\/[^\/]+\.[^\/]+$/', '', $realpath); //删除文件名,只保留目录 + if (empty($realpath)) {return '/';} + + $webroot = FSC::$app['config']['content_directory']; + $arr = explode($webroot, $realpath); + if (count($arr) < 2) { + return $webroot; + } + + return "{$webroot}{$arr[1]}/"; + } + } diff --git a/lib/DirScanner.php b/lib/DirScanner.php index 5d08eca..98258b0 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -144,7 +144,9 @@ Class DirScanner { //根据文件路径生成唯一编号 public function getId($realpath) { - return !empty($realpath) ? md5($realpath) : ''; + if ($realpath == '/') {return md5($realpath);} + + return !empty($realpath) ? md5(preg_replace('/\/$/', '', $realpath)) : ''; } //判断Nginx防盗链MD5加密方式字符串是否合格 @@ -772,7 +774,7 @@ Class DirScanner { } } } - }else if (!empty($dirid) && !empty($this->scanResults)) { + }else if (!empty($dirid) && !empty($this->scanResults) && !empty($this->scanResults[$dirid])) { $directory = $this->scanResults[$dirid]; if (!empty($directory) && !empty($directory['files'])) { foreach($directory['files'] as $id => $file) { @@ -794,4 +796,29 @@ Class DirScanner { return $readme; } + //获取目录下第一个图片作为封面图返回 + public function getSnapshotImage($realpath, $imgExts = array('jpg', 'jpeg', 'png', 'webp', 'gif')) { + $iterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator($realpath, RecursiveDirectoryIterator::SKIP_DOTS), + RecursiveIteratorIterator::SELF_FIRST + ); + + $imgUrl = ''; + foreach ($iterator as $item) { + if ($item->isFile()) { + $extension = $item->getExtension(); + if (in_array(strtolower($extension), $imgExts)) { + $imgPath = $item->getPath() . '/' . $item->getFilename(); + $imgData = $this->getFileData($imgPath); + if (!empty($imgData['path'])) { + $imgUrl = $imgData['path']; + } + break; + } + } + } + + return $imgUrl; + } + } diff --git a/themes/beauty/controller/ListController.php b/themes/beauty/controller/ListController.php index a67ecbe..05d6525 100644 --- a/themes/beauty/controller/ListController.php +++ b/themes/beauty/controller/ListController.php @@ -36,11 +36,11 @@ Class ListController extends Controller { throw new Exception("缓存数据中找不到当前目录,请返回上一页重新进入!", 404); } - $scanner->setWebRoot($this->getCurrentWebroot($currentDir)); + $scanner->setWebRoot($this->getCurrentWebroot($currentDir['realpath'])); $scanner->setRootDir($currentDir['realpath']); //优先从缓存读取数据 - $maxScanDeep = 2; //最大扫描目录级数 + $maxScanDeep = 0; //最大扫描目录级数 $cacheKey = $this->getCacheKey($cateId, 'tree', $maxScanDeep); $cachedData = Common::getCacheFromFile($cacheKey); if (!empty($cachedData)) { @@ -63,20 +63,31 @@ Class ListController extends Controller { } //按照scanResults格式把当前目录扫描结果中的目录数据拼接到当前目录数据里: currentDir - if (!empty($currentDir['directories'])) { - foreach ($currentDir['directories'] as $id => $item) { - if (empty($scanResults[$id]['directories'])) { - continue; + if (!empty($scanResults)) { + $dirs = array(); + $files = array(); + foreach ($scanResults as $id => $item) { + if (!empty($item['directory'])) { + array_push($dirs, $item); + }else { + array_push($files, $item); } + } + + if (!empty($dirs)) { + $currentDir['directories'] = $dirs; + } - $currentDir['directories'][$id] = $scanResults[$id]; + if (!empty($files)) { + $currentDir['files'] = $files; } + + $scanResults = array($cateId => $currentDir); //重新组装数据 } - $scanResults = array($cateId => $currentDir); //重新组装数据 //非首页统一从缓存获取目录数据,有效期 1 小时 - $cacheKey = $this->getCacheKey('all', 'menu', 2); + $cacheKey = $this->getCacheKey('all', 'menu', $maxScanDeep); $menus = Common::getCacheFromFile($cacheKey, 3600); @@ -94,11 +105,10 @@ Class ListController extends Controller { } //获取默认mp3文件 - $rootCateId = $this->get('id', ''); - $mp3File = $scanner->getDefaultFile('mp3', $rootCateId); - if (empty($mp3File)) { - $mp3File = $scanner->getDefaultFile('mp3'); - } + //优先从缓存获取默认mp3文件 + $cacheKey = $this->getCacheKey('root', 'mp3', $maxScanDeep); + $mp3File = Common::getCacheFromFile($cacheKey); + //翻页支持 $page = $this->get('page', 1); @@ -152,16 +162,6 @@ Class ListController extends Controller { return $parent; } - protected function getCurrentWebroot($currentDir) { - $webroot = FSC::$app['config']['content_directory']; - $arr = explode($webroot, $currentDir['realpath']); - if (count($arr) < 2) { - return $webroot; - } - - return "{$webroot}{$arr[1]}/"; - } - //根据目录结构以及当前目录获取面包屑 //缓存key统一生成,方便按规则获取上一级目录的缓存cid protected function getBreadcrumbs($currentDir, $scanResults, $scanner) { @@ -183,7 +183,8 @@ Class ListController extends Controller { //下一级子目录的id $parentCateId = $scanner->getId($parentCate); - $cacheKey = $this->getCacheKey($parentCateId, 'data'); + $maxScanDeep = 0; //所有页面扫描深度都为 1 + $cacheKey = $this->getCacheKey($parentCateId, 'data', $maxScanDeep); array_push($breads, [ 'id' => $cateId, diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index c4aec73..6fb669b 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -23,7 +23,7 @@ Class SiteController extends Controller { //优先从缓存读取数据 $defaultCateId = $scanner->getId(preg_replace("/\/$/", '', realpath($rootDir))); - $maxScanDeep = 2; //最大扫描目录级数 + $maxScanDeep = 0; //最大扫描目录级数 $cacheKey = $this->getCacheKey($defaultCateId, 'tree', $maxScanDeep); $cachedData = Common::getCacheFromFile($cacheKey); if (!empty($cachedData)) { @@ -45,15 +45,22 @@ Class SiteController extends Controller { Common::saveCacheToFile($cacheKey, $scanResults); } - //优先从缓存获取目录数据 $cacheKey = $this->getCacheKey('all', 'menu', $maxScanDeep); $menus = Common::getCacheFromFile($cacheKey); - if (empty($menus)) { + if (empty($menus) && !empty($scanResults)) { //获取目录 $menus = $scanner->getMenus(); + //在path网址中追加cid缓存key参数 + if (!empty($menus)) { + foreach ($menus as $index => $menu) { + $menus[$index]['cid'] = $cacheDataId; + $menus[$index]['path'] .= "&cid={$cacheDataId}"; + } + } + $titles = array(); $readmeFile = $scanner->getDefaultReadme(); if (!empty($readmeFile)) { @@ -80,12 +87,8 @@ Class SiteController extends Controller { } - //当前目录数据 - $cateId = $this->get('id', $menus[0]['id']); - $subcate = $scanResults[$cateId]; - //获取当前目录下的readme - $cateReadmeFile = $scanner->getDefaultReadme($cateId); + $cateReadmeFile = $scanner->getDefaultReadme($defaultCateId); if (!empty($cateReadmeFile)) { $Parsedown = new Parsedown(); $content = file_get_contents($cateReadmeFile['realpath']); @@ -93,11 +96,14 @@ Class SiteController extends Controller { $htmlCateReadme = $scanner->fixMDUrls($cateReadmeFile['realpath'], $htmlCateReadme); } - //获取默认mp3文件 - $rootCateId = $this->get('id', ''); - $mp3File = $scanner->getDefaultFile('mp3', $rootCateId); + //优先从缓存获取默认mp3文件 + $cacheKey = $this->getCacheKey('root', 'mp3', $maxScanDeep); + $mp3File = Common::getCacheFromFile($cacheKey); if (empty($mp3File)) { - $mp3File = $scanner->getDefaultFile('mp3'); + $mp3File = $scanner->getDefaultFile('mp3', $defaultCateId); + if (!empty($mp3File)) { + Common::saveCacheToFile($cacheKey, $mp3File); + } } @@ -114,13 +120,13 @@ Class SiteController extends Controller { } $viewName = 'index'; $params = compact( - 'cateId', 'page', 'pageSize', 'cacheDataId', + 'page', 'pageSize', 'cacheDataId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'mp3File' ); return $this->render($viewName, $params, $pageTitle); } - //清空缓存 + //TODO: 清空所有缓存 public function actionCleancache() { $prefix = FSC::$app['config']['theme']; $cacheKey = "{$prefix}_allFilesTree"; @@ -134,4 +140,35 @@ Class SiteController extends Controller { return $this->renderJson(compact('code', 'msg')); } + //根据目录id,获取第一张图网址作为封面图返回 + public function actionDirsnap() { + $code = 1; + $msg = 'OK'; + $url = ''; + + $cacheId = $this->post('cid', ''); + $cateId = $this->post('id', ''); + if (empty($cacheId) || empty($cateId)) { + $code = 0; + $msg = '参数不能为空'; + }else { + //从缓存数据中获取目录的realpath + $cachedData = Common::getCacheFromFile($cacheId); + if (!empty($cachedData)) { + $realpath = $cachedData[$cateId]['realpath']; + $scanner = new DirScanner(); + $scanner->setWebRoot($this->getCurrentWebroot($realpath)); + $scanner->setRootDir($realpath); + + $imgExts = !empty(FSC::$app['config']['supportedImageExts']) ? FSC::$app['config']['supportedImageExts'] : array('jpg', 'jpeg', 'png', 'webp', 'gif'); + $url = $scanner->getSnapshotImage($realpath, $imgExts); + }else { + $code = 0; + $msg = '缓存数据已失效,请刷新网页'; + } + } + + return $this->renderJson(compact('code', 'msg', 'url')); + } + } diff --git a/themes/beauty/views/site/index.php b/themes/beauty/views/site/index.php index cd83432..945ac15 100644 --- a/themes/beauty/views/site/index.php +++ b/themes/beauty/views/site/index.php @@ -34,13 +34,13 @@