From e8a3ed5ac4b2433f1cdf5d03ed179a9c9b6553ef Mon Sep 17 00:00:00 2001 From: billyct Date: Wed, 29 Nov 2017 10:27:14 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=90=B0=20=E5=A2=9E=E5=8A=A0=E4=B8=A4?= =?UTF-8?q?=E4=B8=AA=20plugin=EF=BC=9APrivateDownloadUrl=EF=BC=8CRefreshFi?= =?UTF-8?q?le?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. privateDownloadUrl: 获取私有文件的 download url 2. refresh:刷新文件缓存 --- src/Plugins/PrivateDownloadUrl.php | 24 ++++++++++++ src/Plugins/RefreshFile.php | 24 ++++++++++++ src/QiniuAdapter.php | 62 ++++++++++++++++++++++++++++++ tests/QiniuAdapterTest.php | 27 ++++++++++++- 4 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/Plugins/PrivateDownloadUrl.php create mode 100644 src/Plugins/RefreshFile.php diff --git a/src/Plugins/PrivateDownloadUrl.php b/src/Plugins/PrivateDownloadUrl.php new file mode 100644 index 0000000..6351c66 --- /dev/null +++ b/src/Plugins/PrivateDownloadUrl.php @@ -0,0 +1,24 @@ +filesystem->getAdapter(); + return $adapter->privateDownloadUrl($path, $expires); + } +} diff --git a/src/Plugins/RefreshFile.php b/src/Plugins/RefreshFile.php new file mode 100644 index 0000000..6962d99 --- /dev/null +++ b/src/Plugins/RefreshFile.php @@ -0,0 +1,24 @@ +filesystem->getAdapter(); + return $adapter->refresh($path); + } +} diff --git a/src/QiniuAdapter.php b/src/QiniuAdapter.php index 744d9bb..71dd3dd 100644 --- a/src/QiniuAdapter.php +++ b/src/QiniuAdapter.php @@ -13,6 +13,7 @@ use League\Flysystem\Adapter\Polyfill\NotSupportingVisibilityTrait; use League\Flysystem\Config; use Qiniu\Auth; +use Qiniu\Cdn\CdnManager; use Qiniu\Storage\BucketManager; use Qiniu\Storage\UploadManager; @@ -60,6 +61,11 @@ class QiniuAdapter extends AbstractAdapter */ protected $bucketManager; + /** + * @var \Qiniu\Cdn\CdnManager + */ + protected $cdnManager; + /** * QiniuAdapter constructor. * @@ -360,6 +366,42 @@ public function fetch($path, $url) return $response; } + + /** + * Get private file download url + * + * @param $path + * @param int $expires + * + * @return string + */ + public function privateDownloadUrl($path, $expires = 3600) + { + $url = $this->getUrl($path); + + return $this->getAuthManager()->privateDownloadUrl($url, $expires); + } + + + /** + * Refresh file cache + * + * @param $path + * + * @return array + */ + public function refresh($path) + { + if (is_string($path)) { + $path = [$path]; + } + + // 将 $path 变成完整的 url + $urls = array_map([$this, 'getUrl'], $path); + + return $this->getCdnManager()->refreshUrls($urls); + } + /** * Get the mime-type of a file. * @@ -426,6 +468,18 @@ public function setAuthManager(Auth $manager) return $this; } + /** + * @param CdnManager $manager + * + * @return $this + */ + public function setCdnManager(CdnManager $manager) + { + $this->cdnManager = $manager; + + return $this; + } + /** * @return \Qiniu\Storage\BucketManager */ @@ -450,6 +504,14 @@ public function getUploadManager() return $this->uploadManager ?: $this->uploadManager = new UploadManager(); } + /** + * @return \Qiniu\Cdn\CdnManager + */ + public function getCdnManager() + { + return $this->cdnManager ?: $this->cdnManager = new CdnManager($this->getAuthManager()); + } + /** * Get the upload token. * diff --git a/tests/QiniuAdapterTest.php b/tests/QiniuAdapterTest.php index 7fade52..94c574f 100644 --- a/tests/QiniuAdapterTest.php +++ b/tests/QiniuAdapterTest.php @@ -35,6 +35,7 @@ public function qiniuProvider() $authManager = \Mockery::mock('stdClass'); $bucketManager = \Mockery::mock('stdClass'); $uploadManager = \Mockery::mock('stdClass'); + $cdnManager = \Mockery::mock('stdClass'); $authManager->allows()->uploadToken('bucket')->andReturns('token'); @@ -42,10 +43,11 @@ public function qiniuProvider() 'getAuthManager' => $authManager, 'getUploadManager' => $uploadManager, 'getBucketManager' => $bucketManager, + 'getCdnManager' => $cdnManager, ]); return [ - [$adapter, compact('authManager', 'bucketManager', 'uploadManager')], + [$adapter, compact('authManager', 'bucketManager', 'uploadManager', 'cdnManager')], ]; } @@ -289,6 +291,29 @@ public function testGetSize($adapter) $this->assertSame('meta-data', $adapter->getSize('foo.md')); } + /** + * @dataProvider qiniuProvider + */ + public function testPrivateDownloadUrl($adapter, $managers) + { + $managers['authManager']->expects()->privateDownloadUrl('http://domain.com/url', 3600)->andReturn('url'); + $this->assertSame('url', $adapter->privateDownloadUrl('url')); + + $managers['authManager']->expects()->privateDownloadUrl('http://domain.com/url', 7200)->andReturn('url'); + $this->assertSame('url', $adapter->privateDownloadUrl('url', 7200)); + } + + + /** + * @dataProvider qiniuProvider + */ + public function testRefresh($adapter, $managers) + { + $managers['cdnManager']->expects()->refreshUrls(['http://domain.com/url'])->andReturn('url'); + $this->assertSame('url', $adapter->refresh('url')); + $this->assertSame('url', $adapter->refresh(['url'])); + } + /** * @dataProvider qiniuProvider */ From 727114900de3a6be6bae272f0af946e7b743ccd3 Mon Sep 17 00:00:00 2001 From: billyct Date: Wed, 29 Nov 2017 12:06:10 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=90=B0fix=20for=20styleci?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Plugins/PrivateDownloadUrl.php | 5 ----- src/Plugins/RefreshFile.php | 5 ----- src/QiniuAdapter.php | 6 +++--- tests/QiniuAdapterTest.php | 1 - 4 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/Plugins/PrivateDownloadUrl.php b/src/Plugins/PrivateDownloadUrl.php index 6351c66..63471de 100644 --- a/src/Plugins/PrivateDownloadUrl.php +++ b/src/Plugins/PrivateDownloadUrl.php @@ -4,11 +4,6 @@ use League\Flysystem\Plugin\AbstractPlugin; -/** - * Class PrivateDownloadUrl - * - * @package \Overtrue\Flysystem\Qiniu\Plugins - */ class PrivateDownloadUrl extends AbstractPlugin { public function getMethod() diff --git a/src/Plugins/RefreshFile.php b/src/Plugins/RefreshFile.php index 6962d99..14ea375 100644 --- a/src/Plugins/RefreshFile.php +++ b/src/Plugins/RefreshFile.php @@ -4,11 +4,6 @@ use League\Flysystem\Plugin\AbstractPlugin; -/** - * Class RefreshFile - * - * @package \Overtrue\Flysystem\Qiniu\Plugins - */ class RefreshFile extends AbstractPlugin { public function getMethod() diff --git a/src/QiniuAdapter.php b/src/QiniuAdapter.php index 71dd3dd..6842b12 100644 --- a/src/QiniuAdapter.php +++ b/src/QiniuAdapter.php @@ -133,7 +133,7 @@ public function writeStream($path, $resource, Config $config) $response = $this->write($path, $contents, $config); - if ($response === false) { + if (false === $response) { return $response; } @@ -368,7 +368,7 @@ public function fetch($path, $url) /** - * Get private file download url + * Get private file download url. * * @param $path * @param int $expires @@ -384,7 +384,7 @@ public function privateDownloadUrl($path, $expires = 3600) /** - * Refresh file cache + * Refresh file cache. * * @param $path * diff --git a/tests/QiniuAdapterTest.php b/tests/QiniuAdapterTest.php index 94c574f..096324c 100644 --- a/tests/QiniuAdapterTest.php +++ b/tests/QiniuAdapterTest.php @@ -303,7 +303,6 @@ public function testPrivateDownloadUrl($adapter, $managers) $this->assertSame('url', $adapter->privateDownloadUrl('url', 7200)); } - /** * @dataProvider qiniuProvider */ From 32462d5af6cfbe4c889984b93f44486c58db85dc Mon Sep 17 00:00:00 2001 From: billyct Date: Wed, 29 Nov 2017 12:40:18 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=90=B0=E4=BF=AE=E6=94=B9=E6=B3=A8?= =?UTF-8?q?=E9=87=8A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/QiniuAdapter.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/QiniuAdapter.php b/src/QiniuAdapter.php index 6842b12..5490804 100644 --- a/src/QiniuAdapter.php +++ b/src/QiniuAdapter.php @@ -366,12 +366,11 @@ public function fetch($path, $url) return $response; } - /** * Get private file download url. * - * @param $path - * @param int $expires + * @param string $path + * @param int $expires * * @return string */ @@ -382,11 +381,10 @@ public function privateDownloadUrl($path, $expires = 3600) return $this->getAuthManager()->privateDownloadUrl($url, $expires); } - /** * Refresh file cache. * - * @param $path + * @param string|array $path * * @return array */ From e8235667856ad956b8242f0e11b06e616411dc41 Mon Sep 17 00:00:00 2001 From: billyct Date: Wed, 29 Nov 2017 12:41:30 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=90=B0=20=E5=88=A0=E6=8E=89=E4=B8=8D?= =?UTF-8?q?=E5=BF=85=E8=A6=81=E7=9A=84=E5=8F=98=E9=87=8F=EF=BC=8C=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E4=B8=80=E8=A1=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Plugins/PrivateDownloadUrl.php | 3 +-- src/Plugins/RefreshFile.php | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Plugins/PrivateDownloadUrl.php b/src/Plugins/PrivateDownloadUrl.php index 63471de..81e10b2 100644 --- a/src/Plugins/PrivateDownloadUrl.php +++ b/src/Plugins/PrivateDownloadUrl.php @@ -13,7 +13,6 @@ public function getMethod() public function handle($path, $expires = 3600) { - $adapter = $this->filesystem->getAdapter(); - return $adapter->privateDownloadUrl($path, $expires); + return $this->filesystem->getAdapter()->privateDownloadUrl($path, $expires); } } diff --git a/src/Plugins/RefreshFile.php b/src/Plugins/RefreshFile.php index 14ea375..3c49787 100644 --- a/src/Plugins/RefreshFile.php +++ b/src/Plugins/RefreshFile.php @@ -13,7 +13,6 @@ public function getMethod() public function handle($path = []) { - $adapter = $this->filesystem->getAdapter(); - return $adapter->refresh($path); + return $this->filesystem->getAdapter()->refresh($path); } }