Skip to content

Commit

Permalink
add: 富文本编辑器支持视频、文件上传 #16
Browse files Browse the repository at this point in the history
  • Loading branch information
eddy8 committed Jan 4, 2021
1 parent 68835c1 commit 74e0f6e
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 4 deletions.
113 changes: 110 additions & 3 deletions app/Http/Controllers/Admin/NEditorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,124 @@ public function catchImage(Request $request)
];
}

public function uploadVideo(Request $request)
{
if (config('light.image_upload.driver') !== 'local') {
$class = config('light.image_upload.class');
return call_user_func([new $class, 'uploadImage'], $request);
}

if (!$request->hasFile('file')) {
return [
'code' => 2,
'msg' => '非法请求'
];
}
$file = $request->file('file');
if (!$this->isValidVideo($file)) {
return [
'code' => 3,
'msg' => '文件不合要求'
];
}

$result = $file->store('video/' . date('Ym'), config('light.neditor.disk'));
if (!$result) {
return [
'code' => 3,
'msg' => '上传失败'
];
}

return [
'code' => 200,
'state' => 'SUCCESS', // 兼容ueditor
'msg' => '',
'url' => Storage::disk(config('light.neditor.disk'))->url($result),
];
}

public function uploadFile(Request $request)
{
if (config('light.image_upload.driver') !== 'local') {
$class = config('light.image_upload.class');
return call_user_func([new $class, 'uploadImage'], $request);
}

if (!$request->hasFile('file')) {
return [
'code' => 2,
'msg' => '非法请求'
];
}
$file = $request->file('file');
if (!$this->isValidFile($file)) {
return [
'code' => 3,
'msg' => '文件不合要求'
];
}

$result = $file->store('file/' . date('Ym'), config('light.neditor.disk'));
if (!$result) {
return [
'code' => 3,
'msg' => '上传失败'
];
}

return [
'code' => 200,
'state' => 'SUCCESS', // 兼容ueditor
'msg' => '',
'url' => Storage::disk(config('light.neditor.disk'))->url($result),
];
}

protected function isValidImage(UploadedFile $file)
{
$c = config('light.neditor.upload');
$config = [
'maxSize' => $c['imageMaxSize'],
'AllowFiles' => $c['imageAllowFiles'],
];

return $this->isValidUploadedFile($file, $config);
}

protected function isValidVideo(UploadedFile $file)
{
$c = config('light.neditor.upload');
$config = [
'maxSize' => $c['videoMaxSize'],
'AllowFiles' => $c['videoAllowFiles'],
];

return $this->isValidUploadedFile($file, $config);
}

protected function isValidFile(UploadedFile $file)
{
$c = config('light.neditor.upload');
$config = [
'maxSize' => $c['fileMaxSize'],
'AllowFiles' => $c['fileAllowFiles'],
];

return $this->isValidUploadedFile($file, $config);
}

protected function isValidUploadedFile(UploadedFile $file, array $config)
{
if (!$file->isValid() ||
$file->getSize() > config('light.neditor.upload.imageMaxSize') ||
$file->getSize() > $config['maxSize'] ||
!in_array(
'.' . strtolower($file->getClientOriginalExtension()),
config('light.neditor.upload.imageAllowFiles')
$config['AllowFiles']
) ||
!in_array(
'.' . strtolower($file->guessExtension()),
config('light.neditor.upload.imageAllowFiles')
$config['AllowFiles']
)
) {
return false;
Expand Down
15 changes: 14 additions & 1 deletion config/light.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
'config' => 'config'
],
// 加载数据库自定义配置
'light_config' => false,
'light_config' => true,

// 系统日志保留时间。单位:天
'log_reserve_days' => 180,
Expand Down Expand Up @@ -92,6 +92,19 @@
'upload' => [
'imageMaxSize' => 8 * 1024 * 1024, /* 上传大小限制,单位B */
'imageAllowFiles' => ['.png', '.jpg', '.jpeg', '.gif', '.bmp', ".webp"], /* 上传图片格式显示 */
"videoMaxSize" => 100 * 1024 * 1024, /* 上传大小限制,单位B */
"videoAllowFiles" => [
".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"
], /* 上传视频格式显示 */
"fileMaxSize" => 50 * 1024 * 1024, /* 上传大小限制,单位B */
"fileAllowFiles" => [
".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp",
".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
], /* 上传文件格式显示 */
]
],
'image_upload' => [
Expand Down

0 comments on commit 74e0f6e

Please sign in to comment.