Browse Source

function scan update

master
filesite 3 years ago
parent
commit
d3bf5e04c5
  1. 91
      lib/DirScanner.php
  2. 5
      test/DirScannerTest.php

91
lib/DirScanner.php

@ -73,8 +73,8 @@ Class DirScanner {
//解析描述文件内容 //解析描述文件内容
private function parseDescriptionFiles($realpath) { private function parseDescriptionFiles($realpath) {
$field = preg_replace('/^.+_([a-z0-9]+)\.txt$/i', "$1", $realpath); $pathinfo = pathinfo($realpath);
if ($field == $realpath) {return false;} $field = preg_replace('/^.+_/', '', $pathinfo['filename']);
$content = file_get_contents($realpath); $content = file_get_contents($realpath);
$data = []; $data = [];
$data[$field] = $content; $data[$field] = $content;
@ -126,13 +126,22 @@ Class DirScanner {
//根据路径生成目录数组 //根据路径生成目录数组
private function getDirData($realpath, $files) { private function getDirData($realpath, $files) {
$id = $this->getId($realpath); $id = $this->getId($realpath);
return [ $data = [
'id' => $id, 'id' => $id,
'directory' => basename($realpath), 'directory' => basename($realpath),
'realpath' => $realpath, 'realpath' => $realpath,
'path' => $this->getDirPath($realpath), 'path' => $this->getDirPath($realpath),
'files' => $files, 'files' => $files,
]; ];
//try to merge description data
if (!empty($files[$id])) {
$data = array_merge($data, $files[$id]);
unset($files[$id]);
$data['files'] = $files;
}
return $data;
} }
//根据路径生成文件数组,兼容URL文件 //根据路径生成文件数组,兼容URL文件
@ -143,7 +152,7 @@ Class DirScanner {
fclose($fp); fclose($fp);
$pathinfo = pathinfo($realpath); $pathinfo = pathinfo($realpath);
$extension = strtolower($pathinfo['extension']); $extension = strtolower($pathinfo['extension']);
return [ $data = [
'id' => $id, 'id' => $id,
'filename' => $pathinfo['filename'], 'filename' => $pathinfo['filename'],
'extension' => $extension, 'extension' => $extension,
@ -156,6 +165,12 @@ Class DirScanner {
'realpath' => $realpath, 'realpath' => $realpath,
'path' => $this->getFilePath( $this->getDirectoryName($pathinfo['dirname'], $dirLevel), $pathinfo['filename'], $extension ), 'path' => $this->getFilePath( $this->getDirectoryName($pathinfo['dirname'], $dirLevel), $pathinfo['filename'], $extension ),
]; ];
if ($extension == 'url') {
$data['shortcut'] = $this->parseShortCuts($realpath, $pathinfo['filename']);
}
return $data;
} }
//根据路径和根目录获取当前扫描的目录深度 //根据路径和根目录获取当前扫描的目录深度
@ -183,6 +198,44 @@ Class DirScanner {
return implode('/', array_splice($names, -$dirLevel)); return implode('/', array_splice($names, -$dirLevel));
} }
//合并描述文件内容到md文件或者目录数据
private function mergeDescriptionData($realpath) {
$data = [];
$ext = $this->parseDescriptionFiles($realpath);
//try to find the md file
$targetFile = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.md', $realpath);
if (file_exists($targetFile)) {
$fileId = $this->getId($targetFile);
if (empty($this->scanResults[$fileId])) {
$ext['id'] = $fileId;
$this->scanResults[$fileId] = $ext;
$data = $ext;
}else {
$data = $this->scanResults[$fileId];
$data = array_merge($data, $ext);
$this->scanResults[$fileId] = $data;
}
}else {
//try to merge to the parent directory
$targetDir = preg_replace('/\/[a-z0-9]+\.txt$/i', '', $realpath);
if (is_dir($targetDir)) {
$dirId = $this->getId($targetDir);
if (empty($this->scanResults[$dirId])) {
$ext['id'] = $dirId;
$this->scanResults[$dirId] = $ext;
$data = $ext;
}else {
$data = $this->scanResults[$dirId];
$data = array_merge($data, $ext);
$this->scanResults[$dirId] = $data;
}
}
}
return $data;
}
//根据文件生成防盗链网址 //根据文件生成防盗链网址
//参考:https://nginx.org/en/docs/http/ngx_http_secure_link_module.html#secure_link //参考:https://nginx.org/en/docs/http/ngx_http_secure_link_module.html#secure_link
@ -370,22 +423,46 @@ Class DirScanner {
foreach($files as $file) { foreach($files as $file) {
if (in_array($file, $ignore_files)) {continue;} if (in_array($file, $ignore_files)) {continue;}
$branch = [];
$realpath = realpath("{$dir}{$file}"); $realpath = realpath("{$dir}{$file}");
if (is_dir($realpath)) { if (is_dir($realpath)) {
$files = []; $files = [];
if ($nextLevels > 0) { if ($nextLevels > 0) {
$files = $this->scan($realpath, $nextLevels); $files = $this->scan($realpath, $nextLevels);
if (!empty($files)) {
foreach($files as $file) {
$this->scanResults[$file['id']] = $file;
}
}
} }
$tree[] = $this->getDirData($realpath, $files); $branch = $this->getDirData($realpath, $files);
}else { }else {
$tree[] = $this->getFileData($realpath, $this->scanningDirLevel); $pathinfo = pathinfo($realpath);
$extension = strtolower($pathinfo['extension']);
if ( in_array($extension, $this->supportFileExtensions) ) {
if ($extension != 'txt') {
$branch = $this->getFileData($realpath, $this->scanningDirLevel);
}else {
//把描述文件内容合并到被描述的目录或md文件数据中
$branch = $this->mergeDescriptionData($realpath);
}
}
}
if (!empty($branch)) {
$this->scanResults[$branch['id']] = $branch;
$tree[$branch['id']] = $branch;
} }
} }
} }
return $tree; return $tree;
} }
//获取扫描结果
public function getScanResults() {
return $this->scanResults;
}
} }

5
test/DirScannerTest.php

@ -69,3 +69,8 @@ $scanner = new DirScannerTest();
$dirTree = $scanner->scan(__DIR__ . '/', 3); $dirTree = $scanner->scan(__DIR__ . '/', 3);
print_r($dirTree); print_r($dirTree);
echo "\n"; echo "\n";
echo "\n";
$scanResults = $scanner->getScanResults();
print_r($scanResults);
echo "\n";

Loading…
Cancel
Save