Browse Source

improve relative path and file id

master
filesite 3 years ago
parent
commit
243156f1d3
  1. 36
      lib/DirScanner.php
  2. 1
      test/DirScannerTest.php

36
lib/DirScanner.php

@ -30,6 +30,7 @@ Class DirScanner { @@ -30,6 +30,7 @@ Class DirScanner {
'snapshot' => '快照图片',
];
private $rootDir; //当前扫描的根目录
private $webRoot = '/content/'; //网站静态文件相对路径的根目录
private $scanningDirLevel = 0; //当前扫描的目录深度
private $scanStartTime = 0; //扫描开始时间,单位:秒
private $scanResults = []; //目录扫描结果
@ -101,7 +102,11 @@ Class DirScanner { @@ -101,7 +102,11 @@ Class DirScanner {
}
//根据文件路径生成唯一编号
//使用相对路径计算md5值
private function getId($realpath) {
if (!empty($this->rootDir)) {
$realpath = str_replace($this->rootDir, '', $realpath);
}
return !empty($realpath) ? md5($realpath) : '';
}
@ -276,17 +281,18 @@ Class DirScanner { @@ -276,17 +281,18 @@ Class DirScanner {
$directory = "/{$directory}";
}
$webRoot = preg_replace('/\/$/', '', $this->webRoot);
$extensionPathMap = [ //默认每种文件读取内容最大大小
'txt' => '',
'md' => '/view/',
'url' => '/link/',
'm3u8' => '/m3u8/',
'jpg' => "{$directory}{$filename}.{$extension}",
'png' => "{$directory}{$filename}.{$extension}",
'gif' => "{$directory}{$filename}.{$extension}",
'ico' => "{$directory}{$filename}.{$extension}",
'mp4' => "{$directory}{$filename}.{$extension}",
'ts' => "{$directory}{$filename}.{$extension}",
'jpg' => "{$webRoot}{$directory}{$filename}.{$extension}",
'png' => "{$webRoot}{$directory}{$filename}.{$extension}",
'gif' => "{$webRoot}{$directory}{$filename}.{$extension}",
'ico' => "{$webRoot}{$directory}{$filename}.{$extension}",
'mp4' => "{$webRoot}{$directory}{$filename}.{$extension}",
'ts' => "{$webRoot}{$directory}{$filename}.{$extension}",
];
$path = isset($extensionPathMap[$extension]) ? $extensionPathMap[$extension] : '';
@ -409,6 +415,24 @@ Class DirScanner { @@ -409,6 +415,24 @@ Class DirScanner {
return $this->nginxSecureTimeout;
}
//设置网站静态文件相对根目录
public function setWebRoot($webRoot) {
if (!empty($webRoot) && !preg_match('/^\//', $webRoot)) {
$webRoot = "/{$webRoot}";
}
if (!empty($webRoot) && !preg_match('/\/$/', $webRoot)) {
$webRoot = "{$webRoot}/";
}
$this->webRoot = $webRoot;
}
//获取网站静态文件相对根目录
public function getWebRoot() {
return $this->webRoot;
}
//获取是否开启防盗链
public function isSecureOn() {
return $this->nginxSecureOn;

1
test/DirScannerTest.php

@ -66,6 +66,7 @@ $scanner = new DirScannerTest(); @@ -66,6 +66,7 @@ $scanner = new DirScannerTest();
//$dirTree = $scanner->scan('./');
//$dirTree = $scanner->scan(__DIR__);
$scanner->setWebRoot('/content/');
$dirTree = $scanner->scan(__DIR__ . '/content/', 4);
echo "Time cost: {$scanner->scanTimeCost} ms\n";
echo "\n";

Loading…
Cancel
Save