|
@@ -8,13 +8,15 @@ class Qiniu
|
|
|
{
|
|
|
private $client;
|
|
|
private $token;
|
|
|
+ private $auth;
|
|
|
+ private $bucket;
|
|
|
|
|
|
public function token($config, $upload, $file = null)
|
|
|
{
|
|
|
$cur = time();
|
|
|
//if (!$config['token'] || ($config['token_endtime'] && $cur > $config['token_endtime'])) {
|
|
|
+ $this->auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
|
|
|
if (true) {
|
|
|
- $auth = new \Qiniu\Auth($config['appkey'], $config['appsecret']);
|
|
|
|
|
|
# 暂时没用到callback
|
|
|
$policy = array(
|
|
@@ -22,7 +24,7 @@ class Qiniu
|
|
|
'callbackBody' => 'filename=$(fname)&key=$(key)&filesize=$(fsize)&width=$(imageInfo.width)&height=$(imageInfo.height)'
|
|
|
);
|
|
|
|
|
|
- $token = $auth->uploadToken($upload['bucket'], $file, 3600);
|
|
|
+ $token = $this->auth->uploadToken($upload['bucket'], $file, 3600);
|
|
|
$up['token'] = $token;
|
|
|
$up['token_endtime'] = $cur + 3600 - 60;
|
|
|
$up['where_id'] = $config['id'];
|
|
@@ -50,17 +52,14 @@ class Qiniu
|
|
|
{
|
|
|
list($type, $token, $domain, $bucket) = $this->token($config, $upload, $file);
|
|
|
$this->token = $token;
|
|
|
- // 构建 UploadManager 对象
|
|
|
- $this->client = new \Qiniu\Storage\UploadManager();
|
|
|
+ $this->bucket = $bucket;
|
|
|
return $this;
|
|
|
}
|
|
|
|
|
|
# 下载文件
|
|
|
public function download($bucket, $file, $local = false)
|
|
|
{
|
|
|
- if (!$this->client) {
|
|
|
- return false;
|
|
|
- }
|
|
|
+ $this->client = new \Qiniu\Storage\UploadManager();
|
|
|
if ($local) {
|
|
|
$options = array(
|
|
|
OssClient::OSS_FILE_DOWNLOAD => $local
|
|
@@ -77,16 +76,23 @@ class Qiniu
|
|
|
# 上传文件
|
|
|
public function upload($file, $source_file, $options = array(), $base64 = false)
|
|
|
{
|
|
|
- if (!$this->client) {
|
|
|
- return array();
|
|
|
- }
|
|
|
- $method = 'putFile';
|
|
|
if ($base64) {
|
|
|
$method = 'put';
|
|
|
+ $this->client = new \Qiniu\Storage\UploadManager();
|
|
|
+ list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);
|
|
|
+ } elseif (strstr($source_file, 'http')) {
|
|
|
+ $method = 'fetch';
|
|
|
+ $this->client = new \Qiniu\Storage\BucketManager($this->auth);
|
|
|
+ $items = $this->client->$method($source_file, $this->bucket, $file);
|
|
|
+ if (isset($items[0])) {
|
|
|
+ $ret = $items[0];
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $method = 'putFile';
|
|
|
+ $this->client = new \Qiniu\Storage\UploadManager();
|
|
|
+ list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);
|
|
|
}
|
|
|
|
|
|
- list($ret, $err) = $this->client->$method($this->token, $file, $source_file, $options);
|
|
|
-
|
|
|
return $ret;
|
|
|
}
|
|
|
|