BaseUpload.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace crmeb\basic;
  3. use crmeb\services\UtilService;
  4. use think\facade\Config;
  5. abstract class BaseUpload extends BaseStorage
  6. {
  7. /**
  8. * 图片信息
  9. * @var array
  10. */
  11. protected $fileInfo;
  12. /**
  13. * 验证配置
  14. * @var string
  15. */
  16. protected $validate;
  17. /**
  18. * 保存路径
  19. * @var string
  20. */
  21. protected $path = '';
  22. protected function initialize(array $config)
  23. {
  24. $this->fileInfo = new \StdClass();
  25. }
  26. /**
  27. * 上传文件路径
  28. * @param string $path
  29. * @return $this
  30. */
  31. public function to(string $path)
  32. {
  33. $this->path = $path;
  34. return $this;
  35. }
  36. /**
  37. * 获取文件信息
  38. * @return array
  39. */
  40. public function getFileInfo()
  41. {
  42. return $this->fileInfo;
  43. }
  44. /**
  45. * 验证合法上传域名
  46. * @param string $url
  47. * @return string
  48. */
  49. protected function checkUploadUrl(string $url)
  50. {
  51. if ($url && strstr($url, 'http') === false) {
  52. $url = 'http://' . $url;
  53. }
  54. return $url;
  55. }
  56. /**
  57. * 获取系统配置
  58. * @return mixed
  59. */
  60. protected function getConfig()
  61. {
  62. $config = Config::get($this->configFile . '.stores.' . $this->name, []);
  63. if (empty($config)) {
  64. $config['filesize'] = Config::get($this->configFile . '.filesize', []);
  65. $config['fileExt'] = Config::get($this->configFile . '.fileExt', []);
  66. $config['fileMime'] = Config::get($this->configFile . '.fileMime', []);
  67. }
  68. return $config;
  69. }
  70. /**
  71. * 设置验证规则
  72. * @param array|null $validate
  73. * @return $this
  74. */
  75. public function validate(?array $validate = null)
  76. {
  77. if (is_null($validate)) {
  78. $validate = $this->getConfig();
  79. }
  80. $this->extractValidate($validate);
  81. return $this;
  82. }
  83. /**
  84. * 提取上传验证
  85. */
  86. protected function extractValidate(array $validateArray)
  87. {
  88. $validate = [];
  89. foreach ($validateArray as $key => $value) {
  90. $validate[] = $key . ':' . (is_array($value) ? implode(',', $value) : $value);
  91. }
  92. $this->validate = implode('|', $validate);
  93. unset($validate);
  94. }
  95. /**
  96. * 提取文件名
  97. * @param string $path
  98. * @param string $ext
  99. * @return string
  100. */
  101. protected function saveFileName(string $path = null, string $ext = 'jpg')
  102. {
  103. return ($path ? substr(md5($path), 0, 5) : '') . date('YmdHis') . rand(0, 9999) . '.' . $ext;
  104. }
  105. /**
  106. * 获取文件类型和大小
  107. * @param string $url
  108. * @param bool $isData
  109. * @return array
  110. */
  111. protected function getFileHeaders(string $url, $isData = true)
  112. {
  113. stream_context_set_default(['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]]);
  114. $header['size'] = 0;
  115. $header['type'] = 'image/jpeg';
  116. if (!$isData) {
  117. return $header;
  118. }
  119. try {
  120. $headerArray = get_headers(str_replace('\\', '/', $url), true);
  121. if (!isset($headerArray['Content-Length'])) {
  122. $header['size'] = 0;
  123. }
  124. if (!isset($headerArray['Content-Type'])) {
  125. $header['type'] = 'image/jpeg';
  126. }
  127. if (is_array($headerArray['Content-Type']) && count($headerArray['Content-Length']) == 2) {
  128. $header['size'] = $headerArray['Content-Length'][1];
  129. }
  130. if (is_array($headerArray['Content-Type']) && count($headerArray['Content-Type']) == 2) {
  131. $header['type'] = $headerArray['Content-Type'][1];
  132. }
  133. } catch (\Exception $e) {
  134. }
  135. return $header;
  136. }
  137. /**
  138. * 获取上传信息
  139. * @return array
  140. */
  141. public function getUploadInfo()
  142. {
  143. if (isset($this->fileInfo->filePath)) {
  144. if (strstr($this->fileInfo->filePath, 'http') === false) {
  145. $url = request()->domain() . $this->fileInfo->filePath;
  146. } else {
  147. $url = $this->fileInfo->filePath;
  148. }
  149. $headers = $this->getFileHeaders($url);
  150. return [
  151. 'name' => $this->fileInfo->fileName,
  152. 'size' => $headers['size'] ?? 0,
  153. 'type' => $headers['type'] ?? 'image/jpeg',
  154. 'dir' => $this->fileInfo->filePath,
  155. 'thumb_path' => $this->fileInfo->filePath,
  156. 'time' => time(),
  157. ];
  158. } else {
  159. return [];
  160. }
  161. }
  162. /**
  163. * 文件上传
  164. * @return mixed
  165. */
  166. abstract public function move(string $file = 'file');
  167. /**
  168. * 文件流上传
  169. * @return mixed
  170. */
  171. abstract public function stream(string $fileContent, string $key = null);
  172. /**
  173. * 删除文件
  174. * @return mixed
  175. */
  176. abstract public function delete(string $filePath);
  177. /**
  178. * 实例化上传
  179. * @return mixed
  180. */
  181. abstract protected function app();
  182. }