Skip to content

Commit bd42f1d

Browse files
committed
add upload action
1 parent f153fac commit bd42f1d

File tree

1 file changed

+95
-13
lines changed

1 file changed

+95
-13
lines changed

sdk.php

Lines changed: 95 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class SDK implements ArrayAccess {
88
//删除
99
//复制 x
1010
//移动 x
11-
//上传 x
11+
//上传
1212
protected $access_token ;
1313
protected $secret_token ;
1414
protected $bucket;
@@ -30,15 +30,18 @@ public function __construct($access_token, $secret_token, $bucket = null)
3030
$this->bucket = $bucket;
3131
}
3232

33+
//获取空间名称
3334
public function getBucket()
3435
{
3536
return $this->bucket;
3637
}
3738

39+
//设置空间
3840
public function setBucket($bucket)
3941
{
4042
$this->bucket = $bucket;
4143
}
44+
4245
/**
4346
* 查看指定文件信息。
4447
* @param string $key 文件名或者目录+文件名
@@ -52,9 +55,16 @@ public function stat($key)
5255
die('error');
5356
}
5457
$url = self::QINIU_RS_HOST .'/stat/' . $this->encode("$bucket:$key");
55-
return $this->get($url);
58+
$token = $this->accessToken($url);
59+
$options[CURLOPT_HTTPHEADER] = array('Authorization: QBox '. $token);
60+
return $this->get($url, $options);
5661
}
5762

63+
/**
64+
* 删除指定文件信息。
65+
* @param string $key 文件名或者目录+文件名
66+
* @return NULL
67+
*/
5868
public function delete($key)
5969
{
6070
list($bucket, $key) = $this->parseKey($key);
@@ -64,7 +74,45 @@ public function delete($key)
6474
}
6575
$url = self::QINIU_RS_HOST .'/delete/' . $this->encode("$bucket:$key");
6676

67-
return $this->get($url);
77+
$token = $this->accessToken($url);
78+
$options[CURLOPT_HTTPHEADER] = array('Authorization: QBox '. $token);
79+
return $this->get($url, $options);
80+
}
81+
82+
public function upload($file, $name=null, $token = null)
83+
{
84+
if ( NULL === $token )
85+
{
86+
$token = $this->uploadToken($this->bucket);
87+
}
88+
89+
if ( !file_exists($file) )
90+
{
91+
die('文件不存在,构建一个临时文件');
92+
}
93+
$hash = hash_file('crc32b', $file);
94+
$array = unpack('N', pack('H*', $hash));
95+
96+
$postFields = array(
97+
'token' => $token,
98+
'file' => '@'.$file,
99+
'key' => $name,
100+
'crc32' => sprintf('%u', $array[1]),
101+
);
102+
103+
//未指定文件名,使用七牛默认的随机文件名
104+
if ( NULL === $name )
105+
{
106+
unset($postFields['key']);
107+
}
108+
else
109+
{
110+
//设置文件名后缀。
111+
}
112+
$options = array(
113+
CURLOPT_POSTFIELDS => $postFields,
114+
);
115+
return $this->get(self::QINIU_UP_HOST, $options);
68116
}
69117

70118
protected function parseKey($key)
@@ -92,6 +140,49 @@ public function getAlias($key)
92140
return isset($this->aliases[$key]) ? $this->aliases[$key] : $key;
93141
}
94142

143+
public function uploadToken($config = array())
144+
{
145+
if ( is_string($config) )
146+
{
147+
$scope = $config;
148+
$config = array();
149+
}
150+
else
151+
{
152+
$scope = $config['scope'];
153+
}
154+
$config['scope'] = $scope;
155+
//硬编码,需修改。
156+
$config['deadline'] = time() + 3600;
157+
foreach ( $this->activeUploadSettings($config) as $key => $value )
158+
{
159+
if ( $value )
160+
{
161+
$config[$key] = $value;
162+
}
163+
}
164+
165+
//build token
166+
$body = json_encode($config);
167+
$body = $this->encode($body);
168+
$sign = hash_hmac('sha1', $body, $this->secret_token, true);
169+
return $this->access_token . ':' . $this->encode($sign) . ':' .$body;
170+
}
171+
172+
public function uploadSettings()
173+
{
174+
return array(
175+
'scope','deadline','callbackUrl', 'callbackBody', 'returnUrl',
176+
'returnBody', 'asyncOps', 'endUser', 'exclusive', 'detectMime',
177+
'fsizeLimit', 'saveKey', 'persistentOps', 'persistentNotifyUrl'
178+
);
179+
}
180+
181+
protected function activeUploadSettings($array)
182+
{
183+
return array_intersect_key($array, array_flip($this->uploadSettings()));
184+
}
185+
95186
public function accessToken($url, $body = false)
96187
{
97188
$url = parse_url($url);
@@ -113,8 +204,6 @@ public function accessToken($url, $body = false)
113204
public function get($url, $options = array())
114205
{
115206
$this->ch = curl_init();
116-
$token = $this->accessToken($url);
117-
$options[CURLOPT_HTTPHEADER] = array('Authorization: QBox '. $token);
118207
$this->options[CURLOPT_URL] = $url;
119208
$this->options = $options + $this->options;
120209
//临时处理逻辑
@@ -220,11 +309,4 @@ public function offsetUnset($key)
220309
{
221310
return $this->delete();
222311
}
223-
}
224-
225-
//demo
226-
$accessKey = '';
227-
$secretKey = '';
228-
$bucket = "phpsdk";
229-
$sdk = new SDK($accessKey, $secretKey, $bucket);
230-
$test = $sdk['test.jpg']; // $sdk->stat('test.jpg'); or $sdk->stat('your bucket|your file name');
312+
}

0 commit comments

Comments
 (0)