get('id', ''); $cacheParentDataId = $this->get('cid', ''); if (empty($cateId) || empty($cacheParentDataId)) { throw new Exception("参数缺失!", 403); } //获取数据 $menus = array(); //菜单,一级目录 $htmlReadme = ''; //Readme.md 内容,底部网站详细介绍 $htmlCateReadme = ''; //当前目录下的Readme.md 内容 $menus_sorted = array(); //Readme_sort.txt 说明文件内容,一级目录菜单从上到下的排序 $scanner = new DirScanner(); //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'] . '_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($currentDir['realpath'], $maxScanDeep); Common::saveCacheToFile($cacheKey, $dirTree); } //优先从缓存读取数据 $cacheKey = $cacheDataId = "{$prefix}_ldata_{$maxScanDeep}_{$cateId}"; $cachedData = Common::getCacheFromFile($cacheKey); if (!empty($cachedData)) { $scanResults = $cachedData; $scanner->setScanResults($cachedData); }else { $scanResults = $scanner->getScanResults(); 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(); $titles = array(); $readmeFile = $scanner->getDefaultReadme(); if (!empty($readmeFile)) { if (!empty($readmeFile['sort'])) { $menus_sorted = explode("\n", $readmeFile['sort']); } $titles = $scanner->getMDTitles($readmeFile['id']); $Parsedown = new Parsedown(); $content = file_get_contents($readmeFile['realpath']); $htmlReadme = $Parsedown->text($content); $htmlReadme = $scanner->fixMDUrls($readmeFile['realpath'], $htmlReadme); } //排序 $sortedTree = $this->sortMenusAndDirTree($menus_sorted, $menus, $dirTree); if (!empty($sortedTree)) { $menus = $sortedTree['menus']; $dirTree = $sortedTree['dirTree']; } //获取目录面包屑 //TODO: 重写此方法,根据scanner->getId()方法来逐级目录生成 $subcate = $scanResults[$cateId]; $breadcrumbs = $this->getBreadcrumbs($currentDir, $cachedParentData, $scanner); //获取当前目录下的readme $cateReadmeFile = $scanner->getDefaultReadme($cateId); if (!empty($cateReadmeFile)) { $Parsedown = new Parsedown(); $content = file_get_contents($cateReadmeFile['realpath']); $htmlCateReadme = $Parsedown->text($content); $htmlCateReadme = $scanner->fixMDUrls($cateReadmeFile['realpath'], $htmlCateReadme); } //获取默认mp3文件 $rootCateId = $this->get('id', ''); $mp3File = $scanner->getDefaultFile('mp3', $rootCateId); if (empty($mp3File)) { $mp3File = $scanner->getDefaultFile('mp3'); } //翻页支持 $page = $this->get('page', 1); $pageSize = $this->get('limit', 24); $pageTitle = !empty($titles) ? $titles[0]['name'] : "FileSite.io - 无数据库、基于文件和目录的Markdown文档、网址导航、图书、图片、视频网站PHP开源系统"; if (!empty($subcate)) { $pageTitle = "{$subcate['directory']}的照片,来自{$pageTitle}"; if (!empty($subcate['title'])) { $pageTitle = $subcate['title']; } } $viewName = '//site/index'; //共享视图 $params = compact( 'cateId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'breadcrumbs', 'htmlCateReadme', 'mp3File', 'page', 'pageSize', 'cacheDataId', 'cacheParentDataId' ); return $this->render($viewName, $params, $pageTitle); } //实现php 5.5开始支持的array_column方法 protected function array_column($arr, $col) { $out = array(); if (!empty($arr) && is_array($arr) && !empty($col)) { foreach ($arr as $index => $item) { if (!empty($item[$col])) { array_push($out, $item[$col]); } } } return $out; } //根据pid在目录数组里找出对应的父目录数据 protected function getParentCateByPid($pid, $cates) { $parent = array(); foreach ($cates as $index => $item) { if ($item['id'] == $pid) { $parent = $item; break; }else if (!empty($item['directories'])) { $parent = $this->getParentCateByPid($pid, $item['directories']); if (!empty($parent)) {break;} } } 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($currentDir, $scanResults, $scanner) { $webroot = FSC::$app['config']['content_directory']; $arr = explode($webroot, $currentDir['realpath']); $breads = array(); if (count($arr) < 2) { return $breads; } $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'], ]); $parentCate = "{$subcate}/"; //记录上一级目录 } //最后一级 array_push($breads, [ 'id' => $currentDir['id'], 'name' => $currentDir['directory'], 'url' => $currentDir['path'], ]); return $breads; } }