Browse Source

improve dir scan

master
filesite 5 months ago
parent
commit
6eec84cb64
  1. 6
      lib/DirScanner.php
  2. 118
      themes/beauty/controller/ListController.php
  3. 9
      themes/beauty/controller/SiteController.php
  4. 8
      themes/beauty/views/site/index.php

6
lib/DirScanner.php

@ -144,11 +144,7 @@ Class DirScanner { @@ -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) : '';
}

118
themes/beauty/controller/ListController.php

@ -9,6 +9,13 @@ require_once __DIR__ . '/../../../plugins/Common.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 { @@ -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 { @@ -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 { @@ -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; @@ -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; @@ -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;
}

9
themes/beauty/controller/SiteController.php

@ -21,18 +21,19 @@ Class SiteController extends Controller { @@ -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 { @@ -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);

8
themes/beauty/views/site/index.php

@ -61,7 +61,7 @@ eof; @@ -61,7 +61,7 @@ eof;
foreach ($breadcrumbs as $bread) {
if ($bread['id'] != $selectedId) {
echo <<<eof
<a href="{$bread['url']}">{$bread['name']}</a> /
<a href="{$bread['url']}&cid={$viewData['cacheParentDataId']}">{$bread['name']}</a> /
eof;
} else {
echo <<<eof
@ -104,7 +104,7 @@ eof; @@ -104,7 +104,7 @@ eof;
foreach ($category['directories'] as $dir) {
echo <<<eof
<div class="im_item bor_radius col-xs-6 col-sm-4 col-md-3 col-lg-2">
<a href="{$dir['path']}" class="bor_radius">
<a href="{$dir['path']}&cid={$viewData['cacheDataId']}" class="bor_radius">
eof;
if (!empty($dir['snapshot'])) {
@ -194,7 +194,7 @@ eof; @@ -194,7 +194,7 @@ eof;
if ($index > 0) {
echo <<<eof
<div class="im_item bor_radius col-xs-6 col-sm-4 col-md-3 col-lg-2">
<a href="javascript:;" class="bor_radius" data-fancybox="gallery" data-src="{$file['path']}" data-caption="{$title}" title="{$title}">
<a href="javascript:;" class="bor_radius" data-fancybox="gallery" data-src="{$file['path']}" data-caption="{$title} - {$file['filename']}" title="{$title} - {$file['filename']}">
<img src="/img/beauty/lazy.svg" data-original="{$file['path']}" class="bor_radius im_img lazy" alt="{$file['filename']}">
<div class="im_img_title">
<span>
@ -208,7 +208,7 @@ eof; @@ -208,7 +208,7 @@ eof;
} else {
echo <<<eof
<div class="im_item bor_radius col-xs-6 col-sm-4 col-md-3 col-lg-2">
<a href="javascript:;" class="bor_radius" data-fancybox="gallery" data-src="{$file['path']}" data-caption="{$title}" title="{$title}">
<a href="javascript:;" class="bor_radius" data-fancybox="gallery" data-src="{$file['path']}" data-caption="{$title} - {$file['filename']}" title="{$title} - {$file['filename']}">
<img src="{$file['path']}" class="bor_radius im_img" alt="{$file['filename']}">
<div class="im_img_title">
<span>

Loading…
Cancel
Save