|
|
@ -52,7 +52,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']); |
|
|
|
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; |
|
|
|
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; |
|
|
|
$maxLevels = 2; |
|
|
|
$maxLevels = FSC::$app['config']['maxScanDirLevels']; |
|
|
|
$dirTree = $scanner->scan($target, $maxLevels); |
|
|
|
$dirTree = $scanner->scan($target, $maxLevels); |
|
|
|
$scanResults = $scanner->getScanResults(); |
|
|
|
$scanResults = $scanner->getScanResults(); |
|
|
|
|
|
|
|
|
|
|
@ -126,12 +126,6 @@ Class ApiController extends Controller { |
|
|
|
try { |
|
|
|
try { |
|
|
|
$res = mkdir("{$target}/{$newDir}"); |
|
|
|
$res = mkdir("{$target}/{$newDir}"); |
|
|
|
if ($res) { |
|
|
|
if ($res) { |
|
|
|
//读取父目录数据,并返回包含新建的子目录在内的新数据 |
|
|
|
|
|
|
|
$scanner = new DirScanner(); |
|
|
|
|
|
|
|
$scanner->setWebRoot(FSC::$app['config']['content_directory']); |
|
|
|
|
|
|
|
$maxLevels = 2; |
|
|
|
|
|
|
|
$data = $scanner->scan($target, $maxLevels); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$code = 1; |
|
|
|
$code = 1; |
|
|
|
$msg = '目录创建完成'; |
|
|
|
$msg = '目录创建完成'; |
|
|
|
}else { |
|
|
|
}else { |
|
|
@ -455,4 +449,112 @@ Class ApiController extends Controller { |
|
|
|
return $logined; |
|
|
|
return $logined; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//保存base64格式的文件 |
|
|
|
|
|
|
|
protected function saveBase64File($base64FileContent, $filePath) { |
|
|
|
|
|
|
|
$saved = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
$base64 = preg_replace('/^data:[a-z0-9]+\/[a-z0-9]+;base64,/i', '', $base64FileContent); |
|
|
|
|
|
|
|
$base64 = str_replace(' ', '+', $base64); |
|
|
|
|
|
|
|
$fileContent = base64_decode($base64); |
|
|
|
|
|
|
|
file_put_contents($filePath, $fileContent); |
|
|
|
|
|
|
|
}catch(Exception $e) { |
|
|
|
|
|
|
|
$saved = false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $saved; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//在指定目录里创建一个新文件 |
|
|
|
|
|
|
|
//@fileType 示例:image/jpeg, video/mp4 |
|
|
|
|
|
|
|
protected function createNewFile($parentDir, $filename) { |
|
|
|
|
|
|
|
$target = __DIR__ . '/../www/' . FSC::$app['config']['content_directory']; |
|
|
|
|
|
|
|
return !empty($parentDir) ? "{$target}{$parentDir}/{$filename}" : "{$target}{$filename}"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//从文件名中解析文件后缀 |
|
|
|
|
|
|
|
protected function getSuffixFromFilename($filename) { |
|
|
|
|
|
|
|
$arr = explode('.', $filename); |
|
|
|
|
|
|
|
if (count($arr) < 2) { |
|
|
|
|
|
|
|
return ''; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return strtolower(array_pop($arr)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//从文件类型中解析文件后缀 |
|
|
|
|
|
|
|
protected function getSuffixFromFileType($fileType) { |
|
|
|
|
|
|
|
$arr = explode('/', $fileType); |
|
|
|
|
|
|
|
if (count($arr) < 2) { |
|
|
|
|
|
|
|
return ''; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$suffix = array_pop($arr); |
|
|
|
|
|
|
|
if (in_array($suffix, ['jpg', 'jpeg'])) { |
|
|
|
|
|
|
|
$suffix = 'jpg'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return strtolower($suffix); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//base64格式文件上传 |
|
|
|
|
|
|
|
//@parent - 可选:文件保存目录,默认保存到根目录 |
|
|
|
|
|
|
|
//@file - 单个文件base64内容 |
|
|
|
|
|
|
|
//@name - 单个文件文件名 |
|
|
|
|
|
|
|
public function actionUploadBase64() { |
|
|
|
|
|
|
|
$code = 0; |
|
|
|
|
|
|
|
$msg = $err = ''; |
|
|
|
|
|
|
|
$data = array(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->isUserLogined() == false) { |
|
|
|
|
|
|
|
$err = '没登陆或登陆已过期!'; |
|
|
|
|
|
|
|
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//参数检查 |
|
|
|
|
|
|
|
$parentDir = $this->post('parent', ''); |
|
|
|
|
|
|
|
$upfile = $this->post('file', ''); |
|
|
|
|
|
|
|
$filename = $this->post('name', ''); |
|
|
|
|
|
|
|
if (empty($upfile) || empty($filename)) { |
|
|
|
|
|
|
|
$err = '所有参数都不能为空!'; |
|
|
|
|
|
|
|
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']); |
|
|
|
|
|
|
|
}else if (!preg_match('/^data:[a-z0-9]+\/[a-z0-9]+;base64,/i', $upfile)) { |
|
|
|
|
|
|
|
$err = '图片数据必需为base64格式!'; |
|
|
|
|
|
|
|
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notLogined']); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//base64格式数据支持 |
|
|
|
|
|
|
|
$configs = FSC::$app['config']['admin']; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
preg_match('/^data:([a-z0-9]+\/[a-z0-9]+);base64,/i', $upfile, $matches); |
|
|
|
|
|
|
|
if (!empty($matches[1])) { |
|
|
|
|
|
|
|
$fileType = strtolower($matches[1]); |
|
|
|
|
|
|
|
if (strpos($filename, '.') === false) {$filename .= "." . $this->getSuffixFromFileType($fileType);} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!in_array($fileType, $configs['allowedUploadFileTypes'])) { |
|
|
|
|
|
|
|
$err = "不支持的文件格式:{$fileType}"; |
|
|
|
|
|
|
|
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']); |
|
|
|
|
|
|
|
}else if ($this->getSuffixFromFilename($filename) != $this->getSuffixFromFileType($fileType)) { |
|
|
|
|
|
|
|
$err = "文件格式和文件名后缀不匹配,请检查文件名后缀"; |
|
|
|
|
|
|
|
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$filePath = $this->createNewFile($parentDir, $filename); |
|
|
|
|
|
|
|
if ($this->saveBase64File($upfile, $filePath)) { |
|
|
|
|
|
|
|
$code = 1; |
|
|
|
|
|
|
|
$msg = '上传完成'; |
|
|
|
|
|
|
|
}else { |
|
|
|
|
|
|
|
$err = '上传失败,请检查数据目录权限配置!'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}else { |
|
|
|
|
|
|
|
$err = "文件数据不是base64格式"; |
|
|
|
|
|
|
|
return $this->renderJson(compact('code', 'msg', 'err', 'data'), $this->httpStatus['notAllowed']); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}catch(Exception $e) { |
|
|
|
|
|
|
|
$err = $e->getMessage(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $this->renderJson(compact('code', 'msg', 'err', 'data')); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|