Driver.php 2.5 KB

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