Driver.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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;
  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. private $config; // upload 配置
  27. private $engine; // 当前存储引擎类
  28. /**
  29. * 存储引擎类列表
  30. */
  31. const ENGINE_CLASS_LIST = [
  32. StorageEnum::LOCAL => Local::class,
  33. StorageEnum::QINIU => QINIU::class,
  34. StorageEnum::ALIYUN => Aliyun::class,
  35. StorageEnum::QCLOUD => Qcloud::class
  36. ];
  37. /**
  38. * 构造方法
  39. * Driver constructor.
  40. * @param $config
  41. * @param null|string $storage 指定存储方式,如不指定则为系统默认
  42. * @throws Exception
  43. */
  44. public function __construct($config, $storage = null)
  45. {
  46. $this->config = $config;
  47. // 实例化当前存储引擎
  48. $this->engine = $this->getEngineClass($storage);
  49. }
  50. /**
  51. * 设置上传的文件信息
  52. * @param string $name
  53. * @return mixed
  54. */
  55. public function setUploadFile($name = 'iFile')
  56. {
  57. return $this->engine->setUploadFile($name);
  58. }
  59. /**
  60. * 设置上传的文件信息
  61. * @param string $filePath
  62. * @return mixed
  63. */
  64. public function setUploadFileByReal(string $filePath)
  65. {
  66. return $this->engine->setUploadFileByReal($filePath);
  67. }
  68. /**
  69. * 设置上传的文件信息
  70. * @param string $name
  71. * @return mixed
  72. */
  73. public function setRootName($name = '')
  74. {
  75. return $this->engine->setRootName($name);
  76. }
  77. /**
  78. * 设置上传文件的验证规则
  79. * @param array $rules
  80. * @return mixed
  81. */
  82. public function setValidationScene(array $rules = [])
  83. {
  84. return $this->engine->setValidationScene($rules);
  85. }
  86. /**
  87. * 执行文件上传
  88. */
  89. public function upload()
  90. {
  91. return $this->engine->upload();
  92. }
  93. /**
  94. * 执行文件删除
  95. * @param string $filePath
  96. * @return mixed
  97. */
  98. public function delete(string $filePath)
  99. {
  100. return $this->engine->delete($filePath);
  101. }
  102. /**
  103. * 执行文件下载
  104. * @param string $filePath
  105. * @return mixed
  106. */
  107. public function download(string $filePath)
  108. {
  109. return $this->engine->download($filePath);
  110. }
  111. /**
  112. * 获取错误信息
  113. * @return mixed
  114. */
  115. public function getError()
  116. {
  117. return $this->engine->getError();
  118. }
  119. /**
  120. * 返回保存的文件信息
  121. * @return mixed
  122. */
  123. public function getSaveFileInfo()
  124. {
  125. return $this->engine->getSaveFileInfo();
  126. }
  127. /**
  128. * 获取当前的存储引擎
  129. * @param null|string $storage 指定存储方式,如不指定则为系统默认
  130. * @return mixed
  131. * @throws Exception
  132. */
  133. private function getEngineClass($storage = null)
  134. {
  135. $storage = is_null($storage) ? $this->config['default'] : $storage;
  136. if (!isset(self::ENGINE_CLASS_LIST[$storage])) {
  137. throw new Exception("未找到存储引擎类: {$storage}");
  138. }
  139. $class = self::ENGINE_CLASS_LIST[$storage];
  140. return new $class($storage, $this->config['engine'][$storage]);
  141. }
  142. }