Skip to content

Commit 3ce8bd9

Browse files
committed
Merge pull request #9 from qiniu/develop
Release v6.0.0
2 parents 38e0702 + e4107db commit 3ce8bd9

16 files changed

+550
-29
lines changed

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ php:
44
- 5.3
55
- 5.4
66
before_script:
7-
- export QINIU_ACCESS_KEY="iN7NgwM31j4-BZacMjPrOQBs34UG1maYCAQmhdCV"
8-
- export QINIU_SECRET_KEY="6QTOr2Jg1gcZEWDQXKOGZh5PziC2MCV5KsntT70j"
7+
- export QINIU_ACCESS_KEY="Vhiv6a22kVN_zhtetbPNeG9sY3JUL1HG597EmBwQ"
8+
- export QINIU_SECRET_KEY="b5b5vNg5nnkwkPfW5ayicPE_pj6hqgKMQEaWQ6JD"
9+
- export QINIU_BUCKET_NAME="phpsdk"
10+
- export QINIU_KEY_NAME="file_name"
911
script:
1012
- cd tests; phpunit .
1113

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## CHANGE LOG
2+
3+
### v6.0.0
4+
5+
2013-06-29 issue [#9](https://github.com/qiniu/api/pull/9)
6+
7+
- 遵循 [sdkspec v6.0.2](https://github.com/qiniu/sdkspec/tree/v6.0.2)
8+
- `Qiniu_Put/PutFile` 调整为基于 up.qiniu.com 的协议,extra *PutExtra 参数可以为 nil
9+
- `Qiniu_Put/PutFile` 支持支持 key = null (UNDEFINED_KEY),这样服务端将自动生成 key 并返回
10+
- `Qiniu_Put/PutFile` 支持自定义的 "x:" 参数(io.PutExtra.Params)、支持 Crc 检查
11+
- 待增加:rsf, batch, resumable io 的支持
12+
- bugfix: 修复 crc32 为负数的错误
13+
- 增加 `Qiniu_RS_Put/PutFile` 辅助函数,用于服务端上传

docs/README.md

Whitespace-only changes.

qiniu/auth_digest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public function SignWithData($data) // => $token
3131
public function SignRequest($req, $incbody) // => ($token, $error)
3232
{
3333
$url = $req->URL;
34-
$data = $url['path'];
34+
$url = parse_url($url['path']);
35+
$data = '';
36+
if (isset($url['path'])) {
37+
$data = $url['path'];
38+
}
3539
if (isset($url['query'])) {
3640
$data .= '?' . $url['query'];
3741
}
@@ -40,7 +44,6 @@ public function SignRequest($req, $incbody) // => ($token, $error)
4044
if ($incbody) {
4145
$data .= $req->Body;
4246
}
43-
4447
return $this->Sign($data);
4548
}
4649
}

qiniu/conf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
$QINIU_UP_HOST = 'http://up.qbox.me';
44
$QINIU_RS_HOST = 'http://rs.qbox.me';
5-
$QINIU_RSP_HOST = 'http://rsp.qbox.me';
5+
$QINIU_RSF_HOST = 'http://rsf.qbox.me';
66

77
$QINIU_ACCESS_KEY = '<Please apply your access key>';
88
$QINIU_SECRET_KEY = '<Dont send your secret key to anyone>';

qiniu/fop.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
require_once("auth_digest.php");
4+
5+
// --------------------------------------------------------------------------------
6+
// class Qiniu_ImageView
7+
8+
class Qiniu_ImageView {
9+
public $Mode;
10+
public $Width;
11+
public $Height;
12+
public $Quality;
13+
public $Format;
14+
15+
public function MakeRequest($url)
16+
{
17+
$ops = array($this->Mode);
18+
19+
if (!empty($this->Width)) {
20+
$ops[] = 'w/' . $this->Width;
21+
}
22+
if (!empty($this->Height)) {
23+
$ops[] = 'h/' . $this->Height;
24+
}
25+
if (!empty($this->Quality)) {
26+
$ops[] = 'q/' . $this->Quality;
27+
}
28+
if (!empty($this->Format)) {
29+
$ops[] = 'format/' . $this->Format;
30+
}
31+
32+
return $url . "?imageView/" . implode('/', $ops);
33+
}
34+
}
35+
36+
// --------------------------------------------------------------------------------
37+
// class Qiniu_Exif
38+
39+
class Qiniu_Exif {
40+
41+
public function MakeRequest($url)
42+
{
43+
return $url . "?exif";
44+
}
45+
46+
}
47+
48+
// --------------------------------------------------------------------------------
49+
// class Qiniu_ImageInfo
50+
51+
class Qiniu_ImageInfo {
52+
53+
public function MakeRequest($url)
54+
{
55+
return $url . "?imageInfo";
56+
}
57+
58+
}

qiniu/http.php

Lines changed: 85 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class Qiniu_Error
99
{
1010
public $Err; // string
11-
public $Reqid; // string
11+
public $Reqid; // string
1212
public $Details; // []string
1313
public $Code; // int
1414

@@ -48,7 +48,7 @@ class Qiniu_Response
4848

4949
public function __construct($code, $body)
5050
{
51-
$this->StatusCode = code;
51+
$this->StatusCode = $code;
5252
$this->Header = array();
5353
$this->Body = $body;
5454
$this->ContentLength = strlen($body);
@@ -62,7 +62,7 @@ function Qiniu_Header_Get($header, $key) // => $val
6262
{
6363
$val = @$header[$key];
6464
if (isset($val)) {
65-
if (isarray($val)) {
65+
if (is_array($val)) {
6666
return $val[0];
6767
}
6868
return $val;
@@ -117,10 +117,18 @@ function Qiniu_Client_do($req) // => ($resp, $error)
117117
CURLOPT_CUSTOMREQUEST => 'POST',
118118
CURLOPT_URL => $url['path']
119119
);
120+
$httpHeader = $req->Header;
121+
if (!empty($httpHeader))
122+
{
123+
$header = array();
124+
foreach($httpHeader as $key => $parsedUrlValue) {
125+
$header[] = "$key: $parsedUrlValue";
126+
}
127+
$options[CURLOPT_HTTPHEADER] = $header;
128+
}
120129
$body = $req->Body;
121130
if (!empty($body)) {
122131
$options[CURLOPT_POSTFIELDS] = $body;
123-
$options[CURLOPT_POSTFIELDSIZE] = strlen($body);
124132
}
125133
curl_setopt_array($ch, $options);
126134
$result = curl_exec($ch);
@@ -138,19 +146,27 @@ function Qiniu_Client_do($req) // => ($resp, $error)
138146
return array($resp, null);
139147
}
140148

141-
class Qiniu_Client
149+
class Qiniu_HttpClient
150+
{
151+
public function RoundTrip($req) // => ($resp, $error)
152+
{
153+
return Qiniu_Client_do($req);
154+
}
155+
}
156+
157+
class Qiniu_MacHttpClient
142158
{
143-
private $mac;
159+
public $Mac;
144160

145161
public function __construct($mac)
146162
{
147-
$this->mac = Qiniu_RequireMac($mac);
163+
$this->Mac = Qiniu_RequireMac($mac);
148164
}
149165

150-
public function Exec($req) // => ($resp, $error)
166+
public function RoundTrip($req) // => ($resp, $error)
151167
{
152168
$incbody = Qiniu_Client_incBody($req);
153-
$token = $this->mac->SignRequest($req, $incbody);
169+
$token = $this->Mac->SignRequest($req, $incbody);
154170
$req->Header['Authorization'] = "QBox $token";
155171
return Qiniu_Client_do($req);
156172
}
@@ -161,6 +177,7 @@ public function Exec($req) // => ($resp, $error)
161177
function Qiniu_Client_ret($resp) // => ($data, $error)
162178
{
163179
$code = $resp->StatusCode;
180+
$data = null;
164181
if ($code >= 200 && $code <= 299) {
165182
if ($resp->ContentLength !== 0) {
166183
$data = json_decode($resp->Body, true);
@@ -180,7 +197,7 @@ function Qiniu_Client_Call($self, $url) // => ($data, $error)
180197
{
181198
$u = array('path' => $url);
182199
$req = new Qiniu_Request($u, null);
183-
list($resp, $err) = $self->Exec($req);
200+
list($resp, $err) = $self->RoundTrip($req);
184201
if ($err !== null) {
185202
return array(null, $err);
186203
}
@@ -191,7 +208,7 @@ function Qiniu_Client_CallNoRet($self, $url) // => $error
191208
{
192209
$u = array('path' => $url);
193210
$req = new Qiniu_Request($u, null);
194-
list($resp, $err) = $self->Exec($req);
211+
list($resp, $err) = $self->RoundTrip($req);
195212
if ($err !== null) {
196213
return array(null, $err);
197214
}
@@ -201,5 +218,62 @@ function Qiniu_Client_CallNoRet($self, $url) // => $error
201218
return Qiniu_ResponseError($resp);
202219
}
203220

221+
function Qiniu_Client_CallWithForm(
222+
$self, $url, $params, $contentType = 'application/x-www-form-urlencoded') // => ($data, $error)
223+
{
224+
$u = array('path' => $url);
225+
if ($contentType === 'application/x-www-form-urlencoded') {
226+
if (is_array($params)) {
227+
$params = http_build_query($params);
228+
}
229+
}
230+
$req = new Qiniu_Request($u, $params);
231+
if ($contentType !== 'multipart/form-data') {
232+
$req->Header['Content-Type'] = $contentType;
233+
}
234+
list($resp, $err) = $self->RoundTrip($req);
235+
if ($err !== null) {
236+
return array(null, $err);
237+
}
238+
return Qiniu_Client_ret($resp);
239+
}
240+
241+
// --------------------------------------------------------------------------------
242+
243+
function Qiniu_Client_CallWithMultipartForm($self, $url, $fields, $files)
244+
{
245+
list($contentType, $body) = Qiniu_Build_MultipartForm($fields, $files);
246+
return Qiniu_Client_CallWithForm($self, $url, $body, $contentType);
247+
}
248+
249+
function Qiniu_Build_MultipartForm($fields, $files) // => ($contentType, $body)
250+
{
251+
$data = array();
252+
$mimeBoundary = md5(microtime());
253+
254+
foreach ($fields as $name => $val){
255+
array_push($data, '--' . $mimeBoundary);
256+
array_push($data, "Content-Disposition: form-data; name=$name");
257+
array_push($data, '');
258+
array_push($data, $val);
259+
}
260+
261+
foreach ($files as $file) {
262+
array_push($data, '--' . $mimeBoundary);
263+
list($name, $fileName, $fileCxt) = $file;
264+
array_push($data, "Content-Disposition: form-data; name=$name; filename=$fileName");
265+
array_push($data, 'Content-Type: application/octet-stream');
266+
array_push($data, '');
267+
array_push($data, $fileCxt);
268+
}
269+
270+
array_push($data, '--' . $mimeBoundary);
271+
array_push($data, '');
272+
273+
$body = implode("\r\n", $data);
274+
$contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
275+
return array($contentType, $body);
276+
}
277+
204278
// --------------------------------------------------------------------------------
205279

qiniu/io.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
require_once("http.php");
4+
require_once("auth_digest.php");
5+
6+
// ----------------------------------------------------------
7+
// class Qiniu_PutExtra
8+
9+
class Qiniu_PutExtra
10+
{
11+
public $Params = null;
12+
public $MimeType = null;
13+
public $Crc32 = 0;
14+
public $CheckCrc = 0;
15+
}
16+
17+
function Qiniu_Put($upToken, $key, $body, $putExtra) // => ($data, $err)
18+
{
19+
global $QINIU_UP_HOST;
20+
21+
if ($putExtra === null) {
22+
$putExtra = new Qiniu_PutExtra;
23+
}
24+
25+
$fields = array('token' => $upToken);
26+
if ($key === null) {
27+
$fname = '?';
28+
} else {
29+
$fname = $key;
30+
$fields['key'] = $key;
31+
}
32+
if ($putExtra->CheckCrc) {
33+
$fields['crc32'] = $putExtra->Crc32;
34+
}
35+
36+
$files = array(array('file', $fname, $body));
37+
38+
$client = new Qiniu_HttpClient;
39+
return Qiniu_Client_CallWithMultipartForm($client, $QINIU_UP_HOST, $fields, $files);
40+
}
41+
42+
function Qiniu_PutFile($upToken, $key, $localFile, $putExtra) // => ($data, $err)
43+
{
44+
global $QINIU_UP_HOST;
45+
46+
if ($putExtra === null) {
47+
$putExtra = new Qiniu_PutExtra;
48+
}
49+
50+
$fields = array('token' => $upToken, 'file' => '@' . $localFile);
51+
if ($key === null) {
52+
$fname = '?';
53+
} else {
54+
$fname = $key;
55+
$fields['key'] = $key;
56+
}
57+
if ($putExtra->CheckCrc) {
58+
if ($putExtra->CheckCrc === 1) {
59+
$hash = hash_file('crc32b', $localFile);
60+
$array = unpack('N', pack('H*', $hash));
61+
$putExtra->Crc32 = $array[1];
62+
}
63+
$fields['crc32'] = sprintf('%u', $putExtra->Crc32);
64+
}
65+
66+
$client = new Qiniu_HttpClient;
67+
return Qiniu_Client_CallWithForm($client, $QINIU_UP_HOST, $fields, 'multipart/form-data');
68+
}
69+

0 commit comments

Comments
 (0)