diff --git a/lib/DirScanner.php b/lib/DirScanner.php index 7cdd69b..b835b12 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -144,11 +144,7 @@ Class DirScanner { } //根据文件路径生成唯一编号 - //使用相对路径计算md5值 - private function getId($realpath) { - if (!empty($this->rootDir)) { - $realpath = str_replace($this->rootDir, '', $realpath); - } + public function getId($realpath) { return !empty($realpath) ? md5($realpath) : ''; } diff --git a/themes/beauty/controller/ListController.php b/themes/beauty/controller/ListController.php index 1021b79..d0722c3 100644 --- a/themes/beauty/controller/ListController.php +++ b/themes/beauty/controller/ListController.php @@ -9,6 +9,13 @@ require_once __DIR__ . '/../../../plugins/Common.php'; Class ListController extends Controller { public function actionIndex() { + $cateId = $this->get('id', ''); + $cacheParentDataId = $this->get('cid', ''); + if (empty($cateId) || empty($cacheParentDataId)) { + throw new Exception("参数缺失!", 403); + } + + //获取数据 $menus = array(); //菜单,一级目录 $htmlReadme = ''; //Readme.md 内容,底部网站详细介绍 @@ -16,23 +23,42 @@ Class ListController extends Controller { $menus_sorted = array(); //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序 $scanner = new DirScanner(); - $scanner->setWebRoot(FSC::$app['config']['content_directory']); - $scanner->setRootDir(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory']); + + //TODO: 根据参数cid获取id对应的目录realpath,从而只扫描这个目录 + $cacheSeconds = 3600; + $cachedParentData = Common::getCacheFromFile($cacheParentDataId, $cacheSeconds); + if (empty($cachedParentData)) { + throw new Exception("缓存已过期,请返回上一页重新进入!", 404); + } + + $currentDir = $cachedParentData[$cateId]; + if (empty($currentDir)) { + throw new Exception("缓存数据中找不到当前目录,请返回上一页重新进入!", 404); + } + + //$scanner->setWebRoot(FSC::$app['config']['content_directory']); + $scanner->setWebRoot($this->getCurrentWebroot($currentDir)); + $scanner->setRootDir($currentDir['realpath']); + //header('Content-Type: text/html; charset=utf-8'); + //echo $this->getCurrentWebroot($currentDir) . "\n"; + //echo $currentDir['realpath'] . "\n"; + //exit; //优先从缓存读取数据 - $prefix = FSC::$app['config']['theme']; - $cacheKey = "{$prefix}_allFilesTree"; + $prefix = FSC::$app['config']['theme'] . '_v1.0'; + $maxScanDeep = 2; //最大扫描目录级数 + $cacheKey = "{$prefix}_ltree_{$maxScanDeep}_{$cateId}"; $cachedData = Common::getCacheFromFile($cacheKey); if (!empty($cachedData)) { $dirTree = $cachedData; $scanner->setTreeData($cachedData); }else { - $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4); + $dirTree = $scanner->scan($currentDir['realpath'], $maxScanDeep); Common::saveCacheToFile($cacheKey, $dirTree); } //优先从缓存读取数据 - $cacheKey = "{$prefix}_allFilesData"; + $cacheKey = $cacheDataId = "{$prefix}_ldata_{$maxScanDeep}_{$cateId}"; $cachedData = Common::getCacheFromFile($cacheKey); if (!empty($cachedData)) { $scanResults = $cachedData; @@ -42,6 +68,23 @@ Class ListController extends Controller { Common::saveCacheToFile($cacheKey, $scanResults); } + //按照scanResults格式把当前目录扫描结果中的目录数据拼接到当前目录数据里: currentDir + if (!empty($currentDir['directories'])) { + foreach ($currentDir['directories'] as $id => $item) { + if (empty($scanResults[$id]['directories'])) { + continue; + } + + $currentDir['directories'][$id] = $scanResults[$id]; + } + } + $scanResults = array($cateId => $currentDir); //重新组装数据 + + //header('Content-Type: text/html; charset=utf-8'); + //print_r($currentDir); + //print_r($scanResults); + //exit; + //获取目录 $menus = $scanner->getMenus(); @@ -69,16 +112,9 @@ Class ListController extends Controller { } //获取目录面包屑 - $cateId = $this->get('id', $menus[0]['id']); + //TODO: 重写此方法,根据scanner->getId()方法来逐级目录生成 $subcate = $scanResults[$cateId]; - $breadcrumbs = $this->getBreadcrumbs($menus, $subcate); - -/* -header('Content-type: text/html; charset=utf-8'); -print_r($subcate); -print_r($breadcrumbs); -print_r($menus);exit; -*/ + $breadcrumbs = $this->getBreadcrumbs($currentDir, $cachedParentData, $scanner); //获取当前目录下的readme $cateReadmeFile = $scanner->getDefaultReadme($cateId); @@ -110,7 +146,7 @@ print_r($menus);exit; $viewName = '//site/index'; //共享视图 $params = compact( 'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme', - 'mp3File', 'page', 'pageSize' + 'mp3File', 'page', 'pageSize', 'cacheDataId', 'cacheParentDataId' ); return $this->render($viewName, $params, $pageTitle); } @@ -148,29 +184,49 @@ print_r($menus);exit; 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]}/"; + } + //根据目录结构以及当前目录获取面包屑 - protected function getBreadcrumbs($menus, $subcate) { + protected function getBreadcrumbs($currentDir, $scanResults, $scanner) { + $webroot = FSC::$app['config']['content_directory']; + $arr = explode($webroot, $currentDir['realpath']); $breads = array(); - array_push($breads, [ - 'id' => $subcate['id'], - 'name' => $subcate['directory'], - 'url' => $subcate['path'], - ]); + if (count($arr) < 2) { + return $breads; + } - $pid = $subcate['pid']; - $parentCate = $this->getParentCateByPid($pid, $menus); - while (!empty($parentCate)) { - array_unshift($breads, [ - 'id' => $parentCate['id'], - 'name' => $parentCate['directory'], - 'url' => $parentCate['path'], + $cates = explode('/', $arr[1]); + $parentCate = "{$arr[0]}{$webroot}"; + foreach ($cates as $cate) { + if ($cate == $currentDir['directory']) {break;} + + $subcate = "{$parentCate}{$cate}"; + $cateId = $scanner->getId($subcate); + array_push($breads, [ + 'id' => $cateId, + 'name' => $cate, + 'url' => $scanResults[$cateId]['path'], ]); - $pid = $parentCate['pid']; - $parentCate = $this->getParentCateByPid($pid, $menus); + $parentCate = "{$subcate}/"; //记录上一级目录 } + //最后一级 + array_push($breads, [ + 'id' => $currentDir['id'], + 'name' => $currentDir['directory'], + 'url' => $currentDir['path'], + ]); + return $breads; } diff --git a/themes/beauty/controller/SiteController.php b/themes/beauty/controller/SiteController.php index db693c4..b550133 100644 --- a/themes/beauty/controller/SiteController.php +++ b/themes/beauty/controller/SiteController.php @@ -21,18 +21,19 @@ Class SiteController extends Controller { //优先从缓存读取数据 $prefix = FSC::$app['config']['theme']; - $cacheKey = "{$prefix}_allFilesTree"; + $maxScanDeep = 2; //最大扫描目录级数 + $cacheKey = "{$prefix}_tree_{$maxScanDeep}_" . md5(FSC::$app['config']['content_directory']); $cachedData = Common::getCacheFromFile($cacheKey); if (!empty($cachedData)) { $dirTree = $cachedData; $scanner->setTreeData($cachedData); }else { - $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], 4); + $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], $maxScanDeep); Common::saveCacheToFile($cacheKey, $dirTree); } //优先从缓存读取数据 - $cacheKey = "{$prefix}_allFilesData"; + $cacheKey = $cacheDataId = "{$prefix}_data_{$maxScanDeep}_" . md5(FSC::$app['config']['content_directory']); $cachedData = Common::getCacheFromFile($cacheKey); if (!empty($cachedData)) { $scanResults = $cachedData; @@ -101,7 +102,7 @@ Class SiteController extends Controller { } $viewName = 'index'; $params = compact( - 'cateId', 'page', 'pageSize', + 'cateId', 'page', 'pageSize', 'cacheDataId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'mp3File' ); return $this->render($viewName, $params, $pageTitle); diff --git a/themes/beauty/views/site/index.php b/themes/beauty/views/site/index.php index 8a0de13..2f672eb 100644 --- a/themes/beauty/views/site/index.php +++ b/themes/beauty/views/site/index.php @@ -61,7 +61,7 @@ eof; foreach ($breadcrumbs as $bread) { if ($bread['id'] != $selectedId) { echo <<{$bread['name']} / + {$bread['name']} / eof; } else { echo << - + eof; if (!empty($dir['snapshot'])) { @@ -194,7 +194,7 @@ eof; if ($index > 0) { echo << - + {$file['filename']}