Skip to content

Commit

Permalink
Fixed #53
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed May 22, 2022
1 parent 44c35f1 commit eb4f850
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
18 changes: 7 additions & 11 deletions src/QiniuAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
Expand Down
16 changes: 11 additions & 5 deletions tests/QiniuAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down Expand Up @@ -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']));
}

/**
Expand Down

0 comments on commit eb4f850

Please sign in to comment.