Basics.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\library\storage\engine;
  13. use Exception;
  14. use think\facade\Request;
  15. use think\file\UploadedFile;
  16. use cores\traits\ErrorTrait;
  17. use app\common\exception\BaseException;
  18. /**
  19. * 存储引擎抽象类
  20. * Class Basics
  21. * @package app\common\library\storage\drivers
  22. */
  23. abstract class Basics
  24. {
  25. use ErrorTrait;
  26. // 当前存储引擎
  27. protected $storage;
  28. // 存储配置
  29. protected $config;
  30. // file对象句柄
  31. /* @var $file UploadedFile */
  32. protected $file;
  33. // 验证规则
  34. protected $validateRuleScene;
  35. // 磁盘配置
  36. protected $disk = 'public';
  37. // 保存的根文件夹名称
  38. protected $rootDirName;
  39. /**
  40. * 构造函数
  41. * Server constructor.
  42. * @param string $storage 存储方式
  43. * @param array|null $config 存储配置
  44. */
  45. public function __construct(string $storage, array $config = null)
  46. {
  47. $this->storage = $storage;
  48. $this->config = $config;
  49. }
  50. /**
  51. * 设置上传的文件信息 (外部用户上传)
  52. * @param string $name
  53. * @return $this
  54. * @throws \cores\exception\BaseException
  55. */
  56. public function setUploadFile(string $name): Basics
  57. {
  58. // 接收上传的文件
  59. try {
  60. $this->file = Request::file($name);
  61. } catch (Exception $e) {
  62. $this->throwFileError($e);
  63. }
  64. empty($this->file) && throwError('未找到上传文件的信息');
  65. return $this;
  66. }
  67. /**
  68. * 文件异常处理
  69. * @param Exception $e
  70. * @throws \cores\exception\BaseException
  71. */
  72. private function throwFileError(Exception $e)
  73. {
  74. $maxSize = ini_get('upload_max_filesize');
  75. $myMsg = $e->getCode() === 1 ? "上传的文件超出了服务器最大限制: {$maxSize};可修改php.ini文件中upload_max_filesize项调整" : false;
  76. throwError($myMsg ?: $e->getMessage());
  77. }
  78. /**
  79. * 设置上传的文件信息 (系统内部上传)
  80. * @param string $filePath 文件路径
  81. * @return $this
  82. * @throws \cores\exception\BaseException
  83. */
  84. public function setUploadFileByReal(string $filePath): Basics
  85. {
  86. // 接收上传的文件
  87. $this->file = new UploadedFile($filePath, basename($filePath));
  88. if (empty($this->file)) {
  89. throwError('未找到上传文件的信息');
  90. }
  91. return $this;
  92. }
  93. /**
  94. * 设置上传文件的验证规则
  95. * @param string $scene
  96. * @return Basics
  97. */
  98. public function setValidationScene(string $scene = ''): Basics
  99. {
  100. $this->validateRuleScene = $scene;
  101. return $this;
  102. }
  103. /**
  104. * 设置磁盘配置
  105. * @param string $disk
  106. * @return $this
  107. */
  108. public function setDisk(string $disk): Basics
  109. {
  110. $this->disk = $disk;
  111. return $this;
  112. }
  113. /**
  114. * 设置上传的文件根目录名称
  115. * [通常是商城的id, 例如: 10001]
  116. * @param string $name
  117. * @return $this
  118. */
  119. public function setRootDirName(string $name): Basics
  120. {
  121. $this->rootDirName = $name;
  122. return $this;
  123. }
  124. /**
  125. * 文件上传
  126. * @return mixed
  127. */
  128. abstract protected function upload();
  129. /**
  130. * 文件删除
  131. * @param string $filePath
  132. * @return mixed
  133. */
  134. abstract protected function delete(string $filePath);
  135. /**
  136. * 临时文件的绝对路径
  137. * @return mixed
  138. */
  139. protected function getRealPath(): string
  140. {
  141. return $this->file->getRealPath();
  142. }
  143. /**
  144. * 返回错误信息
  145. * @return mixed
  146. */
  147. public function getError(): string
  148. {
  149. return $this->error;
  150. }
  151. /**
  152. * 生成保存的文件信息
  153. * @return array
  154. */
  155. public function getSaveFileInfo(): array
  156. {
  157. // 自动生成的文件名称
  158. //$hashName = $this->file->hashName(null);
  159. $hashName = $this->hashName();
  160. // 存储目录
  161. $filePath = $this->getFilePath($hashName);
  162. // 文件名称
  163. // 去除扩展名的写法 stristr($this->file->getOriginalName(), '.', true)
  164. $fileName = $this->file->getOriginalName();
  165. // 文件扩展名
  166. $fileExt = strtolower($this->file->extension());
  167. return [
  168. 'storage' => $this->storage, // 存储方式
  169. 'domain' => $this->config['domain'] ?? '', // 存储域名
  170. 'file_path' => $filePath, // 文件路径
  171. 'file_name' => $fileName, // 文件名称
  172. 'file_size' => $this->file->getSize(), // 文件大小(字节)
  173. 'file_ext' => $fileExt, // 文件扩展名
  174. ];
  175. }
  176. /**
  177. * 自动生成文件名
  178. * @return string
  179. */
  180. private function hashName(): string
  181. {
  182. return $this->file->hashName(function () {
  183. return date('Ymd') . DIRECTORY_SEPARATOR . md5(uniqid((string)mt_rand(), true));
  184. });
  185. }
  186. /**
  187. * 获取hashName的路径
  188. * @param string $hashName
  189. * @return string
  190. */
  191. private function getFilePath(string $hashName): string
  192. {
  193. $filePath = empty($this->rootDirName) ? "{$hashName}" : "{$this->rootDirName}/{$hashName}";
  194. return convert_left_slash($filePath);
  195. }
  196. /**
  197. * 获取hashName的文件名
  198. * @param string $filePath
  199. * @return string
  200. */
  201. protected function getFileHashName(string $filePath): string
  202. {
  203. return basename($filePath);
  204. }
  205. /**
  206. * 获取hashName的文件目录
  207. * @param string $filePath
  208. * @return string
  209. */
  210. protected function getFileHashRoute(string $filePath): string
  211. {
  212. return dirname($filePath);
  213. }
  214. }