diff --git a/src/QiniuAdapter.php b/src/QiniuAdapter.php index 60dc049..273a53b 100644 --- a/src/QiniuAdapter.php +++ b/src/QiniuAdapter.php @@ -80,18 +80,18 @@ public function writeStream(string $path, $contents, Config $config): void public function read(string $path): string { - $result = file_get_contents($this->getUrl($path)); - if ($result === false) { + $result = file_get_contents($this->privateDownloadUrl($path)); + if (false === $result) { throw UnableToReadFile::fromLocation($path); } return $result; } - public function readStream(string $path) + public function readStream(string $path): string { - if (ini_get('allow_url_fopen')) { - if ($result = fopen($this->getUrl($path), 'r')) { + if (ini_get('allow_url_open')) { + if ($result = fopen($this->privateDownloadUrl($path), 'r')) { return $result; } } @@ -231,14 +231,10 @@ public function privateDownloadUrl(string $path, int $expires = 3600): string return $this->getAuthManager()->privateDownloadUrl($this->getUrl($path), $expires); } - public function refresh(string $path) + public function refresh(string|array $path): array { - if (is_string($path)) { - $path = [$path]; - } - // 将 $path 变成完整的 url - $urls = array_map([$this, 'getUrl'], $path); + $urls = array_map([$this, 'getUrl'], (array) $path); return $this->getCdnManager()->refreshUrls($urls); } diff --git a/tests/QiniuAdapterTest.php b/tests/QiniuAdapterTest.php index 10776cb..5a3d037 100644 --- a/tests/QiniuAdapterTest.php +++ b/tests/QiniuAdapterTest.php @@ -167,20 +167,23 @@ public function testHas($adapter, $managers) /** * @dataProvider qiniuProvider */ - public function testRead($adapter) + public function testRead($adapter, $managers) { + $managers['authManager']->expects()->privateDownloadUrl('http://domain.com/foo/file.md', 3600)->andReturn('http://domain.com/foo/file.md'); $this->assertSame( \Overtrue\Flysystem\Qiniu\file_get_contents('http://domain.com/foo/file.md'), $adapter->read('foo/file.md') ); // urlencode + $managers['authManager']->expects()->privateDownloadUrl('http://domain.com/foo/%E6%96%87%E4%BB%B6%E5%90%8D.md', 3600)->andReturn('http://domain.com/foo/%E6%96%87%E4%BB%B6%E5%90%8D.md'); $this->assertSame( \Overtrue\Flysystem\Qiniu\file_get_contents('http://domain.com/foo/%E6%96%87%E4%BB%B6%E5%90%8D.md'), $adapter->read('foo/文件名.md') ); // urlencode with query + $managers['authManager']->expects()->privateDownloadUrl('http://domain.com/foo/%E6%96%87%E4%BB%B6%E5%90%8D.md?info=yes&type=xxx', 3600)->andReturn('http://domain.com/foo/%E6%96%87%E4%BB%B6%E5%90%8D.md?info=yes&type=xxx'); $this->assertSame( \Overtrue\Flysystem\Qiniu\file_get_contents('http://domain.com/foo/%E6%96%87%E4%BB%B6%E5%90%8D.md?info=yes&type=xxx'), $adapter->read('foo/文件名.md?info=yes&type=xxx') @@ -190,15 +193,18 @@ public function testRead($adapter) /** * @dataProvider qiniuProvider */ - public function testReadStream($adapter) + public function testReadStream($adapter, $managers) { $GLOBALS['result_of_ini_get'] = true; + $managers['authManager']->expects()->privateDownloadUrl('http://domain.com/foo/file.md', 3600)->andReturn('http://domain.com/foo/file.md'); + $this->assertSame( \Overtrue\Flysystem\Qiniu\fopen('http://domain.com/foo/file.md', 'r'), $adapter->readStream('foo/file.md') ); + $managers['authManager']->expects()->privateDownloadUrl('http://domain.com/foo/%E6%96%87%E4%BB%B6%E5%90%8D.md', 3600)->andReturn('http://domain.com/foo/%E6%96%87%E4%BB%B6%E5%90%8D.md'); $this->assertSame( \Overtrue\Flysystem\Qiniu\fopen('http://domain.com/foo/%E6%96%87%E4%BB%B6%E5%90%8D.md', 'r'), $adapter->readStream('foo/文件名.md') @@ -283,9 +289,9 @@ public function testPrivateDownloadUrl($adapter, $managers) */ 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'])); + $managers['cdnManager']->expects()->refreshUrls(['http://domain.com/url'])->andReturn(['url']); + $this->assertSame(['url'], $adapter->refresh('url')); + $this->assertSame(['url'], $adapter->refresh(['url'])); } /**