UploadExtend.php 2.6 KB

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