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); + } +}