Browse Source

add scan time cost

master
filesite 3 years ago
parent
commit
b7924d09ac
  1. 24
      lib/DirScanner.php
  2. 8
      test/DirScannerTest.php

24
lib/DirScanner.php

@ -31,6 +31,7 @@ Class DirScanner {
]; ];
private $rootDir; //当前扫描的根目录 private $rootDir; //当前扫描的根目录
private $scanningDirLevel = 0; //当前扫描的目录深度 private $scanningDirLevel = 0; //当前扫描的目录深度
private $scanStartTime = 0; //扫描开始时间,单位:秒
private $scanResults = []; //目录扫描结果 private $scanResults = []; //目录扫描结果
@ -68,7 +69,7 @@ Class DirScanner {
'm3u8', //视频 'm3u8', //视频
]; ];
public $scanTimeCost = 0; //上一次目录扫描耗时,单位: public $scanTimeCost = 0; //上一次目录扫描耗时,单位:
//解析描述文件内容 //解析描述文件内容
@ -130,7 +131,7 @@ Class DirScanner {
'id' => $id, 'id' => $id,
'directory' => basename($realpath), 'directory' => basename($realpath),
'realpath' => $realpath, 'realpath' => $realpath,
'path' => $this->getDirPath($realpath), 'path' => $this->getDirPath($id),
'files' => $files, 'files' => $files,
]; ];
@ -163,7 +164,7 @@ Class DirScanner {
'ctime' => $fstat['ctime'], 'ctime' => $fstat['ctime'],
], ],
'realpath' => $realpath, 'realpath' => $realpath,
'path' => $this->getFilePath( $this->getDirectoryName($pathinfo['dirname'], $dirLevel), $pathinfo['filename'], $extension ), 'path' => $this->getFilePath( $id, $this->getDirectoryName($pathinfo['dirname'], $dirLevel), $pathinfo['filename'], $extension ),
]; ];
if ($extension == 'url') { if ($extension == 'url') {
@ -262,7 +263,7 @@ Class DirScanner {
} }
//根据文件生成相对路径 //根据文件生成相对路径
protected function getFilePath($directory, $filename, $extension) { protected function getFilePath($id, $directory, $filename, $extension) {
if (empty($directory)) { if (empty($directory)) {
$directory = '/'; $directory = '/';
} }
@ -291,9 +292,9 @@ Class DirScanner {
if (!empty($path) && in_array($extension, ['md', 'url', 'm3u8'])) { if (!empty($path) && in_array($extension, ['md', 'url', 'm3u8'])) {
if ($this->nginxSecureOn && $extension == 'm3u8') { if ($this->nginxSecureOn && $extension == 'm3u8') {
$path = $this->getSecureLink($path); $path = $this->getSecureLink($path);
$path = "{$path}&file=" . urlencode($filename); $path = "{$path}&id={$id}";
}else { }else {
$path = "{$path}?file=" . urlencode($filename); $path = "{$path}?id={$id}";
} }
}else if (!empty($path) && $this->nginxSecureOn) { }else if (!empty($path) && $this->nginxSecureOn) {
$path = $this->getSecureLink($path); $path = $this->getSecureLink($path);
@ -303,8 +304,8 @@ Class DirScanner {
} }
//根据目录生成相对路径 //根据目录生成相对路径
protected function getDirPath($realpath) { protected function getDirPath($id) {
return '/list/?dir=' . urlencode(basename($realpath)); return "/list/?id={$id}";
} }
@ -408,6 +409,10 @@ Class DirScanner {
//扫描目录获取目录和文件列表,支持指定目录扫描深度(目录级数) //扫描目录获取目录和文件列表,支持指定目录扫描深度(目录级数)
public function scan($dir, $levels = 3) { public function scan($dir, $levels = 3) {
if (empty($this->scanStartTime)) {
$this->scanStartTime = microtime(true);
}
$tree = array(); $tree = array();
$ignore_files = array('.', '..'); $ignore_files = array('.', '..');
@ -457,6 +462,9 @@ Class DirScanner {
} }
} }
$time = microtime(true);
$this->scanTimeCost = $this->scanStartTime > 0 ? ceil( ($time - $this->scanStartTime)*1000 ) : 0;
return $tree; return $tree;
} }

8
test/DirScannerTest.php

@ -67,10 +67,12 @@ $scanner = new DirScannerTest();
//$dirTree = $scanner->scan('./'); //$dirTree = $scanner->scan('./');
//$dirTree = $scanner->scan(__DIR__); //$dirTree = $scanner->scan(__DIR__);
$dirTree = $scanner->scan(__DIR__ . '/', 3); $dirTree = $scanner->scan(__DIR__ . '/', 3);
echo "Time cost: {$scanner->scanTimeCost} ms\n";
echo "\n";
print_r($dirTree); print_r($dirTree);
echo "\n"; echo "\n";
echo "\n"; echo "\n";
$scanResults = $scanner->getScanResults(); //$scanResults = $scanner->getScanResults();
print_r($scanResults); //print_r($scanResults);
echo "\n"; //echo "\n";

Loading…
Cancel
Save