setWebRoot(FSC::$app['config']['content_directory']); $rootDir = __DIR__ . '/../../../www/' . FSC::$app['config']['content_directory']; $scanner->setRootDir($rootDir); //优先从缓存读取数据 $defaultCateId = $scanner->getId(preg_replace("/\/$/", '', realpath($rootDir))); $maxScanDeep = 2; //最大扫描目录级数 $cacheKey = $this->getCacheKey($defaultCateId, 'tree', $maxScanDeep); $cachedData = Common::getCacheFromFile($cacheKey); if (!empty($cachedData)) { $dirTree = $cachedData; $scanner->setTreeData($cachedData); }else { $dirTree = $scanner->scan(__DIR__ . '/../../../www/' . FSC::$app['config']['content_directory'], $maxScanDeep); Common::saveCacheToFile($cacheKey, $dirTree); } //优先从缓存读取数据 $cacheKey = $cacheDataId = $this->getCacheKey($defaultCateId, 'data', $maxScanDeep); $cachedData = Common::getCacheFromFile($cacheKey); if (!empty($cachedData)) { $scanResults = $cachedData; $scanner->setScanResults($cachedData); }else { $scanResults = $scanner->getScanResults(); Common::saveCacheToFile($cacheKey, $scanResults); } //优先从缓存获取目录数据 $cacheKey = $this->getCacheKey('all', 'menu', $maxScanDeep); $menus = Common::getCacheFromFile($cacheKey); if (empty($menus)) { //获取目录 $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']; } Common::saveCacheToFile($cacheKey, $menus); //保存目录数据 } //当前目录数据 $cateId = $this->get('id', $menus[0]['id']); $subcate = $scanResults[$cateId]; //获取当前目录下的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($readmeFile['title'])) { $pageTitle = $readmeFile['title']; } if (!empty($subcate)) { $pageTitle = "{$subcate['directory']}照片,来自{$pageTitle}"; } $viewName = 'index'; $params = compact( 'cateId', 'page', 'pageSize', 'cacheDataId', 'dirTree', 'scanResults', 'menus', 'htmlReadme', 'htmlCateReadme', 'mp3File' ); return $this->render($viewName, $params, $pageTitle); } //清空缓存 public function actionCleancache() { $prefix = FSC::$app['config']['theme']; $cacheKey = "{$prefix}_allFilesTree"; Common::cleanFileCache($cacheKey); $cacheKey = "{$prefix}_allFilesData"; Common::cleanFileCache($cacheKey); $code = 1; $msg = 'OK'; return $this->renderJson(compact('code', 'msg')); } }