Skip to content

Commit

Permalink
update GetFederationToken
Browse files Browse the repository at this point in the history
  • Loading branch information
freyo committed Jan 7, 2019
1 parent 730b374 commit c3510ec
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -7,7 +7,8 @@
"league/flysystem": "~1.0",
"guzzlehttp/guzzle": "~6.0",
"qcloud/cos-sdk-v5": "^1.2.2",
"nesbot/carbon": "~1.0"
"nesbot/carbon": "~1.0",
"ext-json": "*"
},
"require-dev": {
"php": ">=5.5.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter.php
Expand Up @@ -71,7 +71,7 @@ public function getBucketWithAppId()
*/
public function getBucket()
{
return str_replace('-'.$this->getAppId(), '', $this->config['bucket']);
return $this->config['bucket'];
}

/**
Expand Down
27 changes: 23 additions & 4 deletions src/Plugins/GetFederationToken.php
Expand Up @@ -19,20 +19,39 @@ public function getMethod()
/**
* @param string $path
* @param int $seconds
* @param callable $customPolicy
* @param string $name
*
* @return bool|array
*/
public function handle($path = '', $seconds = 7200)
public function handle($path = '', $seconds = 7200, callable $customPolicy = null, $name = 'cos')
{
$policy = is_callable($customPolicy)
? $this->getCustomPolicy($customPolicy, $path)
: $this->getPolicy($path);

$params = [
'durationSeconds' => $seconds,
'name' => 'cos',
'policy' => urlencode($this->getPolicy($path))
'name' => $name,
'policy' => urlencode($policy)
];

return $this->request($params, 'GetFederationToken');
}

/**
* @param callable $callable
* @param $path
*
* @return string
*/
protected function getCustomPolicy(callable $callable, $path)
{
$policy = call_user_func($callable, $path, $this->getConfig());

return json_encode($policy, JSON_UNESCAPED_SLASHES);
}

/**
* @param $path
*
Expand Down Expand Up @@ -67,7 +86,7 @@ protected function getPolicy($path)
],
];

return str_replace('\\/', '/', json_encode($policy));
return json_encode($policy, JSON_UNESCAPED_SLASHES);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/ServiceProvider.php
Expand Up @@ -3,6 +3,7 @@
namespace Freyo\Flysystem\QcloudCOSv5;

use Freyo\Flysystem\QcloudCOSv5\Plugins\CDN;
use Freyo\Flysystem\QcloudCOSv5\Plugins\GetFederationToken;
use Freyo\Flysystem\QcloudCOSv5\Plugins\GetUrl;
use Freyo\Flysystem\QcloudCOSv5\Plugins\PutRemoteFile;
use Freyo\Flysystem\QcloudCOSv5\Plugins\PutRemoteFileAs;
Expand Down Expand Up @@ -38,6 +39,7 @@ public function boot()
$flysystem->addPlugin(new GetUrl());
$flysystem->addPlugin(new CDN());
$flysystem->addPlugin(new TCaptcha());
$flysystem->addPlugin(new GetFederationToken());

return $flysystem;
});
Expand Down

0 comments on commit c3510ec

Please sign in to comment.