From d823f74015c0feb59f234047e28c07e4a78e7c7c Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Wed, 11 Jan 2023 17:17:36 +0800 Subject: [PATCH] feat: add uploader --- .../Http/Controllers}/OptionController.php | 4 +- .../Http/Controllers/UploadController.php | 34 +++ .../Providers/CommonServiceProvider.php | 23 ++ modules/{Options => Common}/README.md | 0 .../Repository/Options}/Components.php | 2 +- .../Repository/Options}/Controllers.php | 2 +- .../Repository/Options}/DataRange.php | 2 +- .../Repository/Options}/Factory.php | 2 +- .../Repository/Options}/Modules.php | 2 +- .../Repository/Options}/OptionInterface.php | 2 +- .../Repository/Options}/Status.php | 2 +- modules/Common/Support/Upload/Uploader.php | 78 ++++++ .../Support/Upload/Uses/LocalUpload.php | 54 ++++ modules/Common/Support/Upload/Uses/Upload.php | 237 ++++++++++++++++++ modules/Common/route.php | 22 ++ 15 files changed, 457 insertions(+), 9 deletions(-) rename modules/{Options/Http => Common/Http/Controllers}/OptionController.php (75%) create mode 100644 modules/Common/Http/Controllers/UploadController.php create mode 100644 modules/Common/Providers/CommonServiceProvider.php rename modules/{Options => Common}/README.md (100%) rename modules/{Options/Repository => Common/Repository/Options}/Components.php (95%) rename modules/{Options/Repository => Common/Repository/Options}/Controllers.php (94%) rename modules/{Options/Repository => Common/Repository/Options}/DataRange.php (95%) rename modules/{Options/Repository => Common/Repository/Options}/Factory.php (92%) rename modules/{Options/Repository => Common/Repository/Options}/Modules.php (91%) rename modules/{Options/Repository => Common/Repository/Options}/OptionInterface.php (76%) rename modules/{Options/Repository => Common/Repository/Options}/Status.php (90%) create mode 100644 modules/Common/Support/Upload/Uploader.php create mode 100644 modules/Common/Support/Upload/Uses/LocalUpload.php create mode 100644 modules/Common/Support/Upload/Uses/Upload.php create mode 100644 modules/Common/route.php diff --git a/modules/Options/Http/OptionController.php b/modules/Common/Http/Controllers/OptionController.php similarity index 75% rename from modules/Options/Http/OptionController.php rename to modules/Common/Http/Controllers/OptionController.php index 2a3ec52..97a9bd0 100644 --- a/modules/Options/Http/OptionController.php +++ b/modules/Common/Http/Controllers/OptionController.php @@ -1,9 +1,9 @@ upload($request->file('file')); + } + + /** + * image + * + * @param Request $request + * @param Uploader $uploader + * @return string + */ + public function image(Request $request, Uploader $uploader) + { + return $uploader->upload($request->file('image')); + } +} diff --git a/modules/Common/Providers/CommonServiceProvider.php b/modules/Common/Providers/CommonServiceProvider.php new file mode 100644 index 0000000..35cc03b --- /dev/null +++ b/modules/Common/Providers/CommonServiceProvider.php @@ -0,0 +1,23 @@ +getDriver()->setUploadedFile($file)->upload(); + } catch (\Exception $exception) { + throw new FailedException($exception->getMessage()); + } + } + + + /** + * get driver + * + */ + public function getDriver() + { + $drivers = $this->getDrivers(); + + $driver = $drivers[$this->driver] ?? null; + + if (! $driver) { + throw new FailedException('Upload Driver Not Found'); + } + + return app($driver); + } + + + /** + * set driver + * + * @param string $driver + * @return $this + */ + public function setDriver(string $driver): static + { + $this->driver = $driver; + + return $this; + } + + /** + * get drivers + * + * @return string[] + */ + public function getDrivers(): array + { + return [ + 'local' => LocalUpload::class + ]; + } +} diff --git a/modules/Common/Support/Upload/Uses/LocalUpload.php b/modules/Common/Support/Upload/Uses/LocalUpload.php new file mode 100644 index 0000000..3202fbc --- /dev/null +++ b/modules/Common/Support/Upload/Uses/LocalUpload.php @@ -0,0 +1,54 @@ +addUrl($this->getUploadPath()); + } + + /** + * app url + * + * @param $path + * @return mixed + */ + protected function addUrl($path): mixed + { + $path['path'] = config('app.url') . '/'. $path['path']; + + return $path; + } + + + /** + * local upload + * + * @return string + */ + protected function localUpload(): string + { + $this->checkSize(); + + $storePath = 'uploads' . DIRECTORY_SEPARATOR . $this->getUploadedFileMimeType() . DIRECTORY_SEPARATOR . date('Y-m-d', time()); + + $filename = $this->generateImageName($this->getUploadedFileExt()); + + Storage::build([ + 'driver' => 'local', + 'root' => $storePath + ])->put($filename, $this->file->getContent()); + + return $storePath . DIRECTORY_SEPARATOR . $filename; + } +} diff --git a/modules/Common/Support/Upload/Uses/Upload.php b/modules/Common/Support/Upload/Uses/Upload.php new file mode 100644 index 0000000..89bdb10 --- /dev/null +++ b/modules/Common/Support/Upload/Uses/Upload.php @@ -0,0 +1,237 @@ +checkExt(); + + $this->checkSize(); + + // 如果是上传图片资源的的话保存 + // 如果是由其他方式上传的图片路径就直接返回 + if (!$this->file instanceof UploadedFile) { + return $this->file; + } + + // if ($this instanceof OssUploadService) { + // return $this->file->getPathname(); + // } + + return true; + } + + /** + * + * @return array + */ + public function getUploadPath(): array + { + $method = $this->getUploadMethod(); + + return $this->info($this->{$method}()); + } + + /** + * 生成文件名称 + * + * @time 2019年07月26日 + * @param string $ext + * @return string + */ + protected function generateImageName(string $ext): string + { + $filename = $this->params['filename'] ?? ''; + + $randomString = date('Y') . Str::random(10) . time(); + + if ($filename) { + $randomString = $filename . '_' . $randomString; + } + + return $randomString . '.' . $ext; + } + + /** + * upload method + * + * @return string + */ + protected function getUploadMethod(): string + { + $class = get_called_class(); + + $class = explode('\\', $class); + + $className = array_pop($class); + + $method = lcfirst($className); + + if (!method_exists($this, $method)) { + throw new FailedException(sprintf('Method %s in Class %s Not Found~', $method, $className)); + } + + return $method; + } + + /** + * get uploaded file info + * + * @param $path + * @return array + */ + protected function info($path): array + { + return [ + 'path' => $path, + 'ext' => $this->getUploadedFileExt(), + 'type' => $this->getUploadedFileMimeType(), + 'size' => $this->getUploadedFileSize(), + 'originalName' => $this->getOriginName(), + ]; + } + + /** + * check extension + */ + protected function checkExt(): void + { + $extensions = config(sprintf('upload.%s.ext', $this->getUploadedFileMimeType())); + + $fileExt = $this->getUploadedFileExt(); + + if (!in_array($fileExt, $extensions)) { + throw new FailedException(sprintf('不支持该上传文件类型(%s)类型', $fileExt)); + } + } + + /** + * check file size + */ + protected function checkSize(): void + { + $size = 10 * 1024 * 1024; + + if ($this->getUploadedFileSize() > $size) { + throw new FailedException('超过文件最大支持的大小'); + } + } + + /** + * get uploaded file mime type + * + * @return string + */ + protected function getUploadedFileMimeType(): string + { + if ($this->file instanceof UploadedFile) { + + $imageMimeType = [ + 'image/gif', 'image/jpeg', 'image/png', 'application/x-shockwave-flash', + 'image/psd', 'image/bmp', 'image/tiff', 'image/jp2', + 'application/x-shockwave-flash', 'image/iff', 'image/vnd.wap.wbmp', 'image/xbm', + 'image/vnd.microsoft.icon', 'image/x-icon', 'image/*', 'image/jpg', + ]; + + return in_array($this->file->getClientMimeType(), $imageMimeType) ? 'image' : 'file'; + } + + return in_array($this->getUploadedFileExt(), config('upload.image.ext')) ? 'image' : 'file'; + } + + + /** + * get uploaded file extension + * + * @return array|string + */ + protected function getUploadedFileExt(): array|string + { + if ($this->file instanceof UploadedFile) { + return strtolower($this->file->getClientOriginalExtension()); + } + + // 直传文件 + return pathinfo($this->file, PATHINFO_EXTENSION); + } + + /** + * get uploaded file size + * + * @return false|int + */ + protected function getUploadedFileSize(): bool|int + { + if ($this->file instanceof UploadedFile) { + return $this->file->getSize(); + } + + return 0; + } + + /** + * get origin name + * + * @return string|null + */ + public function getOriginName(): ?string + { + // 上传图片获取 + if ($this->file instanceof UploadedFile) { + return $this->file->getClientOriginalName(); + } + + return ''; + } + + + /** + * 参数设置 + * + * @time 2019年07月25日 + * @param $name + * @param $value + */ + public function __set($name, $value) + { + $this->{$name} = $value; + } + + /** + * set uploaded file + * + * @param mixed $file + * @return $this + */ + public function setUploadedFile(mixed $file): static + { + $this->file = $file; + + return $this; + } +} diff --git a/modules/Common/route.php b/modules/Common/route.php new file mode 100644 index 0000000..4a324b7 --- /dev/null +++ b/modules/Common/route.php @@ -0,0 +1,22 @@ +