Skip to content

Commit f7c6206

Browse files
committed
add io
1 parent e6aab6e commit f7c6206

File tree

8 files changed

+231
-6
lines changed

8 files changed

+231
-6
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ 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"
99
script:
1010
- cd tests; phpunit .
1111

qiniu/auth_digest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ public function SignRequest($req, $incbody) // => ($token, $error)
3232
{
3333
$url = $req->URL;
3434
$url = parse_url($url['path']);
35-
$data = $url['path'];
35+
$data = '';
36+
if (isset($url['path'])) {
37+
$data = $url['path'];
38+
}
3639
if (isset($url['query'])) {
3740
$data .= '?' . $url['query'];
3841
}

qiniu/http.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,20 +211,62 @@ function Qiniu_Client_CallNoRet($self, $url) // => $error
211211
return Qiniu_ResponseError($resp);
212212
}
213213

214-
function Qiniu_Client_CallWithForm($self, $url, $params) // => ($data, $error)
214+
function Qiniu_Client_CallWithForm($self, $url, $params, $contentType = 'application/x-www-form-urlencoded') // => ($data, $error)
215215
{
216216
$u = array('path' => $url);
217-
if (is_array($params)) {
217+
if ($contentType === 'application/x-www-form-urlencoded' && is_array($params)) {
218218
$params = http_build_query($params);
219219
}
220220
$req = new Qiniu_Request($u, $params);
221-
$req->Header['Content-Type'] = 'application/x-www-form-urlencoded';
221+
$req->Header['Content-Type'] = $contentType;
222222
list($resp, $err) = $self->Exec($req);
223223
if ($err !== null) {
224224
return array(null, $err);
225225
}
226226
return Qiniu_Client_ret($resp);
227227
}
228228

229+
function Qiniu_Client_CallWithMultiPart($self, $url, $fields, $files)
230+
{
231+
list($contentType, $body) = Qiniu_Encode_MultiPart_Form($fields, $files);
232+
return Qiniu_Client_CallWithForm($self, $url, $body, $contentType);
233+
}
234+
229235
// --------------------------------------------------------------------------------
230236

237+
function Qiniu_Encode_MultiPart_Form($fields, $files) // => ($contentType, $body)
238+
{
239+
$eol = "\r\n";
240+
$data = array();
241+
if ($fields == '') {
242+
$fields = array();
243+
}
244+
if ($files == '') {
245+
$files = array();
246+
}
247+
248+
$mimeBoundary = md5(time());
249+
foreach ($fields as $name => $val){
250+
array_push($data, '--' . $mimeBoundary);
251+
array_push($data, "Content-Disposition: form-data; name=$name");
252+
array_push($data, '');
253+
array_push($data, $val);
254+
}
255+
256+
foreach ($files as $file) {
257+
array_push($data, '--' . $mimeBoundary);
258+
list($name, $fileName, $fileCxt) = $file;
259+
array_push($data, "Content-Disposition: form-data; name=$name; filename=$fileName");
260+
array_push($data, 'Content-Type: application/octet-stream');
261+
array_push($data, '');
262+
array_push($data, $fileCxt);
263+
}
264+
265+
array_push($data, '--' . $mimeBoundary);
266+
array_push($data, '');
267+
268+
$body = implode($eol, $data);
269+
$contentType = 'multipart/form-data; boundary=' . $mimeBoundary;
270+
return array($contentType, $body);
271+
}
272+

qiniu/io.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
require_once("http.php");
4+
require_once("auth_digest.php");
5+
6+
7+
define('UNDEFINED_KEY', "?");
8+
define('NO_CRC32', 0);
9+
define('AUTO_CRC32', 1);
10+
define('WITH_CRC32', 2);
11+
12+
// ----------------------------------------------------------
13+
// class Qiniu_PutExtra
14+
15+
class Qiniu_PutExtra
16+
{
17+
public $Params;
18+
public $MimeType;
19+
public $Crc32;
20+
public $CheckCrc;
21+
}
22+
23+
function Qiniu_Put($upToken, $key, $body, $putExtra) // => ($data, $err)
24+
{
25+
global $QINIU_UP_HOST;
26+
$fields = array('key' => $key, 'token' => $upToken);
27+
28+
if ( $putExtra->CheckCrc == AUTO_CRC32 || $putExtra->CheckCrc == WITH_CRC32 ) {
29+
$fields['crc32'] = $putExtra->Crc32;
30+
}
31+
$files = array(array('file', $key, $body));
32+
33+
$client = new Qiniu_Client(null);
34+
return Qiniu_Client_CallWithMultiPart($client, $QINIU_UP_HOST, $fields, $files);
35+
}
36+
37+
function Qiniu_PutFile($upToken, $key, $localFile, $putExtra) // => ($data, $err)
38+
{
39+
global $QINIU_UP_HOST;
40+
$fields = array('key' => $key, 'token' => $upToken, 'file' => '@' . $localFile);
41+
42+
if ( $putExtra->CheckCrc == AUTO_CRC32 ) {
43+
$hash = hash_file('crc32b', $localFile);
44+
$crc32 = unpack('N', pack('H*', $hash));
45+
$fields['crc32'] = $crc32[1];
46+
} elseif ($putExtra->CheckCrc == WITH_CRC32 ) {
47+
$fields['crc32'] = $putExtra->Crc32;
48+
}
49+
50+
$client = new Qiniu_Client(null);
51+
return Qiniu_Client_CallWithForm($client, $QINIU_UP_HOST, $fields, 'multipart/form-data');
52+
}

qiniu/rs.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class Qiniu_RS_PutPolicy
5050
public $EndUser;
5151
public $Expires;
5252

53+
public function __construct($scope)
54+
{
55+
$this->Scope = $scope;
56+
}
57+
5358
public function Token($mac) // => $token
5459
{
5560
$deadline = $this->Expires;

tests/IoTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
require("bootstrap.php");
4+
5+
class IoTest extends PHPUnit_Framework_TestCase
6+
{
7+
public $bucket = BUCKET_NAME;
8+
public $key = "iotest";
9+
public $client;
10+
11+
public function setUp()
12+
{
13+
$this->client = new Qiniu_Client(null);
14+
}
15+
16+
public function testPutFile()
17+
{
18+
$putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
19+
$upToken = $putPolicy->Token(null);
20+
$putExtra = new Qiniu_PutExtra();
21+
$putExtra->CheckCrc = 1;
22+
list($ret, $err) = Qiniu_PutFile($upToken, $this->key, __File__, $putExtra);
23+
$this->assertArrayHasKey('hash', $ret);
24+
$this->assertNull($err);
25+
26+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $this->key);
27+
$this->assertNull($err);
28+
}
29+
30+
public function testPut()
31+
{
32+
$putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
33+
$upToken = $putPolicy->Token(null);
34+
$putExtra = new Qiniu_PutExtra();
35+
$putExtra->CheckCrc = 1;
36+
list($ret, $err) = Qiniu_Put($upToken, $this->key, "hello world!", $putExtra);
37+
$this->assertArrayHasKey('hash', $ret);
38+
$this->assertNull($err);
39+
40+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $this->key);
41+
$this->assertNull($err);
42+
}
43+
}

