Skip to content

Commit

Permalink
Cleanup and Fixed version bugs. overtrue/laravel-filesystem-qiniu#2,o…
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Aug 25, 2017
1 parent 7450e6f commit f01f750
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 15 deletions.
32 changes: 29 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ $adapter = new QiniuAdapter($accessKey, $secretKey, $bucket, $domain);

$flysystem = new League\Flysystem\Filesystem($adapter);

# if you want to use fetch method
$flysystem->addPlugin(new FetchFile());

```

## API
Expand Down Expand Up @@ -79,8 +76,37 @@ string $flysystem->getMimetype('file.md');

int $flysystem->getTimestamp('file.md');

```

### Plugins

File Url:

```php
use Overtrue\Flysystem\Qiniu\Plugins\FileUrl;

$flysystem->addPlugin(new FileUrl());

string $flysystem->getUrl('file.md');
```

Fetch file:

```php
use Overtrue\Flysystem\Qiniu\Plugins\FetchFile;

$flysystem->addPlugin(new FetchFile());

bool $flysystem->fetch('file.md', 'http://httpbin.org/robots.txt');
```

Upload Token:

```php
use Overtrue\Flysystem\Qiniu\Plugins\UploadToken;
$flysystem->addPlugin(new UploadToken());

string $flysystem->getUploadToken('file.md', 3600);
```

# Integration
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"require": {
"php": ">=5.5.9",
"league/flysystem": "^1.0",
"qiniu/php-sdk": "^7.1"
"qiniu/php-sdk": "^7.2"
},
"require-dev": {
"php": ">=5.6.0",
Expand Down
25 changes: 25 additions & 0 deletions src/Plugins/FileUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the overtrue/flysystem-qiniu.
* (c) overtrue <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\Flysystem\Qiniu\Plugins;

use League\Flysystem\Plugin\AbstractPlugin;

class FileUrl extends AbstractPlugin
{
public function getMethod()
{
return 'getUrl';
}

public function handle($path)
{
return $this->filesystem->getAdapter()->getUrl($path);
}
}
25 changes: 25 additions & 0 deletions src/Plugins/UploadToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the overtrue/flysystem-qiniu.
* (c) overtrue <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Overtrue\Flysystem\Qiniu\Plugins;

use League\Flysystem\Plugin\AbstractPlugin;

class UploadToken extends AbstractPlugin
{
public function getMethod()
{
return 'getUploadToken';
}

public function handle($key = null, $expires = 3600, $policy = null, $strictPolice = null)
{
return $this->filesystem->getAdapter()->getUploadToken($key, $expires, $policy, $strictPolice);
}
}
9 changes: 5 additions & 4 deletions src/QiniuAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public function listContents($directory = '', $recursive = false)

$result = $this->getBucketManager()->listFiles($this->bucket, $directory);

foreach ($result[0] as $files) {
foreach (isset($result[0]['items']) ? $result[0]['items'] : [] as $files) {
$list[] = $this->normalizeFileInfo($files);
}

Expand Down Expand Up @@ -455,13 +455,14 @@ public function getUploadManager()
*
* @param string|null $key
* @param int $expires
* @param bool $policy
* @param string|null $policy
* @param string|null $strictPolice
*
* @return string
*/
public function getUploadToken($key = null, $expires = 3600, $policy = true)
public function getUploadToken($key = null, $expires = 3600, $policy = null, $strictPolice = null)
{
return $this->getAuthManager()->uploadToken($this->bucket, $key, $expires, $policy);
return $this->getAuthManager()->uploadToken($this->bucket, $key, $expires, $policy, $strictPolice);
}

/**
Expand Down
17 changes: 10 additions & 7 deletions tests/QiniuAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public function testReadStream($adapter)
public function testListContents($adapter, $managers)
{
$managers['bucketManager']->expects()->listFiles('bucket', 'path/to/list')
->andReturns([[
->andReturn([['items' => [
[
'key' => 'foo.md',
'putTime' => 123 * 10000000,
Expand All @@ -238,7 +238,7 @@ public function testListContents($adapter, $managers)
'putTime' => 124 * 10000000,
'fsize' => 124,
],
]])
]]])
->twice();

$this->assertSame([
Expand Down Expand Up @@ -294,17 +294,20 @@ public function testGetSize($adapter)
*/
public function testGetUploadToken($adapter, $managers)
{
$managers['authManager']->expects()->uploadToken('bucket', null, 3600, true)->andReturn('token');
$managers['authManager']->expects()->uploadToken('bucket', null, 3600, null, null)->andReturn('token');
$this->assertSame('token', $adapter->getUploadToken());

$managers['authManager']->expects()->uploadToken('bucket', 'key', 3600, true)->andReturn('token');
$managers['authManager']->expects()->uploadToken('bucket', 'key', 3600, null, null)->andReturn('token');
$this->assertSame('token', $adapter->getUploadToken('key'));

$managers['authManager']->expects()->uploadToken('bucket', 'key', 7200, true)->andReturn('token');
$managers['authManager']->expects()->uploadToken('bucket', 'key', 7200, null, null)->andReturn('token');
$this->assertSame('token', $adapter->getUploadToken('key', 7200));

$managers['authManager']->expects()->uploadToken('bucket', 'key', 7200, false)->andReturn('token');
$this->assertSame('token', $adapter->getUploadToken('key', 7200, false));
$managers['authManager']->expects()->uploadToken('bucket', 'key', 7200, 'foo', null)->andReturn('token');
$this->assertSame('token', $adapter->getUploadToken('key', 7200, 'foo'));

$managers['authManager']->expects()->uploadToken('bucket', 'key', 7200, 'foo', 'bar')->andReturn('token');
$this->assertSame('token', $adapter->getUploadToken('key', 7200, 'foo', 'bar'));
}

/**
Expand Down

0 comments on commit f01f750

Please sign in to comment.