From d3bf5e04c5e1558b640dd4c0498209aa6f42c50f Mon Sep 17 00:00:00 2001 From: filesite Date: Thu, 17 Mar 2022 12:20:07 +0800 Subject: [PATCH] function scan update --- lib/DirScanner.php | 91 +++++++++++++++++++++++++++++++++++++---- test/DirScannerTest.php | 5 +++ 2 files changed, 89 insertions(+), 7 deletions(-) diff --git a/lib/DirScanner.php b/lib/DirScanner.php index d2b3d21..7513565 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -73,8 +73,8 @@ Class DirScanner { //解析描述文件内容 private function parseDescriptionFiles($realpath) { - $field = preg_replace('/^.+_([a-z0-9]+)\.txt$/i', "$1", $realpath); - if ($field == $realpath) {return false;} + $pathinfo = pathinfo($realpath); + $field = preg_replace('/^.+_/', '', $pathinfo['filename']); $content = file_get_contents($realpath); $data = []; $data[$field] = $content; @@ -126,13 +126,22 @@ Class DirScanner { //根据路径生成目录数组 private function getDirData($realpath, $files) { $id = $this->getId($realpath); - return [ + $data = [ 'id' => $id, 'directory' => basename($realpath), 'realpath' => $realpath, 'path' => $this->getDirPath($realpath), '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文件 @@ -143,7 +152,7 @@ Class DirScanner { fclose($fp); $pathinfo = pathinfo($realpath); $extension = strtolower($pathinfo['extension']); - return [ + $data = [ 'id' => $id, 'filename' => $pathinfo['filename'], 'extension' => $extension, @@ -156,6 +165,12 @@ Class DirScanner { 'realpath' => $realpath, '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)); } + //合并描述文件内容到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 @@ -370,22 +423,46 @@ Class DirScanner { foreach($files as $file) { if (in_array($file, $ignore_files)) {continue;} + $branch = []; $realpath = realpath("{$dir}{$file}"); if (is_dir($realpath)) { $files = []; if ($nextLevels > 0) { $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 { - $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; } + //获取扫描结果 + public function getScanResults() { + return $this->scanResults; + } + } diff --git a/test/DirScannerTest.php b/test/DirScannerTest.php index 706be53..31c5120 100644 --- a/test/DirScannerTest.php +++ b/test/DirScannerTest.php @@ -69,3 +69,8 @@ $scanner = new DirScannerTest(); $dirTree = $scanner->scan(__DIR__ . '/', 3); print_r($dirTree); echo "\n"; +echo "\n"; + +$scanResults = $scanner->getScanResults(); +print_r($scanResults); +echo "\n";