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