BaseUpload.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace core\upload;
  12. use core\exception\UploadFileException;
  13. use core\loader\Storage;
  14. /**
  15. * Class BaseUpload
  16. */
  17. abstract class BaseUpload extends Storage
  18. {
  19. protected $file;//上传的文件对象
  20. protected $file_info;//上传的文件属性参数
  21. protected $file_name;//新文件名
  22. protected $name;
  23. protected $full_file;//完整的文件地址
  24. protected $full_path;
  25. protected $validate;
  26. protected $type;
  27. protected $config;
  28. //可能还需要一个完整路径
  29. protected $storage_type;
  30. /**
  31. * 初始化
  32. * @param array $config
  33. * @return void
  34. */
  35. protected function initialize(array $config = [])
  36. {
  37. $this->config = $config;
  38. $this->storage_type = $config['storage_type'];
  39. }
  40. /**
  41. * 附件上传
  42. * @param string $dir
  43. * @return mixed
  44. */
  45. abstract protected function upload(string $dir);
  46. /**
  47. * 抓取远程附件
  48. * @param string $url
  49. * @param string|null $key
  50. * @return mixed
  51. */
  52. abstract protected function fetch(string $url, ?string $key);
  53. /**
  54. * base64文件上云
  55. * @param string $base64_data
  56. * @param string|null $key
  57. * @return mixed
  58. */
  59. abstract protected function base64(string $base64_data, ?string $key = null);
  60. /**
  61. * 附件删除
  62. * @param string $file_name
  63. * @return mixed
  64. */
  65. abstract protected function delete(string $file_name);
  66. /**
  67. * 缩略图
  68. * @param string $file_path
  69. * @param $thumb_type
  70. * @return mixed
  71. */
  72. abstract protected function thumb(string $file_path, $thumb_type);
  73. /**
  74. * 读取文件
  75. * @param string $name
  76. * @param bool $is_rename
  77. */
  78. public function read(string $name, bool $is_rename = true)
  79. {
  80. $this->name = $name;
  81. $this->file = request()->file($name);
  82. if (empty($this->file))
  83. throw new UploadFileException(100012);
  84. $this->file_info = [
  85. 'name' => $this->file->getOriginalName(),//文件原始名称
  86. 'mime' => $this->file->getOriginalMime(),//上传文件类型信息
  87. 'real_path' => $this->file->getRealPath(),//上传文件真实路径
  88. 'ext' => $this->file->getOriginalExtension(),//上传文件后缀
  89. 'size' => $this->file->getSize(),//上传文件大小
  90. ];
  91. if ($is_rename) {
  92. $this->file_name = $this->createFileName();
  93. } else {
  94. $this->file_name = $this->file_info['name'];
  95. }
  96. }
  97. /**
  98. * 设置文件类型
  99. * @param string $type
  100. * @return $this
  101. */
  102. public function setType(string $type)
  103. {
  104. $this->type = $type;
  105. return $this;
  106. }
  107. /**
  108. * 校验文件是否合法
  109. */
  110. public function check()
  111. {
  112. }
  113. /**
  114. * 生成新的文件名
  115. * @return string
  116. */
  117. public function createFileName(string $key = '', string $ext = '')
  118. {
  119. //DIRECTORY_SEPARATOR 常量
  120. $storage_tag = '_' . $this->storage_type;
  121. if (empty($key)) {
  122. return time() . md5($this->file_info['real_path']) . $storage_tag . '.' . $this->file_info['ext'];
  123. } else {
  124. return time() . md5($key) . $storage_tag . '.' . $ext;
  125. }
  126. }
  127. /**
  128. * 获取原始附件信息
  129. * @return mixed
  130. */
  131. public function getFileInfo()
  132. {
  133. return $this->file_info;
  134. }
  135. /**
  136. * 获取上传文件的真实完整路径
  137. * @return mixed
  138. */
  139. public function getRealPath()
  140. {
  141. return $this->file_info['real_path'];
  142. }
  143. /**
  144. * 获取生成的文件完整地址
  145. * @return string
  146. */
  147. public function getFullPath(string $dir = '')
  148. {
  149. return $this->full_path ?: $this->concatFullPath($dir);
  150. }
  151. /**
  152. * 合并路径和文件名
  153. * @param string $dir
  154. * @return string
  155. */
  156. public function concatFullPath(string $dir = '')
  157. {
  158. $this->full_path = implode('/', array_filter([$dir, $this->getFileName()]));
  159. return $this->full_path;
  160. }
  161. /**
  162. * 获取文件名
  163. * @return mixed
  164. */
  165. public function getFileName()
  166. {
  167. return $this->file_name;
  168. }
  169. public function getUrl(string $path = '')
  170. {
  171. $path = !empty($path) ? $path : $this->getFullPath();
  172. $domain = $this->config['domain'] ?? '';
  173. $domain = empty($domain) ? '' : $domain . '/';
  174. return $domain . $path;
  175. }
  176. /**
  177. * 验证
  178. * @param array $validate
  179. * @return $this
  180. */
  181. public function setValidate(array $validate = [])
  182. {
  183. $this->validate = $validate ?: config('upload.rules')[$this->type] ?? [];
  184. return $this;
  185. }
  186. /**
  187. * 根据上传文件的类型来校验文件是否符合配置
  188. * @return void
  189. */
  190. public function validate()
  191. {
  192. if (empty($this->file))
  193. throw new UploadFileException('UPLOAD_FAIL');
  194. $config['file_ext'] = $this->validate['ext'] ?? [];
  195. $config['file_mime'] = $this->validate['mime'] ?? [];
  196. $config['file_size'] = $this->validate['size'] ?? 0;
  197. $rule = [];
  198. $file_size = $config['file_size'] ?? 0;
  199. if ($file_size > 0) {
  200. $rule[] = 'fileSize:' . $file_size;
  201. }
  202. //验证上传文件类型
  203. $file_mime = $config['file_mime'] ?? [];
  204. $file_ext = $config['file_ext'] ?? [];
  205. if (!empty($file_ext)) {
  206. $rule[] = 'fileExt:' . implode(',', $file_ext);
  207. }
  208. if (!empty($rule)) {
  209. if (!in_array($this->file->getOriginalMime(), $file_mime)) {
  210. throw new UploadFileException('UPLOAD_TYPE_NOT_SUPPORT');
  211. }
  212. validate([$this->name => implode('|', $rule)])->check([$this->name => $this->file]);
  213. }
  214. }
  215. }