Driver.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\common\service\storage;
  3. use app\common\service\ConfigService;
  4. use think\Exception;
  5. /**
  6. * 存储模块驱动
  7. * Class driver
  8. * @package app\common\library\storage
  9. */
  10. class Driver
  11. {
  12. private $config; // upload 配置
  13. private $engine; // 当前存储引擎类
  14. /**
  15. * 构造方法
  16. * Driver constructor.
  17. * @param $config
  18. * @param null|string $storage 指定存储方式,如不指定则为系统默认
  19. * @throws Exception
  20. */
  21. public function __construct($config, $storage = null)
  22. {
  23. $this->config = $config;
  24. $this->engine = $this->getEngineClass($storage);
  25. }
  26. /**
  27. * 设置上传的文件信息
  28. * @param string $name
  29. * @return mixed
  30. */
  31. public function setUploadFile($name = 'iFile')
  32. {
  33. return $this->engine->setUploadFile($name);
  34. }
  35. /**
  36. * 设置上传的文件信息
  37. * @param string $filePath
  38. * @return mixed
  39. */
  40. public function setUploadFileByReal($filePath)
  41. {
  42. return $this->engine->setUploadFileByReal($filePath);
  43. }
  44. /**
  45. * 执行文件上传
  46. * @param $save_dir (保存路径)
  47. * @return mixed
  48. */
  49. public function upload($save_dir)
  50. {
  51. return $this->engine->upload($save_dir);
  52. }
  53. /**
  54. * Notes: 抓取网络资源
  55. * @param $url
  56. * @param $key
  57. * @return mixed
  58. * @author 张无忌(2021/3/2 14:16)
  59. */
  60. public function fetch($url, $key)
  61. {
  62. return $this->engine->fetch($url, $key);
  63. }
  64. /**
  65. * 执行文件删除
  66. * @param $fileName
  67. * @return mixed
  68. */
  69. public function delete($fileName)
  70. {
  71. return $this->engine->delete($fileName);
  72. }
  73. /**
  74. * 获取错误信息
  75. * @return mixed
  76. */
  77. public function getError()
  78. {
  79. return $this->engine->getError();
  80. }
  81. /**
  82. * 获取文件路径
  83. * @return mixed
  84. */
  85. public function getFileName()
  86. {
  87. return $this->engine->getFileName();
  88. }
  89. /**
  90. * 返回文件信息
  91. * @return mixed
  92. */
  93. public function getFileInfo()
  94. {
  95. return $this->engine->getFileInfo();
  96. }
  97. /**
  98. * 获取当前的存储引擎
  99. * @param null|string $storage 指定存储方式,如不指定则为系统默认
  100. * @return mixed
  101. * @throws Exception
  102. */
  103. private function getEngineClass($storage = null)
  104. {
  105. $engineName = is_null($storage) ? $this->config['default'] : $storage;
  106. $classSpace = __NAMESPACE__ . '\\engine\\' . ucfirst($engineName);
  107. if (!class_exists($classSpace)) {
  108. throw new Exception('未找到存储引擎类: ' . $engineName);
  109. }
  110. if ($engineName == 'local') {
  111. return new $classSpace();
  112. }
  113. return new $classSpace(ConfigService::get('storage', $engineName));
  114. }
  115. }