|
@@ -72,22 +72,14 @@ class Upload extends Controller
|
|
|
{
|
|
{
|
|
|
try {
|
|
try {
|
|
|
request()->validate([
|
|
request()->validate([
|
|
|
- 'file' => [
|
|
|
|
|
- 'required',
|
|
|
|
|
- 'file',
|
|
|
|
|
- 'mimes:jpg,jpeg,png,webp,mp4,mov,avi,webm',
|
|
|
|
|
- 'max:51200' // 最大 50MB,单位KB
|
|
|
|
|
- ],
|
|
|
|
|
|
|
+ 'file' => ['required', 'file', 'mimes:jpg,jpeg,png,webp,mp4,mov,avi,webm', 'max:51200'],// 最大 50MB,单位KB
|
|
|
]);
|
|
]);
|
|
|
-
|
|
|
|
|
if (request()->hasFile('file')) {
|
|
if (request()->hasFile('file')) {
|
|
|
$file = request()->file('file');
|
|
$file = request()->file('file');
|
|
|
$fileName = md5_file($file->getRealPath());
|
|
$fileName = md5_file($file->getRealPath());
|
|
|
-
|
|
|
|
|
// 获取原始扩展名
|
|
// 获取原始扩展名
|
|
|
$extension = $file->getClientOriginalExtension();
|
|
$extension = $file->getClientOriginalExtension();
|
|
|
$fileName .= $extension ? ('.' . $extension) : '';
|
|
$fileName .= $extension ? ('.' . $extension) : '';
|
|
|
-
|
|
|
|
|
// 根据类型判断存放目录(可选)
|
|
// 根据类型判断存放目录(可选)
|
|
|
$isImage = in_array(strtolower($extension), ['jpg', 'jpeg', 'png', 'webp']);
|
|
$isImage = in_array(strtolower($extension), ['jpg', 'jpeg', 'png', 'webp']);
|
|
|
$dir = $isImage ? 'images' : 'videos';
|
|
$dir = $isImage ? 'images' : 'videos';
|
|
@@ -106,7 +98,7 @@ class Upload extends Controller
|
|
|
} catch (ValidationException $e) {
|
|
} catch (ValidationException $e) {
|
|
|
return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
|
|
return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
|
|
|
} catch (Exception $e) {
|
|
} catch (Exception $e) {
|
|
|
- return $this->error($e->getCode());
|
|
|
|
|
|
|
+ return $this->error($e->getCode(), $e->getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return $this->success(['path' => Util::ensureUrl($path)]);
|
|
return $this->success(['path' => Util::ensureUrl($path)]);
|