Storage.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +---------------------------------------------------------------------
  9. // | Author: Dean <zxxjjforever@163.com>
  10. // +----------------------------------------------------------------------
  11. namespace cmf\lib;
  12. class Storage
  13. {
  14. private $driver;
  15. /**
  16. * @var object 对象实例
  17. */
  18. protected static $instance;
  19. /**
  20. * 构造方法,用于构造存储实例
  21. * @param string $driver 要使用的存储驱动 Qiniu-七牛存储驱动
  22. * @param array $driverConfig
  23. * @throws \Exception
  24. */
  25. public function __construct($driver = null, $driverConfig = null)
  26. {
  27. if (empty($driver)) {
  28. $storageSetting = cmf_get_option('storage');
  29. if (empty($storageSetting)) {
  30. $driver = 'Local';
  31. $driverConfig = [];
  32. } else {
  33. $driver = isset($storageSetting['type']) ? $storageSetting['type'] : 'Local';
  34. if ($driver == 'Local') {
  35. $driverConfig = [];
  36. } else {
  37. $driverConfig = $storageSetting['storages'][$driver];
  38. }
  39. }
  40. }
  41. if (empty($driverConfig['driver'])) {
  42. $storageDriverClass = "\\cmf\\lib\\storage\\$driver";
  43. } else {
  44. $storageDriverClass = $driverConfig['driver'];
  45. }
  46. $storage = new $storageDriverClass($driverConfig);
  47. $this->driver = $storage;
  48. }
  49. /**
  50. * 文件上传
  51. * @param string $file 上传文件路径
  52. * @param string $filePath 文件路径相对于upload目录
  53. * @param string $fileType 文件类型,image,video,audio,file
  54. * @param array $param 额外参数
  55. * @return mixed
  56. */
  57. public function upload($file, $filePath, $fileType = 'image', $param = null)
  58. {
  59. return $this->driver->upload($file, $filePath, $fileType, $param);
  60. }
  61. /**
  62. * 初始化
  63. * @param $type
  64. * @param $config
  65. * @return \cmf\lib\Storage
  66. */
  67. public static function instance($type = null, $config = null)
  68. {
  69. if (is_null(self::$instance)) {
  70. self::$instance = new static($type, $config);
  71. }
  72. return self::$instance;
  73. }
  74. /**
  75. * 获取图片预览地址
  76. * @param string $file
  77. * @param string $style
  78. * @return mixed
  79. */
  80. public function getPreviewUrl($file, $style = '')
  81. {
  82. return $this->driver->getPreviewUrl($file, $style);
  83. }
  84. /**
  85. * 获取图片地址
  86. * @param string $file
  87. * @param string $style
  88. * @return mixed
  89. */
  90. public function getImageUrl($file, $style = '')
  91. {
  92. return $this->driver->getImageUrl($file, $style);
  93. }
  94. /**
  95. * 获取文件地址
  96. * @param string $file
  97. * @param string $style
  98. * @return mixed
  99. */
  100. public function getUrl($file, $style = '')
  101. {
  102. return $this->driver->getUrl($file, $style);
  103. }
  104. /**
  105. * 获取文件下载地址
  106. * @param string $file
  107. * @param int $expires
  108. * @return mixed
  109. */
  110. public function getFileDownloadUrl($file, $expires = 3600)
  111. {
  112. return $this->driver->getFileDownloadUrl($file, $expires);
  113. }
  114. /**
  115. * 获取云存储域名
  116. * @return mixed
  117. */
  118. public function getDomain()
  119. {
  120. return $this->driver->getDomain();
  121. }
  122. /**
  123. * 获取文件相对上传目录路径
  124. * @param string $url
  125. * @return mixed
  126. */
  127. public function getFilePath($url)
  128. {
  129. return $this->driver->getFilePath($url);
  130. }
  131. }