tests/RsTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
require("bootstrap.php");
4+
5+
class RsApiTest extends PHPUnit_Framework_TestCase
6+
{
7+
public $client;
8+
public $bucket = BUCKET_NAME;
9+
public $notExistKey = "not_exist";
10+
public $key1 = KEY_NAME;
11+
public $key2 = "file_name_2";
12+
public $key3 = "file_name_3";
13+
public $key4 = "file_name_4";
14+
public function setUp()
15+
{
16+
$this->client = new Qiniu_Client(null);
17+
}
18+
19+
public function testStat()
20+
{
21+
list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $this->key1);
22+
$this->assertArrayHasKey('hash', $ret);
23+
$this->assertNull($err);
24+
list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $this->notExistKey);
25+
$this->assertNull($ret);
26+
$this->assertFalse($err === null);
27+
}
28+
29+
public function testDeleteMoveCopy()
30+
{
31+
Qiniu_RS_Delete($this->client, $this->bucket, $this->key2);
32+
Qiniu_RS_Delete($this->client, $this->bucket, $this->key3);
33+
34+
$err = Qiniu_RS_Copy($this->client, $this->bucket, $this->key1, $this->bucket, $this->key2);
35+
$this->assertNull($err);
36+
$err = Qiniu_RS_Move($this->client, $this->bucket, $this->key2, $this->bucket, $this->key3);
37+
$this->assertNull($err);
38+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $this->key3);
39+
$this->assertNull($err);
40+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $this->key2);
41+
$this->assertNotNull($err, "delete key2 false");
42+
}
43+
44+
public function testBatchStat()
45+
{
46+
$entries = array(new Qiniu_RS_EntryPath($this->bucket, $this->key1), new Qiniu_RS_EntryPath($this->bucket, $this->key2));
47+
list($ret, $err) = Qiniu_RS_BatchStat($this->client, $entries);
48+
$this->assertNotNull($err);
49+
$this->assertEquals($ret[0]['code'], 200);
50+
$this->assertEquals($ret[1]['code'], 612);
51+
}
52+
53+
public function testBatchDeleteMoveCopy()
54+
{
55+
$e1 = new Qiniu_RS_EntryPath($this->bucket, $this->key1);
56+
$e2 = new Qiniu_RS_EntryPath($this->bucket, $this->key2);
57+
$e3 = new Qiniu_RS_EntryPath($this->bucket, $this->key3);
58+
$e4 = new Qiniu_RS_EntryPath($this->bucket, $this->key4);
59+
Qiniu_RS_BatchDelete($this->client, array($e2, $e3,$e4));
60+
61+
$entryPairs = array(new Qiniu_RS_EntryPathPair($e1, $e2), new Qiniu_RS_EntryPathPair($e1, $e3));
62+
list($ret, $err) = Qiniu_RS_BatchCopy($this->client, $entryPairs);
63+
$this->assertNull($err);
64+
$this->assertEquals($ret[0]['code'], 200);
65+
$this->assertEquals($ret[0]['code'], 200);
66+
67+
list($ret, $err) = Qiniu_RS_BatchMove($this->client, array(new Qiniu_RS_EntryPathPair($e2, $e4)));
68+
$this->assertNull($err);
69+
$this->assertEquals($ret[0]['code'], 200);
70+
71+
list($ret, $err) = Qiniu_RS_BatchDelete($this->client, array($e3, $e4));
72+
$this->assertNull($err);
73+
$this->assertEquals($ret[0]['code'], 200);
74+
$this->assertEquals($ret[0]['code'], 200);
75+
76+
Qiniu_RS_BatchDelete($this->client, array($e2, $e3, $e4));
77+
}
78+
79+
}

tests/bootstrap.php

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

33
require_once("../qiniu/rs.php");
44
require_once("../qiniu/rsf.php");
5+
require_once("../qiniu/io.php");
56

67
$accessKey = getenv("QINIU_ACCESS_KEY");
78
$secretKey = getenv("QINIU_SECRET_KEY");

0 commit comments

Comments
 (0)