From b019cafb7185007f9db58b12eec554cd4974a1f8 Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Fri, 1 Dec 2023 14:07:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20app/Http/Controllers/WebCo?= =?UTF-8?q?ntroller.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/WebController.php | 63 ++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 app/Http/Controllers/WebController.php diff --git a/app/Http/Controllers/WebController.php b/app/Http/Controllers/WebController.php new file mode 100644 index 0000000..3e1ca42 --- /dev/null +++ b/app/Http/Controllers/WebController.php @@ -0,0 +1,63 @@ +input('path', 'uploads').'/'.date('Y-m-d'); + $result = []; + + $disk = $this->getDisk(); + + // file 文件 + $files = $request->except('path'); + foreach ($files as $key => $fileData) { + $item = null; + if (is_array($fileData)) { + foreach ($fileData as $file) { + $item[] = $disk->url($this->saveFile($disk, $path, $file)); + } + } else { + $item = $disk->url($this->saveFile($disk, $path, $fileData)); + } + $result[$key] = $item; + } + + return $this->response()->success($result); + } + + protected function saveFile($disk, $path, $file = null) + { + $filePath = ''; + if ($file instanceof UploadedFile) { + //限制文件类型: + if(in_array($file->getClientOriginalExtension(), [ + 'jpeg', 'jpg', 'gif', 'bmp', 'png', //图片 + 'mp4', //视频 + 'mp3', //音频 + ])){ + $filePath = $disk->putFile($path, $file); + } + } elseif (preg_match('/^(data:\s*image\/(\w+);base64,)/', $file, $result)) { + $type = $result[2]; + if (in_array($type, ['jpeg', 'jpg', 'gif', 'bmp', 'png'])) { + $filePath = $path.'/'.uniqid().'.'.$type; + $disk->put($filePath, base64_decode(str_replace($result[1], '', $file))); + } + } + + return $filePath; + } + + protected function getDisk($disk = null): Filesystem + { + return Storage::disk($disk); + } +}