123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace App\Services\Interface\Database\Uploader;
- use App\Services\Interface\Database\CommonDb;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Request;
- use Illuminate\Support\Str;
- class UploaderDb extends CommonDb
- {
- private array $uploadConfig = [];
- public function __construct(array $interfaceInfo, array $assembleInfo, array $assembleColumns, array $requestData, array $columnIdToCodes)
- {
- parent::__construct($interfaceInfo, $assembleInfo, $assembleColumns, $requestData, $columnIdToCodes);
- }
- public function exec()
- {
- if (!isset($this->interfaceInfo['upload_config']) || empty($this->interfaceInfo['upload_config'])) {
- abort(508, '接口错误,上传参数配置有误! ');
- }
- // 获取接口信息
- $this->uploadConfig = json_decode($this->interfaceInfo['upload_config'], true);
- $requestSettings = json_decode($this->interfaceInfo['request_settings'], true);
- if (empty($requestSettings)) {
- $fileKey = 'file';
- } else {
- $fileKey = $this->uploadConfig['file'] ?? "file";
- }
- $type = $this->uploadConfig['type'];
- if ($type == 2) {
- $fileObj = $this->requestData[$fileKey];
- if (empty($fileObj)) {
- abort(508, '上传有误!文件不存在!');
- }
- unset($this->requestData[$fileKey]);
- // 图片base64
- return $this->upBase64($fileObj);
- } elseif ($type == 3) {
- // 二进制流
- dd('暂不支持');
- } else {
- $fileObj = Request::file($fileKey);
- if (empty($fileObj)) {
- abort(508, '上传有误!文件不存在!');
- }
- unset($this->requestData[$fileKey]);
- return $this->uploadFilePath($fileObj);
- }
- }
- /**
- * 上传
- *
- * @param UploadedFile $file
- * @return JsonResponse
- */
- private function uploadFilePath(UploadedFile $file): JsonResponse
- {
- $fileName = $file->getClientOriginalName();
- $size = $file->getSize();
- $path = $this->uploadConfig['savePath'];
- $isRandomFilename = $this->uploadConfig['isRandomFilename'];
- if (empty($path)) {
- $uploadDir = '/upload/' . mt_rand(0, 10000) . '/' . mt_rand(0, 10000);
- } else {
- $uploadDir = $this->getSavePath($path);
- }
- $disk_dir = public_path('static/' . $uploadDir);
- if (!file_exists($disk_dir) || !is_dir($disk_dir)) {
- @mkdir($disk_dir, 0777, true);
- }
- $extension = $file->getClientOriginalExtension();
- if ($isRandomFilename) {
- $fileName = md5(microtime()) . '.' . $extension;
- }
- $file->move($disk_dir, $fileName);
- $path = '/static' . $uploadDir . '/' . $fileName;
- $data = [
- 'filename' => $fileName,
- 'path' => $path,
- 'size' => $size,
- 'extension' => $extension
- ];
- $isSuccess = $this->saveData($data);
- if ($isSuccess) {
- return responseMessage(1001, '', $data);
- } else {
- return responseMessage(2001, '上传失败,请重试!');
- }
- }
- /**
- * 上传图片base64
- *
- * @param $base64Data
- * @return JsonResponse
- */
- public function upBase64($base64Data): JsonResponse
- {
- //截取字符串
- $base64Data = preg_replace('/data:image\/png;base64,/is', '', $base64Data);
- $img = base64_decode($base64Data);
- if (empty($img)) {
- return responseMessage(2002, '上传失败');
- }
- $path = $this->uploadConfig['savePath'];
- if (empty($path)) {
- $uploadDir = '/upload/' . mt_rand(0, 10000) . '/' . mt_rand(0, 10000) . '/';
- } else {
- $uploadDir = $this->getSavePath($path);
- }
- $disk_dir = public_path('static/' . $uploadDir);
- if (!file_exists($disk_dir) || !is_dir($disk_dir)) {
- @mkdir($disk_dir, 0777, true);
- }
- $fileName = md5(microtime()) . '.png';
- $filePath = $disk_dir . '/' . $fileName;
- if (!file_put_contents($filePath, $img)) { //失败
- return responseMessage(2003, '上传失败,请重试!');
- } else { //成功
- $data = [
- 'mid' => Str::random(12),
- 'filename' => $fileName,
- 'path' => $path,
- 'size' => strlen($img),
- 'extension' => 'png'
- ];
- $isSuccess = $this->saveData($data);
- if ($isSuccess) {
- $data['id'] = $isSuccess;
- return responseMessage(1001, '', $data);
- } else {
- return responseMessage(2001, '上传失败,请重试!');
- }
- }
- }
- private function saveData($data)
- {
- $find = DB::connection($this->getDbConnection())->table($this->table);
- if (!isset($this->requestData['mid'])) {
- $this->requestData['mid'] = Str::random(12);
- }
- if (!isset($this->requestData['created_at'])) {
- $this->requestData['created_at'] = time();
- }
- if (!isset($this->requestData['updated_at'])) {
- $this->requestData['updated_at'] = time();
- }
- $data = array_merge($this->requestData, $data);
- return $find->insertGetId($data);
- }
- /**
- * 获取保存路径,替换变量或随机数
- *
- * @param $path
- * @return string
- */
- private function getSavePath($path): string
- {
- // 变量替换
- preg_match_all('/\$\{(.*?)\}/i', $path, $arr);
- if (isset($arr[1]) && $arr[1]) {
- foreach ($arr[1] as $keyName) {
- $path = preg_replace('/\$\{' . $keyName . '\}/i', $this->requestData[$keyName], $path);
- }
- }
- // 随机数替换
- preg_match_all('/\$\[(.*?)\]/i', $path, $arr2);
- if (isset($arr2[1]) && $arr2[1]) {
- foreach ($arr2[1] as $keyName) {
- $exArr = explode('-', $keyName);
- if ($exArr[0]) {
- $start = intval($exArr[0]);
- } else {
- $start = 0;
- }
- if ($exArr[1]) {
- $end = intval($exArr[1]);
- } else {
- $end = 10000;
- }
- $path = preg_replace('/\$\[' . $keyName . '\]/i', mt_rand($start, $end), $path);
- }
- }
- return '/' . trim($path, '/');
- }
- }
|