Browse Source

DirScanner add api call support, improve dir/file management api

master
filesite 2 years ago
parent
commit
740b844cb4
  1. 81
      controller/ApiController.php
  2. 5
      lib/DirScanner.php

81
controller/ApiController.php

@ -38,6 +38,21 @@ Class ApiController extends Controller {
return $this->renderJson($params); return $this->renderJson($params);
} }
//判断父目录是否合法
protected function isParentDirectoryValid($parentDir) {
if (empty($parentDir) || strpos($parentDir, '../') !== false) {
return false;
}
$valid = true;
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'] . "/{$parentDir}";
if (!is_dir($target)) {
$valid = false;
}
return $valid;
}
//目录、文件列表 //目录、文件列表
public function actionLs() { public function actionLs() {
$code = 0; $code = 0;
@ -51,6 +66,7 @@ Class ApiController extends Controller {
$scanner = new DirScanner(); $scanner = new DirScanner();
$scanner->setWebRoot(FSC::$app['config']['content_directory']); $scanner->setWebRoot(FSC::$app['config']['content_directory']);
$scanner->isApi = true; //realpath返回相对路径
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; $target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'];
$maxLevels = FSC::$app['config']['maxScanDirLevels']; $maxLevels = FSC::$app['config']['maxScanDirLevels'];
$dirTree = $scanner->scan($target, $maxLevels); $dirTree = $scanner->scan($target, $maxLevels);
@ -81,6 +97,12 @@ Class ApiController extends Controller {
$cateId = $this->get('id', $menus[0]['id']); $cateId = $this->get('id', $menus[0]['id']);
$data['menus'] = $menus; $data['menus'] = $menus;
//替换realpath,改为相对路径返回
$basePath = realpath($target);
if (!preg_match('/\/$/', $basePath)) {
$basePath .= '/';
}
$data['dirTree'] = $scanResults[$cateId]; $data['dirTree'] = $scanResults[$cateId];
$code = 1; $code = 1;
@ -117,10 +139,12 @@ Class ApiController extends Controller {
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; $target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'];
if (!empty($parentDir)) { if (!empty($parentDir)) {
$target = "{$target}/{$parentDir}"; $target = "{$target}/{$parentDir}";
}
if (!is_dir($target)) { //父目录合法性检查
$err = "父目录{$parentDir}不存在"; if ($this->isParentDirectoryValid($parentDir) == false) {
return $this->renderJson(compact('code', 'msg', 'err', 'data')); $err = "父目录{$parentDir}不存在";
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']);
}
} }
try { try {
@ -164,10 +188,12 @@ Class ApiController extends Controller {
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; $target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'];
if (!empty($parentDir)) { if (!empty($parentDir)) {
$target = "{$target}/{$parentDir}"; $target = "{$target}/{$parentDir}";
}
if (!is_dir($target)) { //父目录合法性检查
$err = "父目录{$parentDir}不存在"; if ($this->isParentDirectoryValid($parentDir) == false) {
return $this->renderJson(compact('code', 'msg', 'err', 'data')); $err = "父目录{$parentDir}不存在";
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']);
}
} }
try { try {
@ -196,25 +222,21 @@ Class ApiController extends Controller {
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']); return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']);
} }
$parentDir = $this->post('parent', '');
$fromDir = $this->post('from', ''); $fromDir = $this->post('from', '');
$toDir = $this->post('to', ''); $toDir = $this->post('to', '');
$maxDirLen = 50; $maxDirLen = 50;
if (empty($fromDir) || mb_strlen($fromDir, 'utf-8') > $maxDirLen || empty($toDir) || mb_strlen($toDir, 'utf-8') > $maxDirLen) { if (empty($fromDir) || mb_strlen($fromDir, 'utf-8') > $maxDirLen || empty($toDir) || mb_strlen($toDir, 'utf-8') > $maxDirLen) {
$err = "目录名不能为空且最长 {$maxDirLen} 个字符"; $err = "目录名不能为空且最长 {$maxDirLen} 个字符";
return $this->renderJson(compact('code', 'msg', 'err', 'data')); return $this->renderJson(compact('code', 'msg', 'err', 'data'));
}else if ($this->isParentDirectoryValid($fromDir) == false) { //目录合法性检查
$err = "目录{$fromDir}不存在";
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']);
}else if ($this->isParentDirectoryValid($toDir) == false) { //目录合法性检查
$err = "目录{$toDir}不存在";
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']);
} }
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; $target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'];
if (!empty($parentDir)) {
$target = "{$target}/{$parentDir}";
}
if (!is_dir($target)) {
$err = "父目录{$parentDir}不存在";
return $this->renderJson(compact('code', 'msg', 'err', 'data'));
}
try { try {
$res = rename("{$target}/{$fromDir}", "{$target}/{$toDir}"); $res = rename("{$target}/{$fromDir}", "{$target}/{$toDir}");
if ($res) { if ($res) {
@ -257,10 +279,12 @@ Class ApiController extends Controller {
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; $target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'];
if (!empty($parentDir)) { if (!empty($parentDir)) {
$target = "{$target}/{$parentDir}"; $target = "{$target}/{$parentDir}";
}
if (!is_dir($target)) { //父目录合法性检查
$err = "父目录{$parentDir}不存在"; if ($this->isParentDirectoryValid($parentDir) == false) {
return $this->renderJson(compact('code', 'msg', 'err', 'data')); $err = "父目录{$parentDir}不存在";
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']);
}
} }
try { try {
@ -304,10 +328,12 @@ Class ApiController extends Controller {
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; $target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'];
if (!empty($parentDir)) { if (!empty($parentDir)) {
$target = "{$target}/{$parentDir}"; $target = "{$target}/{$parentDir}";
}
if (!is_dir($target)) { //父目录合法性检查
$err = "父目录{$parentDir}不存在"; if ($this->isParentDirectoryValid($parentDir) == false) {
return $this->renderJson(compact('code', 'msg', 'err', 'data')); $err = "父目录{$parentDir}不存在";
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']);
}
} }
try { try {
@ -521,6 +547,9 @@ Class ApiController extends Controller {
}else if (!preg_match('/^data:[a-z0-9]+\/[a-z0-9]+;base64,/i', $upfile)) { }else if (!preg_match('/^data:[a-z0-9]+\/[a-z0-9]+;base64,/i', $upfile)) {
$err = '图片数据必需为base64格式!'; $err = '图片数据必需为base64格式!';
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']); return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']);
}else if (!empty($parentDir) && $this->isParentDirectoryValid($parentDir) == false) { //父目录合法性检查
$err = "父目录{$parentDir}不存在";
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']);
} }
//base64格式数据支持 //base64格式数据支持

5
lib/DirScanner.php

@ -75,6 +75,7 @@ Class DirScanner {
); );
public $scanTimeCost = 0; //上一次目录扫描耗时,单位:毫秒 public $scanTimeCost = 0; //上一次目录扫描耗时,单位:毫秒
public $isApi = false; //如果为API获取数据,则realpath只返回相对路径
//判断目录名或文件名是否合法 //判断目录名或文件名是否合法
@ -173,7 +174,7 @@ Class DirScanner {
$data = array( $data = array(
'id' => $id, 'id' => $id,
'directory' => $this->basename($realpath), 'directory' => $this->basename($realpath),
'realpath' => $realpath, 'realpath' => $this->isApi ? $this->getRelativeDirname($realpath) : $realpath,
'path' => $this->getDirPath($id), 'path' => $this->getDirPath($id),
); );
@ -223,7 +224,7 @@ Class DirScanner {
'mtime' => $fstat['mtime'], 'mtime' => $fstat['mtime'],
'ctime' => $fstat['ctime'], 'ctime' => $fstat['ctime'],
), ),
'realpath' => $realpath, 'realpath' => $this->isApi ? $this->getRelativeDirname($realpath) : $realpath,
'path' => $this->getFilePath( $id, $this->getRelativeDirname($pathinfo['dirname']), $pathinfo['filename'], $extension, $fstat['mtime'] ), 'path' => $this->getFilePath( $id, $this->getRelativeDirname($pathinfo['dirname']), $pathinfo['filename'], $extension, $fstat['mtime'] ),
); );

Loading…
Cancel
Save