UploadExtend.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\extra\tools;
  3. use app\service\system\UploadService;
  4. class UploadExtend
  5. {
  6. /**
  7. * 本地对象存储.
  8. */
  9. public const MODE_LOCAL = 'local';
  10. /**
  11. * 阿里云对象存储.
  12. */
  13. public const MODE_OSS = 'oss';
  14. /**
  15. * 腾讯云对象存储.
  16. */
  17. public const MODE_COS = 'cos';
  18. /**
  19. * 七牛云对象存储.
  20. */
  21. public const MODE_QINIU = 'qiniu';
  22. /**
  23. * S3对象存储.
  24. */
  25. public const MODE_S3 = 's3';
  26. protected array $config = [];
  27. /**
  28. * Support Storage
  29. */
  30. static $allowStorage = [
  31. self::MODE_LOCAL,
  32. self::MODE_OSS,
  33. self::MODE_COS,
  34. self::MODE_QINIU,
  35. self::MODE_S3
  36. ];
  37. /**
  38. * 存储磁盘
  39. * @param string|null $name
  40. * @param bool $_is_file_upload
  41. * @return mixed
  42. * @author Tinywan(ShaoBo Wan)
  43. */
  44. public static function disk(string $name = null, bool $_is_file_upload = true)
  45. {
  46. $storage = $name ?? self::getDefaultStorage();
  47. $config = self::getConfig($storage);
  48. return new $config['adapter'](array_merge(
  49. $config, ['_is_file_upload' => $_is_file_upload]
  50. ));
  51. }
  52. /**
  53. * 默认存储
  54. * @return mixed
  55. * @author Tinywan(ShaoBo Wan)
  56. */
  57. public static function getDefaultStorage()
  58. {
  59. return self::getConfig('default');
  60. }
  61. /**
  62. * 获取存储配置
  63. * @param string|null $name 名称
  64. * @return mixed
  65. * @author Tinywan(ShaoBo Wan)
  66. */
  67. public static function getConfig(string $name = null)
  68. {
  69. $config = (new UploadService)->setConfigVal($name);
  70. if (!is_null($name)) {
  71. return $config['storage'][$name];
  72. }
  73. return $config['storage']["default"];
  74. }
  75. /**
  76. * 配置信息
  77. * @param array $config
  78. */
  79. public function config(array $config)
  80. {
  81. $this->$config = $config;
  82. return $this;
  83. }
  84. /**
  85. * @param $name
  86. * @param $arguments
  87. * @return mixed
  88. * @author Tinywan(ShaoBo Wan)
  89. */
  90. public static function __callStatic($name, $arguments)
  91. {
  92. return static::disk()->{$name}(...$arguments);
  93. }
  94. }