From 7402ad14c97dc0241d4b0de156e5e45c1781c64a Mon Sep 17 00:00:00 2001 From: filesite Date: Thu, 31 Mar 2022 08:26:17 +0800 Subject: [PATCH] improve snapshot's path --- lib/DirScanner.php | 27 +++++++++++++++++++++------ test/DirScannerTest.php | 2 +- test/init_test_files.sh | 4 ++-- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/DirScanner.php b/lib/DirScanner.php index d5b60df..fa20ea6 100644 --- a/lib/DirScanner.php +++ b/lib/DirScanner.php @@ -75,11 +75,26 @@ Class DirScanner { //解析描述文件内容 - private function parseDescriptionFiles($realpath) { + //snapshot相对路径完善,支持secure_link + private function parseDescriptionFiles($realpath, $dirLevel) { $pathinfo = pathinfo($realpath); $field = preg_replace('/^.+_/', '', $pathinfo['filename']); $content = file_get_contents($realpath); $data = []; + if (in_array($field, ['title', 'snapshot'])) { + $content = preg_replace('/\s/', '', $content); + if ($field == 'snapshot') { + $img_realpath = str_replace("{$pathinfo['filename']}.{$pathinfo['extension']}", $content, $realpath); + echo "img_realpath: ".$img_realpath."\n"; + $id = $this->getId($img_realpath); + $fp = fopen($img_realpath, 'r'); + $fstat = fstat($fp); + fclose($fp); + $img_pathinfo = pathinfo($img_realpath); + $extension = strtolower($img_pathinfo['extension']); + $content = $this->getFilePath( $id, $this->getDirectoryName($img_pathinfo['dirname'], $dirLevel), $img_pathinfo['filename'], $extension, $fstat['mtime'] ); + } + } $data[$field] = $content; return $data; @@ -206,9 +221,9 @@ Class DirScanner { } //合并描述文件内容到md文件或者目录数据 - private function mergeDescriptionData($realpath) { + private function mergeDescriptionData($realpath, $dirLevel) { $data = []; - $ext = $this->parseDescriptionFiles($realpath); + $ext = $this->parseDescriptionFiles($realpath, $dirLevel); //try to find the md file $targetFile = preg_replace('/_?[a-z0-9]+\.txt$/iU', '.md', $realpath); @@ -481,7 +496,7 @@ Class DirScanner { $branch = $this->getFileData($realpath, $this->scanningDirLevel); }else { //把描述文件内容合并到被描述的目录或md文件数据中 - $branch = $this->mergeDescriptionData($realpath); + $branch = $this->mergeDescriptionData($realpath, $this->scanningDirLevel); } } } @@ -521,10 +536,10 @@ Class DirScanner { 'path' => $item['path'], ]; if (!empty($item['snapshot'])) { - $dir['snapshot'] = preg_replace('/\s/', '', $item['snapshot']); + $dir['snapshot'] = $item['snapshot']; } if (!empty($item['title'])) { - $dir['title'] = preg_replace('/\s/', '', $item['title']); + $dir['title'] = $item['title']; } if (!empty($item['description'])) { $dir['description'] = $item['description']; diff --git a/test/DirScannerTest.php b/test/DirScannerTest.php index 0d76220..69d535a 100644 --- a/test/DirScannerTest.php +++ b/test/DirScannerTest.php @@ -71,7 +71,7 @@ $dirTree = $scanner->scan(__DIR__ . '/content/', 4); echo "Time cost: {$scanner->scanTimeCost} ms\n"; echo "\n"; -$readme_id = '34528ca632426cd2cd570dd32c9dcf4c'; +$readme_id = '6a8a2df95732bd046e20269707a70171'; $readme_titles = $scanner->getMDTitles($readme_id); echo "Titles of MD file {$readme_id}:\n"; print_r($readme_titles); diff --git a/test/init_test_files.sh b/test/init_test_files.sh index 900cee6..614d97f 100755 --- a/test/init_test_files.sh +++ b/test/init_test_files.sh @@ -54,7 +54,7 @@ tee -a "小说/金庸小说/最爱金庸.url" < "小说/金庸小说/snapshot.txt" +echo '最爱金庸网站图标.ico' > "小说/金庸小说/snapshot.txt" touch "小说/古龙小说/最爱古龙网站图标.ico" touch "小说/古龙小说/最爱古龙.url" @@ -74,7 +74,7 @@ tee -a "图片/图片搜索/description.txt" < "图片/图片搜索/snapshot.txt" +echo '谷歌图片搜索图标.ico' > "图片/图片搜索/snapshot.txt" touch "图片/必应图片搜索/bing图标.ico" touch "图片/必应图片搜索/bing.url"