You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.1 KiB
73 lines
2.1 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\ApiResponseService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class UploadController extends Controller
|
|
{
|
|
/**
|
|
* @var ApiResponseService
|
|
*/
|
|
protected ApiResponseService $responseService;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param ApiResponseService $responseService
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService,
|
|
) {
|
|
$this->responseService = $responseService;
|
|
}
|
|
|
|
/**
|
|
* 上传图片
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
* @throws ValidationException
|
|
*/
|
|
public function uploadImage(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$rules = [
|
|
'file' => 'required|image|max:10240', // 10MB = 10240KB
|
|
];
|
|
$messages = [
|
|
'file.required' => __('validation.upload.i_empty'),
|
|
'file.image' => __('validation.upload.i_image'),
|
|
'file.max' => __('validation.upload.i_max'),
|
|
];
|
|
$validator = Validator::make($request->all(), $rules, $messages);
|
|
if ($validator->fails()) {
|
|
throw new ValidationException($validator);
|
|
}
|
|
$file = $request->file('file');
|
|
$filename = time() . '_' . md5($file->getClientOriginalName()) . '.'
|
|
. $file->getClientOriginalExtension();
|
|
$path = $file->storeAs(
|
|
'images/floor/' . date("Ymd"),
|
|
$filename,
|
|
'public'
|
|
);
|
|
return $this->responseService->success(
|
|
['url' => Storage::url($path)]
|
|
);
|
|
} catch (ValidationException $e) {
|
|
throw $e;
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.upload.error') . ':'
|
|
. $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
|
|
}
|
|
|