BaseUpload.php 5.1 KB

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