Browse Source

add file move to directory support

master
wen 2 years ago
parent
commit
9c1198058b
  1. 34
      controller/ApiController.php

34
controller/ApiController.php

@ -41,6 +41,15 @@ Class ApiController extends Controller {
return $this->renderJson($params); return $this->renderJson($params);
} }
private function getParentDir($realpath) {
$realpath = preg_replace('/\/$/', '', $realpath);
$arr = explode('/', $realpath);
if (count($arr) < 2) {return $realpath;}
array_pop($arr);
return implode('/', $arr);
}
//判断父目录是否合法 //判断父目录是否合法
protected function isParentDirectoryValid($parentDir) { protected function isParentDirectoryValid($parentDir) {
if (empty($parentDir) || strpos($parentDir, '../') !== false) { if (empty($parentDir) || strpos($parentDir, '../') !== false) {
@ -48,6 +57,7 @@ Class ApiController extends Controller {
} }
$valid = true; $valid = true;
$parentDir = preg_replace('/^\//', '', $parentDir);
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'] . "/{$parentDir}"; $target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'] . "/{$parentDir}";
if (!is_dir($target)) { if (!is_dir($target)) {
$valid = false; $valid = false;
@ -241,19 +251,35 @@ Class ApiController extends Controller {
} }
$fromDir = $this->post('from', ''); $fromDir = $this->post('from', '');
$fromParent = $this->getParentDir($fromDir);
$toDir = $this->post('to', ''); $toDir = $this->post('to', '');
$toParent = $this->getParentDir($toDir);
$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'), $this->httpStatus['notAllowed']); return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']);
}else if ($this->isParentDirectoryValid($fromDir) == false) { //目录合法性检查 }else if ($this->isParentDirectoryValid($fromParent) == false) { //父目录合法性检查
$err = "目录{$fromDir}不存在"; $err = "被移动目录{$fromParent}不存在";
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']); return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']);
}else if ($this->isParentDirectoryValid($toDir) == false) { //目录合法性检查 }else if ($this->isParentDirectoryValid($toParent) == false) { //父目录合法性检查
$err = "目录{$toDir}不存在"; $err = "目标目录{$toParent}不存在";
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']); return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']);
} }
//如果from是一个文件,而to是目录
if (preg_match('/\.\w+$/', $fromDir) && (
preg_match('/\/$/', $toDir) || preg_match('/\/[^\.]+$/', $toDir)
)
) {
if ($this->isParentDirectoryValid($toDir) == false) { //父目录合法性检查
$err = "目标目录{$toDir}不存在";
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']);
}
$fromFile = str_replace("{$fromParent}/", '', $fromDir);
$toDir = preg_match('/\/$/', $toDir) ? "{$toDir}{$fromFile}" : "{$toDir}/{$fromFile}";
}
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; $target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory'];
try { try {
$res = rename("{$target}/{$fromDir}", "{$target}/{$toDir}"); $res = rename("{$target}/{$fromDir}", "{$target}/{$toDir}");

Loading…
Cancel
Save