feat 增加后缀鉴别
This commit is contained in:
@@ -86,16 +86,23 @@ class File extends AdminBase
|
||||
$sub_dir = $this->request->get('sub_dir','');
|
||||
$local = $local == 0 ? false : true;
|
||||
$save_path = 'uploads/other';
|
||||
$extensions = [];
|
||||
|
||||
if ($local && $local !== '') {
|
||||
if ($local && $sub_dir !== '') {
|
||||
if (!preg_match('/^[A-Za-z0-9_-]+$/', $sub_dir)) {
|
||||
return $this->_error('上传目录不合法');
|
||||
}
|
||||
$save_path = 'uploads/other/'.$sub_dir;
|
||||
if ($sub_dir === 'cert') {
|
||||
$extensions = ['pem'];
|
||||
}
|
||||
}
|
||||
|
||||
$result = FileServer::other($save_path, $local);
|
||||
$result = FileServer::other($save_path, $local, $extensions);
|
||||
$this->successOrError($result);
|
||||
}
|
||||
$this->_success();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace app\admin\controller;
|
||||
|
||||
|
||||
use app\admin\logic\LiveRoomLogic;
|
||||
use app\common\validate\Upload as UploadValidate;
|
||||
|
||||
class LiveRoom extends AdminBase
|
||||
{
|
||||
@@ -96,7 +97,24 @@ class LiveRoom extends AdminBase
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$file = request()->file('file');
|
||||
if (empty($file)) {
|
||||
return $this->_error('未找到上传文件的信息');
|
||||
}
|
||||
|
||||
$validate = (new UploadValidate())->setAllowedExtensions(config('project.file_image'));
|
||||
if (!$validate->check(['file' => $file])) {
|
||||
return $this->_error($validate->getError());
|
||||
}
|
||||
|
||||
$fileInfo = $file->getInfo();
|
||||
if (!check_is_image($fileInfo['tmp_name'] ?? '')) {
|
||||
return $this->_error('不是有效的图像文件');
|
||||
}
|
||||
|
||||
$info = $file->move( './uploads/temp');
|
||||
if (empty($info)) {
|
||||
return $this->_error($file->getError());
|
||||
}
|
||||
$result = LiveRoomLogic::upload($info->getSaveName());
|
||||
if (is_string($result)) {
|
||||
return $this->_error($result);
|
||||
@@ -107,4 +125,4 @@ class LiveRoom extends AdminBase
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,15 @@
|
||||
#logo{
|
||||
width: 176px;height: 42px
|
||||
}
|
||||
.captcha-box input {
|
||||
width: 178px !important;
|
||||
}
|
||||
.captcha-img {
|
||||
width: 100px;
|
||||
height: 38px;
|
||||
margin-left: 12px;
|
||||
cursor: pointer;
|
||||
}
|
||||
</style>
|
||||
<body>
|
||||
<div class="login">
|
||||
@@ -49,6 +58,18 @@
|
||||
placeholder="请输入密码"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-box-item captcha-box">
|
||||
<div class="icon">
|
||||
<img src="__PUBLIC__/static/common/image/login/login_code.png"/>
|
||||
</div>
|
||||
<div style="display: flex;align-items: center;">
|
||||
<input type="text" name="captcha" lay-verify="required" lay-vertype="tips"
|
||||
class="layui-input" style="border:none;padding-left: 20px;"
|
||||
placeholder="请输入图形验证码"/>
|
||||
<img id="captcha-img" class="captcha-img" src="" title="点击刷新验证码"/>
|
||||
<input type="hidden" name="captcha_key" value=""/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-box-checked">
|
||||
<div>
|
||||
@@ -80,6 +101,19 @@
|
||||
, form = layui.form
|
||||
form.render();
|
||||
|
||||
function getCaptcha() {
|
||||
$.get('{:url("account/captcha")}', function (res) {
|
||||
if (res.code == 1) {
|
||||
$('#captcha-img').attr('src', res.data.image || '');
|
||||
$('[name="captcha_key"]').val(res.data.key || '');
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$('#captcha-img').on('click', function () {
|
||||
getCaptcha();
|
||||
});
|
||||
getCaptcha();
|
||||
|
||||
function login(obj) {
|
||||
like.ajax({
|
||||
@@ -95,8 +129,10 @@
|
||||
}, function () {
|
||||
location.href = '../'; //后台主页
|
||||
});
|
||||
} else {
|
||||
getCaptcha();
|
||||
$('[name="captcha"]').val('');
|
||||
}
|
||||
$('#captcha').attr('src', '{:captcha_src()}?t=' + new Date().getTime());
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -118,11 +154,11 @@
|
||||
$('#login').trigger('click');
|
||||
}
|
||||
});
|
||||
$('[name="code"]').keyup(function (event) {
|
||||
$('[name="captcha"]').keyup(function (event) {
|
||||
if (event.keyCode == 13) {
|
||||
$('#login').trigger('click');
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</html>
|
||||
</html>
|
||||
|
||||
@@ -49,7 +49,7 @@ class FileServer extends ServerBase
|
||||
}
|
||||
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
$StorageDriver->setUploadFile('file', config('project.file_image'));
|
||||
|
||||
// 验证是否是图片文件
|
||||
if (! check_is_image($StorageDriver->getFileInfo()['tmp_name'] ?? '')) {
|
||||
@@ -110,7 +110,7 @@ class FileServer extends ServerBase
|
||||
|
||||
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
$StorageDriver->setUploadFile('file', config('project.file_image'));
|
||||
$fileName = $StorageDriver->getFileName();
|
||||
$fileInfo = $StorageDriver->getFileInfo();
|
||||
|
||||
@@ -172,7 +172,7 @@ class FileServer extends ServerBase
|
||||
}
|
||||
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
$StorageDriver->setUploadFile('file', config('project.file_video'));
|
||||
|
||||
$fileName = $StorageDriver->getFileName();
|
||||
$fileInfo = $StorageDriver->getFileInfo();
|
||||
@@ -217,7 +217,7 @@ class FileServer extends ServerBase
|
||||
* @return array
|
||||
* @author 张无忌(2021/2/20 9:53)
|
||||
*/
|
||||
public static function other($save_dir='uploads/other', $isLocal=false )
|
||||
public static function other($save_dir='uploads/other', $isLocal=false, array $extensions = [] )
|
||||
{
|
||||
try {
|
||||
if ($isLocal == false) {
|
||||
@@ -236,7 +236,7 @@ class FileServer extends ServerBase
|
||||
}
|
||||
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
$StorageDriver->setUploadFile('file', $extensions);
|
||||
|
||||
if (!$StorageDriver->upload($save_dir)) {
|
||||
throw new Exception('上传失败' . $StorageDriver->getError());
|
||||
@@ -366,7 +366,7 @@ class FileServer extends ServerBase
|
||||
}
|
||||
|
||||
$StorageDriver = new StorageDriver($config);
|
||||
$StorageDriver->setUploadFile('file');
|
||||
$StorageDriver->setUploadFile('file', config('project.file_video'));
|
||||
|
||||
|
||||
// 视频上传路径
|
||||
@@ -434,4 +434,4 @@ class FileServer extends ServerBase
|
||||
}
|
||||
return $temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,9 +33,9 @@ class Driver
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function setUploadFile($name = 'iFile')
|
||||
public function setUploadFile($name = 'iFile', array $extensions = [])
|
||||
{
|
||||
return $this->engine->setUploadFile($name);
|
||||
return $this->engine->setUploadFile($name, $extensions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,7 +34,7 @@ abstract class Server
|
||||
* @param string $name
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setUploadFile($name)
|
||||
public function setUploadFile($name, array $extensions = [])
|
||||
{
|
||||
// 接收上传的文件
|
||||
$this->file = request()->file($name);
|
||||
@@ -42,8 +42,8 @@ abstract class Server
|
||||
throw new Exception('未找到上传文件的信息');
|
||||
}
|
||||
// 校验文件
|
||||
$validate = (new Upload());
|
||||
if (!$validate->check(['file' => request()->file($name)])){
|
||||
$validate = (new Upload())->setAllowedExtensions($extensions);
|
||||
if (!$validate->check(['file' => $this->file])){
|
||||
throw new Exception($validate->getError());
|
||||
}
|
||||
// 文件信息
|
||||
|
||||
@@ -31,4 +31,18 @@ class Upload extends Validate
|
||||
'file.fileExt' => '该文件类型不允许上传',
|
||||
];
|
||||
|
||||
}
|
||||
/**
|
||||
* 按上传场景收窄允许的文件后缀。
|
||||
* @param array $extensions
|
||||
* @return $this
|
||||
*/
|
||||
public function setAllowedExtensions(array $extensions)
|
||||
{
|
||||
$extensions = array_filter(array_unique(array_map('strtolower', $extensions)));
|
||||
if (!empty($extensions)) {
|
||||
$this->rule['file'] = 'fileExt:' . implode(',', $extensions);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user