UploadService.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\extra\service\basic;
  3. use app\model\system\SystemConfig;
  4. use Tinywan\Storage\Adapter\CosAdapter;
  5. use Tinywan\Storage\Adapter\LocalAdapter;
  6. use Tinywan\Storage\Adapter\QiniuAdapter;
  7. class UploadService
  8. {
  9. /**
  10. * 配置
  11. * @return array
  12. */
  13. public function setConfigVal(string $type = ""): array
  14. {
  15. $data = (new SystemConfig)->where("type","storage")->column("value","name");
  16. $config = [];
  17. $config['storage']['default'] = $data['type'];
  18. $config['storage']['include'] = !empty($data['allow_exts']) ? explode(",",$data['allow_exts']) : [];
  19. $config['storage']['exclude'] = !empty($data['noallow_exts']) ? explode(",",$data['noallow_exts']) : [];
  20. $config['storage']['nums'] = 10;
  21. $config['storage']['single_limit'] = 1024 * 1024 * 200;
  22. $config['storage']['total_limit'] = 1024 * 1024 * 200;
  23. if (empty($type)) $type = $data['type'];
  24. switch ($type)
  25. {
  26. case "local":
  27. $config['storage']['local'] = [
  28. 'adapter' => LocalAdapter::class,
  29. 'root' => public_path().'/uploads/storage/',
  30. 'dirname' => function () {
  31. return date('Ymd');
  32. },
  33. 'domain' => "",
  34. 'uri' => '/uploads/storage/', // 如果 domain + uri 不在 public 目录下,请做好软链接,否则生成的url无法访问
  35. 'algo' => 'sha1',
  36. ];
  37. break;
  38. case "qiniu":
  39. $config['storage']['qiniu'] = [
  40. 'adapter' => QiniuAdapter::class,
  41. 'accessKey' => $data['qiniu_access_key'],
  42. 'secretKey' => $data['qiniu_secret_key'],
  43. 'bucket' => $data['qiniu_bucket'],
  44. 'dirname' => 'storage/'.date("Ymd"),
  45. 'domain' => $this->setDomain($data['oss_http_protocol'],$data['qiniu_http_domain']),
  46. ];
  47. break;
  48. case "oss":
  49. $config['storage']['oss'] = [
  50. 'adapter' => \Tinywan\Storage\Adapter\OssAdapter::class,
  51. 'accessKeyId' => $data['oss_access_key'],
  52. 'accessKeySecret' => $data['oss_secret_key'],
  53. 'bucket' => $data['oss_bucket'],
  54. 'dirname' => function () {
  55. return 'storage/'.date("Ymd");
  56. },
  57. 'domain' => $this->setDomain($data['oss_http_protocol'],$data['oss_http_domain']),
  58. 'endpoint' => $data['oss_region'],
  59. 'algo' => 'sha1',
  60. ];
  61. break;
  62. case "cos":
  63. $config['storage']['cos'] = [
  64. 'adapter' => CosAdapter::class,
  65. 'secretId' => $data['cos_access_key'],
  66. 'secretKey' => $data['cos_secret_key'],
  67. 'bucket' => $data['cos_bucket'],
  68. 'dirname' => 'storage/'.date("Ymd"),
  69. 'domain' => $this->setDomain($data['oss_http_protocol'],$data['cos_http_domain']),
  70. 'region' => $data['cos_region'],
  71. ];
  72. break;
  73. default:
  74. break;
  75. }
  76. return $config;
  77. }
  78. /**
  79. * 协议域名
  80. * @param string $type
  81. * @param string $domain
  82. * @return string
  83. */
  84. protected function setDomain(string $type,string $domain): string
  85. {
  86. if ($type == "auto") {
  87. return "//".$domain;
  88. }
  89. return $type."://".$domain;
  90. }
  91. }