DriveInterface.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace common\components\uploaddrive;
  3. use common\helpers\RegularHelper;
  4. use common\models\common\Attachment;
  5. use League\Flysystem\Filesystem;
  6. use Overtrue\Flysystem\Cos\CosAdapter;
  7. use Overtrue\Flysystem\Qiniu\QiniuAdapter;
  8. use Xxtime\Flysystem\Aliyun\OssAdapter;
  9. /**
  10. * Interface DriveInterface
  11. * @package common\components\uploaddrive
  12. */
  13. abstract class DriveInterface
  14. {
  15. /**
  16. * @var array
  17. */
  18. protected $config;
  19. /**
  20. * @var CosAdapter|OssAdapter|QiniuAdapter
  21. */
  22. protected $adapter;
  23. /**
  24. * 上传组件
  25. *
  26. * @var Filesystem
  27. */
  28. protected $filesystem;
  29. /**
  30. * DriveInterface constructor.
  31. * @param $config
  32. */
  33. public function __construct($config)
  34. {
  35. $this->config = $config;
  36. $this->create();
  37. }
  38. /**
  39. * @return Filesystem
  40. */
  41. public function entity(): Filesystem
  42. {
  43. if (!$this->filesystem instanceof Filesystem) {
  44. $this->filesystem = new Filesystem($this->adapter);
  45. }
  46. return $this->filesystem;
  47. }
  48. /**
  49. * @param $baseInfo
  50. * @param $drive
  51. * @param $fullPath
  52. * @return mixed
  53. */
  54. public function getUrl($baseInfo, $drive, $fullPath)
  55. {
  56. $baseInfo = $this->baseUrl($baseInfo, $fullPath);
  57. if ($drive != Attachment::DRIVE_LOCAL && !RegularHelper::verify('url', $baseInfo['url'])) {
  58. $baseInfo['url'] = 'http://' . $baseInfo['url'];
  59. }
  60. return $baseInfo;
  61. }
  62. /**
  63. * @param $baseInfo
  64. * @param $drive
  65. * @param $fullPath
  66. * @return mixed
  67. */
  68. public function getDomainName($baseInfo, $drive, $fullPath)
  69. {
  70. $baseInfo = $this->domainName($baseInfo, $fullPath);
  71. if ($drive != Attachment::DRIVE_LOCAL && !RegularHelper::verify('url', $baseInfo['domain_name'])) {
  72. $baseInfo['domain_name'] = 'http://' . $baseInfo['domain_name'];
  73. }
  74. return $baseInfo;
  75. }
  76. /**
  77. * 返回路由
  78. *
  79. * @param $baseInfo
  80. * @param $fullPath
  81. * @return $baseInfo
  82. */
  83. abstract protected function baseUrl($baseInfo, $fullPath);
  84. /**
  85. * 返回域名
  86. *
  87. * @param $baseInfo
  88. * @param $fullPath
  89. * @return $baseInfo
  90. */
  91. abstract protected function domainName($baseInfo, $fullPath);
  92. /**
  93. * @return mixed
  94. */
  95. abstract protected function create();
  96. }