198 lines
8.0 KiB
PHP
198 lines
8.0 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use Illuminate\Http\JsonResponse;
|
||
use App\Http\Controllers\Controller;
|
||
use Illuminate\Http\Request;
|
||
use OSS\OssClient;
|
||
|
||
class UeditorUploadController extends Controller
|
||
{
|
||
private $stateMap = array( //上传状态映射表,国际化用户需考虑此处数据的国际化
|
||
"SUCCESS", //上传成功标记,在UEditor中内不可改变,否则flash判断会出错
|
||
"文件大小超出 upload_max_filesize 限制",
|
||
"文件大小超出 MAX_FILE_SIZE 限制",
|
||
"文件未被完整上传",
|
||
"没有文件被上传",
|
||
"上传文件为空",
|
||
"ERROR_TMP_FILE" => "临时文件错误",
|
||
"ERROR_TMP_FILE_NOT_FOUND" => "找不到临时文件",
|
||
"ERROR_SIZE_EXCEED" => "文件大小超出网站限制",
|
||
"ERROR_TYPE_NOT_ALLOWED" => "文件类型不允许",
|
||
"ERROR_CREATE_DIR" => "目录创建失败",
|
||
"ERROR_DIR_NOT_WRITEABLE" => "目录没有写权限",
|
||
"ERROR_FILE_MOVE" => "文件保存时出错",
|
||
"ERROR_FILE_NOT_FOUND" => "找不到上传文件",
|
||
"ERROR_WRITE_CONTENT" => "写入文件内容错误",
|
||
"ERROR_UNKNOWN" => "未知错误",
|
||
"ERROR_DEAD_LINK" => "链接不可用",
|
||
"ERROR_HTTP_LINK" => "链接不是http链接",
|
||
"ERROR_HTTP_CONTENTTYPE" => "链接contentType不正确",
|
||
"INVALID_URL" => "非法 URL",
|
||
"INVALID_IP" => "非法 IP"
|
||
);
|
||
|
||
public function upload(Request $request)
|
||
{
|
||
$action = $request->action;
|
||
$config = config('UEditorUpload.upload');
|
||
switch ($action) {
|
||
case 'config':
|
||
return Response()->json($config)
|
||
->header('Access-Control-Allow-Headers','*')
|
||
->setCallback(request()->input('callback'));
|
||
//->header("Content-Type", "text/html; charset=utf-8");
|
||
break;
|
||
case 'uploadimage':
|
||
$upload_config = array(
|
||
"pathFormat" => $config['imagePathFormat'],
|
||
"maxSize" => $config['imageMaxSize'],
|
||
"allowFiles" => $config['imageAllowFiles']
|
||
);
|
||
$field = $config['imageFieldName'];
|
||
$bucket = config('alioss.buckets.picture');
|
||
break;
|
||
case 'uploadscrawl':
|
||
$upload_config = array(
|
||
"pathFormat" => $config['scrawlPathFormat'],
|
||
"maxSize" => $config['scrawlMaxSize'],
|
||
"allowFiles" => $config['scrawlAllowFiles'],
|
||
"oriName" => "scrawl.png"
|
||
);
|
||
$field = $config['scrawlFieldName'];
|
||
$bucket = config('alioss.buckets.file');
|
||
break;
|
||
case 'uploadvideo':
|
||
$upload_config = array(
|
||
"pathFormat" => $config['videoPathFormat'],
|
||
"maxSize" => $config['videoMaxSize'],
|
||
"allowFiles" => $config['videoAllowFiles']
|
||
);
|
||
$field = $config['videoFieldName'];
|
||
$bucket = config('alioss.buckets.picture');
|
||
break;
|
||
case 'uploadfile':
|
||
default:
|
||
$upload_config = array(
|
||
"pathFormat" => $config['filePathFormat'],
|
||
"maxSize" => $config['fileMaxSize'],
|
||
"allowFiles" => $config['fileAllowFiles']
|
||
);
|
||
$field = $config['fileFieldName'];
|
||
$bucket = config('alioss.buckets.file');
|
||
break;
|
||
}
|
||
|
||
$file = $request->file($field);
|
||
$oriName = $file->getClientOriginalName();
|
||
$fileSize = $file->getClientSize();
|
||
$fileType = $file->getClientOriginalExtension();
|
||
$filePath = $file->getRealPath();
|
||
$fileName = $file->getFileName();
|
||
$result = [
|
||
'original'=> $oriName,
|
||
'size'=> $fileSize,
|
||
'state'=> "SUCCESS",
|
||
'title'=> $fileName,
|
||
'type'=> $fileType,
|
||
'url'=> $filePath,
|
||
];
|
||
|
||
if (!$file) {
|
||
$result['stateInfo'] = $this->getStateInfo("ERROR_FILE_NOT_FOUND");
|
||
return Response()->json($result)
|
||
->header('Access-Control-Allow-Headers','*');
|
||
//->header("Content-Type", "text/html; charset=utf-8");
|
||
}
|
||
if (!file_exists($filePath)) {
|
||
$result['stateInfo'] = $this->getStateInfo("ERROR_TMP_FILE_NOT_FOUND");
|
||
return Response()->json($result)
|
||
->header('Access-Control-Allow-Headers','*');
|
||
//->header("Content-Type", "text/html; charset=utf-8");
|
||
} else if (!in_array('.'.$file->getClientOriginalExtension(),$upload_config['allowFiles'])) {
|
||
$result['stateInfo'] = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
|
||
return Response()->json($result)
|
||
->header('Access-Control-Allow-Headers','*');
|
||
//->header("Content-Type", "text/html; charset=utf-8");
|
||
} else if($fileSize>$upload_config['maxSize']) {
|
||
$result['stateInfo'] = $this->getStateInfo("ERROR_SIZE_EXCEED");
|
||
return Response()->json($result)
|
||
->header('Access-Control-Allow-Headers','*');
|
||
//->header("Content-Type", "text/html; charset=utf-8");
|
||
}
|
||
|
||
$object = date('Y') . date('m') . "/" . date('d')."/".$this->GetRandStr(6).time().'.'.$fileType;
|
||
$upload_result = $this->uploadToAliOSS($filePath,$object,$bucket);
|
||
if($upload_result['status']) {
|
||
if(strpos($request->url(),'admin.ufutx.net') !== false) {
|
||
$result['url'] = $upload_result['path'];
|
||
} else {
|
||
$reg = '/(http):\/\/([^\/]+)/i';
|
||
preg_match($reg, $upload_result['path'],$res);
|
||
$patterns[0] = '/'.$res[2].'/';
|
||
$replacements[0] = 'image.fulllinkai.com';
|
||
$result['url'] = preg_replace($patterns, $replacements, $upload_result['path']);
|
||
}
|
||
$this->stateInfo = $this->stateMap[0];
|
||
} else {
|
||
$this->stateInfo = $this->getStateInfo("ERROR_UNKNOWN");
|
||
}
|
||
|
||
return Response()->json($result)
|
||
->header('Access-Control-Allow-Headers','*')
|
||
->header('Access-Control-Allow-Headers', 'X-Requested-With,X_Requested_With');
|
||
//->header("Content-Type", "text/html; charset=utf-8");
|
||
}
|
||
|
||
/**
|
||
* 上传错误检查
|
||
* @param $errCode
|
||
* @return string
|
||
*/
|
||
private function getStateInfo($errCode)
|
||
{
|
||
return !$this->stateMap[$errCode] ? $this->stateMap["ERROR_UNKNOWN"] : $this->stateMap[$errCode];
|
||
}
|
||
|
||
function uploadToAliOSS($file,$object,$bucket){
|
||
try {
|
||
$accessKeyId = config('alioss.id');//涉及到隐私就不放出来了
|
||
$accessKeySecret = config('alioss.secret');//涉及到隐私就不放出来了
|
||
$endpoint = config('alioss.host');//节点
|
||
|
||
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
|
||
$ossClient->setTimeout(3600 /* seconds */);
|
||
$ossClient->setConnectTimeout(10 /* seconds */);
|
||
// 先把本地的example.jpg上传到指定$bucket, 命名为$object
|
||
$ossClient->uploadFile($bucket, $object, $file);
|
||
$signedUrl = $ossClient->signUrl($bucket, $object);
|
||
$path = explode('?',$signedUrl)[0];
|
||
$obj['status'] = true;
|
||
$obj['path'] = $path;
|
||
} catch (OssException $e) {
|
||
$obj['status'] = false;
|
||
$obj['path'] = "";
|
||
print $e->getMessage();
|
||
}
|
||
return $obj;
|
||
}
|
||
|
||
function GetRandStr($length){
|
||
//字符组合
|
||
$str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||
$len = strlen($str)-1;
|
||
$randstr = '';
|
||
for ($i=0;$i<$length;$i++) {
|
||
$num=mt_rand(0,$len);
|
||
$randstr .= $str[$num];
|
||
}
|
||
return $randstr;
|
||
}
|
||
|
||
function index(Request $request)
|
||
{
|
||
return $this->success('ok');
|
||
}
|
||
}
|