Basics.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. $this->file = Request::instance()->file($name);
  59. if (empty($this->file)) {
  60. throwError('未找到上传文件的信息');
  61. }
  62. return $this;
  63. }
  64. /**
  65. * 设置上传的文件信息 (系统内部上传)
  66. * @param string $filePath 文件路径
  67. * @return $this
  68. * @throws BaseException
  69. */
  70. public function setUploadFileByReal(string $filePath)
  71. {
  72. // 接收上传的文件
  73. $this->file = new UploadedFile($filePath, basename($filePath));
  74. if (empty($this->file)) {
  75. throwError('未找到上传文件的信息');
  76. }
  77. return $this;
  78. }
  79. /**
  80. * 设置上传文件的验证规则
  81. * @param string $scene
  82. * @return mixed
  83. */
  84. public function setValidationScene(string $scene = '')
  85. {
  86. $this->validateRuleScene = $scene;
  87. return $this;
  88. }
  89. /**
  90. * 设置磁盘配置
  91. * @param string $disk
  92. * @return $this
  93. */
  94. public function setDisk(string $disk)
  95. {
  96. $this->disk = $disk;
  97. return $this;
  98. }
  99. /**
  100. * 设置上传的文件根目录名称
  101. * [通常是商城的id, 例如: 10001]
  102. * @param string $name
  103. * @return $this
  104. */
  105. public function setRootDirName(string $name)
  106. {
  107. $this->rootDirName = $name;
  108. return $this;
  109. }
  110. /**
  111. * 文件上传
  112. * @return mixed
  113. */
  114. abstract protected function upload();
  115. /**
  116. * 文件删除
  117. * @param string $filePath
  118. * @return mixed
  119. */
  120. abstract protected function delete(string $filePath);
  121. /**
  122. * 临时文件的绝对路径
  123. * @return mixed
  124. */
  125. protected function getRealPath(): string
  126. {
  127. return $this->file->getRealPath();
  128. }
  129. /**
  130. * 返回错误信息
  131. * @return mixed
  132. */
  133. public function getError(): string
  134. {
  135. return $this->error;
  136. }
  137. /**
  138. * 生成保存的文件信息
  139. * @return array
  140. */
  141. public function getSaveFileInfo(): array
  142. {
  143. // 自动生成的文件名称
  144. // $hashName = $this->file->hashName(null);
  145. $hashName = $this->hashName();
  146. // 存储目录
  147. $filePath = $this->getFilePath($hashName);
  148. // 文件名称
  149. // 去除扩展名的写法 stristr($this->file->getOriginalName(), '.', true)
  150. $fileName = $this->file->getOriginalName();
  151. // 文件扩展名
  152. $fileExt = strtolower($this->file->extension());
  153. return [
  154. 'storage' => $this->storage, // 存储方式
  155. 'domain' => $this->config['domain'] ?? '', // 存储域名
  156. 'file_path' => $filePath, // 文件路径
  157. 'file_name' => $fileName, // 文件名称
  158. 'file_size' => $this->file->getSize(), // 文件大小(字节)
  159. 'file_ext' => $fileExt, // 文件扩展名
  160. ];
  161. }
  162. /**
  163. * 自动生成文件名
  164. * @return string
  165. */
  166. private function hashName()
  167. {
  168. return $this->file->hashName(function () {
  169. return date('Ymd') . DIRECTORY_SEPARATOR . md5(uniqid((string)mt_rand(), true));
  170. });
  171. }
  172. /**
  173. * 获取hashName的路径
  174. * @param string $hashName
  175. * @return string
  176. */
  177. private function getFilePath(string $hashName): string
  178. {
  179. $filePath = empty($this->rootDirName) ? "{$hashName}" : "{$this->rootDirName}/{$hashName}";
  180. return convert_left_slash($filePath);
  181. }
  182. /**
  183. * 获取hashName的文件名
  184. * @param string $filePath
  185. * @return string
  186. */
  187. protected function getFileHashName(string $filePath): string
  188. {
  189. return basename($filePath);
  190. }
  191. /**
  192. * 获取hashName的文件目录
  193. * @param string $filePath
  194. * @return string
  195. */
  196. protected function getFileHashRoute(string $filePath): string
  197. {
  198. return dirname($filePath);
  199. }
  200. }