ufutx.love.util/app/Http/Controllers/CommonController.php

158 lines
6.3 KiB
PHP
Raw Normal View History

2025-08-06 13:32:09 +08:00
<?php
namespace App\Http\Controllers;
use App\Facades\UploadService;
2025-08-06 14:13:58 +08:00
use App\Http\Response\ResponseJson;
2025-08-06 13:32:09 +08:00
use Illuminate\Http\Request;
2025-08-06 14:13:58 +08:00
use Illuminate\Support\Facades\Log;
2025-08-06 13:32:09 +08:00
class CommonController extends Controller
{
2025-08-06 14:13:58 +08:00
use ResponseJson;
2025-08-06 13:32:09 +08:00
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"
);
2025-08-06 14:13:58 +08:00
public function checkToken($token): bool
{
$token2 = date("Ym-d") . "$@.";
Log::info(base64_encode($token2), [$token]);
return base64_encode($token2) == $token;
}
2025-08-06 13:32:09 +08:00
public function upload(Request $request)
{
2025-08-06 14:13:58 +08:00
if (!$this->checkToken($request->input("token")))
return $this->failure("upload token失效");
2025-08-06 13:32:09 +08:00
$action = $request->action;
$config = config('UEditorUpload.upload');
switch ($action) {
case 'config':
2025-08-06 16:29:46 +08:00
// return Response()->json($config)
// ->header('Access-Control-Allow-Headers', '*')
// ->setCallback(request()->input('callback'));
return Response()->json($config);
2025-08-06 13:32:09 +08:00
//->header("Content-Type", "text/html; charset=utf-8");
case 'uploadimage':
$upload_config = array(
"pathFormat" => $config['imagePathFormat'],
"maxSize" => $config['imageMaxSize'],
"allowFiles" => $config['imageAllowFiles']
);
$field = $config['imageFieldName'];
break;
case 'uploadscrawl':
$upload_config = array(
"pathFormat" => $config['scrawlPathFormat'],
"maxSize" => $config['scrawlMaxSize'],
"allowFiles" => $config['scrawlAllowFiles'],
"oriName" => "scrawl.png"
);
$field = $config['scrawlFieldName'];
break;
case 'uploadvideo':
$upload_config = array(
"pathFormat" => $config['videoPathFormat'],
"maxSize" => $config['videoMaxSize'],
"allowFiles" => $config['videoAllowFiles']
);
$field = $config['videoFieldName'];
break;
case 'uploadfile':
default:
$upload_config = array(
"pathFormat" => $config['filePathFormat'],
"maxSize" => $config['fileMaxSize'],
"allowFiles" => $config['fileAllowFiles']
);
$field = $config['fileFieldName'];
break;
}
$file = $request->file($field);
$oriName = $file->getClientOriginalName();
$fileSize = $file->getSize();
$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");
}
$url = UploadService::uploadFile($file);
if ($url) {
$result['url'] = $url;
$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)
{
try {
return !$this->stateMap[$errCode] ? $this->stateMap["ERROR_UNKNOWN"] : $this->stateMap[$errCode];
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息中,请稍后再试');
}
}
}