Upload.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. namespace Modules\Common\Support\Upload\Uses;
  3. use Catch\Exceptions\FailedException;
  4. use Illuminate\Http\UploadedFile;
  5. use Illuminate\Support\Str;
  6. abstract class Upload
  7. {
  8. /**
  9. * uploadFile object
  10. *
  11. * @var mixed
  12. */
  13. protected mixed $file;
  14. /**
  15. *
  16. * @var array
  17. */
  18. protected array $params;
  19. public abstract function upload();
  20. /**
  21. *
  22. * @return mixed|true
  23. */
  24. protected function dealBeforeUpload(): mixed
  25. {
  26. $this->checkExt();
  27. $this->checkSize();
  28. // 如果是上传图片资源的的话保存
  29. // 如果是由其他方式上传的图片路径就直接返回
  30. if (!$this->file instanceof UploadedFile) {
  31. return $this->file;
  32. }
  33. // if ($this instanceof OssUploadService) {
  34. // return $this->file->getPathname();
  35. // }
  36. return true;
  37. }
  38. /**
  39. *
  40. * @return array
  41. */
  42. public function getUploadPath(): array
  43. {
  44. $method = $this->getUploadMethod();
  45. return $this->info($this->{$method}());
  46. }
  47. /**
  48. * 生成文件名称
  49. *
  50. * @time 2019年07月26日
  51. * @param string $ext
  52. * @return string
  53. */
  54. protected function generateImageName(string $ext): string
  55. {
  56. $filename = $this->params['filename'] ?? '';
  57. $randomString = date('Y') . Str::random(10) . time();
  58. if ($filename) {
  59. $randomString = $filename . '_' . $randomString;
  60. }
  61. return $randomString . '.' . $ext;
  62. }
  63. /**
  64. * upload method
  65. *
  66. * @return string
  67. */
  68. protected function getUploadMethod(): string
  69. {
  70. $class = get_called_class();
  71. $class = explode('\\', $class);
  72. $className = array_pop($class);
  73. $method = lcfirst($className);
  74. if (!method_exists($this, $method)) {
  75. throw new FailedException(sprintf('Method %s in Class %s Not Found~', $method, $className));
  76. }
  77. return $method;
  78. }
  79. /**
  80. * get uploaded file info
  81. *
  82. * @param $path
  83. * @return array
  84. */
  85. protected function info($path): array
  86. {
  87. return [
  88. 'path' => $path,
  89. 'ext' => $this->getUploadedFileExt(),
  90. 'type' => $this->getUploadedFileMimeType(),
  91. 'size' => $this->getUploadedFileSize(),
  92. 'originalName' => $this->getOriginName(),
  93. ];
  94. }
  95. /**
  96. * check extension
  97. */
  98. protected function checkExt(): void
  99. {
  100. $extensions = config(sprintf('upload.%s.ext', $this->getUploadedFileMimeType()));
  101. $fileExt = $this->getUploadedFileExt();
  102. if (!in_array($fileExt, $extensions)) {
  103. throw new FailedException(sprintf('不支持该上传文件类型(%s)类型', $fileExt));
  104. }
  105. }
  106. /**
  107. * check file size
  108. */
  109. protected function checkSize(): void
  110. {
  111. $size = 10 * 1024 * 1024;
  112. if ($this->getUploadedFileSize() > $size) {
  113. throw new FailedException('超过文件最大支持的大小');
  114. }
  115. }
  116. /**
  117. * get uploaded file mime type
  118. *
  119. * @return string
  120. */
  121. protected function getUploadedFileMimeType(): string
  122. {
  123. if ($this->file instanceof UploadedFile) {
  124. $imageMimeType = [
  125. 'image/gif', 'image/jpeg', 'image/png', 'application/x-shockwave-flash',
  126. 'image/psd', 'image/bmp', 'image/tiff', 'image/jp2',
  127. 'application/x-shockwave-flash', 'image/iff', 'image/vnd.wap.wbmp', 'image/xbm',
  128. 'image/vnd.microsoft.icon', 'image/x-icon', 'image/*', 'image/jpg',
  129. ];
  130. return in_array($this->file->getClientMimeType(), $imageMimeType) ? 'image' : 'file';
  131. }
  132. return in_array($this->getUploadedFileExt(), config('upload.image.ext')) ? 'image' : 'file';
  133. }
  134. /**
  135. * get uploaded file extension
  136. *
  137. * @return array|string
  138. */
  139. protected function getUploadedFileExt(): array|string
  140. {
  141. if ($this->file instanceof UploadedFile) {
  142. return strtolower($this->file->getClientOriginalExtension());
  143. }
  144. // 直传文件
  145. return pathinfo($this->file, PATHINFO_EXTENSION);
  146. }
  147. /**
  148. * get uploaded file size
  149. *
  150. * @return false|int
  151. */
  152. protected function getUploadedFileSize(): bool|int
  153. {
  154. if ($this->file instanceof UploadedFile) {
  155. return $this->file->getSize();
  156. }
  157. return 0;
  158. }
  159. /**
  160. * get origin name
  161. *
  162. * @return string|null
  163. */
  164. public function getOriginName(): ?string
  165. {
  166. // 上传图片获取
  167. if ($this->file instanceof UploadedFile) {
  168. return $this->file->getClientOriginalName();
  169. }
  170. return '';
  171. }
  172. /**
  173. * 参数设置
  174. *
  175. * @time 2019年07月25日
  176. * @param $name
  177. * @param $value
  178. */
  179. public function __set($name, $value)
  180. {
  181. $this->{$name} = $value;
  182. }
  183. /**
  184. * set uploaded file
  185. *
  186. * @param mixed $file
  187. * @return $this
  188. */
  189. public function setUploadedFile(mixed $file): static
  190. {
  191. $this->file = $file;
  192. return $this;
  193. }
  194. }