Driver.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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;
  13. use think\Exception;
  14. use app\common\library\storage\engine\Aliyun;
  15. use app\common\library\storage\engine\Local;
  16. use app\common\library\storage\engine\Qcloud;
  17. use app\common\library\storage\engine\Qiniu;
  18. use app\common\enum\file\Storage as StorageEnum;
  19. /**
  20. * 存储模块驱动
  21. * Class driver
  22. * @package app\common\library\storage
  23. */
  24. class Driver
  25. {
  26. // upload 配置
  27. private array $config;
  28. // 当前存储引擎类
  29. private $engine;
  30. /**
  31. * 存储引擎类列表
  32. */
  33. const ENGINE_CLASS_LIST = [
  34. StorageEnum::LOCAL => Local::class,
  35. StorageEnum::QINIU => QINIU::class,
  36. StorageEnum::ALIYUN => Aliyun::class,
  37. StorageEnum::QCLOUD => Qcloud::class
  38. ];
  39. /**
  40. * 构造方法
  41. * Driver constructor.
  42. * @param array $config
  43. * @param string|null $storage 指定存储方式,如不指定则为系统默认
  44. * @throws Exception
  45. */
  46. public function __construct(array $config, string $storage = null)
  47. {
  48. // upload 配置
  49. $this->config = $config;
  50. // 实例化当前存储引擎
  51. $this->engine = $this->getEngineClass($storage);
  52. }
  53. /**
  54. * 设置上传的文件信息
  55. * @param string $name
  56. * @return mixed
  57. */
  58. public function setUploadFile(string $name = 'iFile')
  59. {
  60. return $this->engine->setUploadFile($name);
  61. }
  62. /**
  63. * 设置上传的文件信息
  64. * @param string $filePath
  65. * @return mixed
  66. */
  67. public function setUploadFileByReal(string $filePath)
  68. {
  69. return $this->engine->setUploadFileByReal($filePath);
  70. }
  71. /**
  72. * 设置上传的文件信息
  73. * @param string $name
  74. * @return mixed
  75. */
  76. public function setRootName(string $name = '')
  77. {
  78. return $this->engine->setRootName($name);
  79. }
  80. /**
  81. * 设置上传文件的验证规则
  82. * @param array $rules
  83. * @return mixed
  84. */
  85. public function setValidationScene(array $rules = [])
  86. {
  87. return $this->engine->setValidationScene($rules);
  88. }
  89. /**
  90. * 执行文件上传
  91. */
  92. public function upload()
  93. {
  94. return $this->engine->upload();
  95. }
  96. /**
  97. * 执行文件删除
  98. * @param string $filePath
  99. * @return mixed
  100. */
  101. public function delete(string $filePath)
  102. {
  103. return $this->engine->delete($filePath);
  104. }
  105. /**
  106. * 获取错误信息
  107. * @return mixed
  108. */
  109. public function getError()
  110. {
  111. return $this->engine->getError();
  112. }
  113. /**
  114. * 返回保存的文件信息
  115. * @return array
  116. */
  117. public function getSaveFileInfo(): array
  118. {
  119. return $this->engine->getSaveFileInfo();
  120. }
  121. /**
  122. * 获取当前的存储引擎
  123. * @param string|null $storage 指定存储方式,如不指定则为系统默认
  124. * @return mixed
  125. * @throws Exception
  126. */
  127. private function getEngineClass(string $storage = null)
  128. {
  129. $storage = is_null($storage) ? $this->config['default'] : $storage;
  130. if (!isset(self::ENGINE_CLASS_LIST[$storage])) {
  131. throw new Exception("未找到存储引擎类: {$storage}");
  132. }
  133. $class = self::ENGINE_CLASS_LIST[$storage];
  134. return new $class($storage, $this->config['engine'][$storage]);
  135. }
  136. }