Skip to content

Commit f4e0af3

Browse files
committed
Merge pull request #8 from xushiwei/feature/v6
add rs_utils.php
2 parents 4131b25 + f75978e commit f4e0af3

File tree

9 files changed

+113
-26
lines changed

9 files changed

+113
-26
lines changed

qiniu/http.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,17 @@ public function RoundTrip($req) // => ($resp, $error)
156156

157157
class Qiniu_MacHttpClient
158158
{
159-
private $mac;
159+
public $Mac;
160160

161161
public function __construct($mac)
162162
{
163-
$this->mac = Qiniu_RequireMac($mac);
163+
$this->Mac = Qiniu_RequireMac($mac);
164164
}
165165

166166
public function RoundTrip($req) // => ($resp, $error)
167167
{
168168
$incbody = Qiniu_Client_incBody($req);
169-
$token = $this->mac->SignRequest($req, $incbody);
169+
$token = $this->Mac->SignRequest($req, $incbody);
170170
$req->Header['Authorization'] = "QBox $token";
171171
return Qiniu_Client_do($req);
172172
}

qiniu/io.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ function Qiniu_PutFile($upToken, $key, $localFile, $putExtra) // => ($data, $err
5757
if ($putExtra->CheckCrc) {
5858
if ($putExtra->CheckCrc === 1) {
5959
$hash = hash_file('crc32b', $localFile);
60-
$crc32 = unpack('N', pack('H*', $hash));
61-
$fields['crc32'] = $crc32[1];
62-
} else {
63-
$fields['crc32'] = $putExtra->Crc32;
60+
$array = unpack('N', pack('H*', $hash));
61+
$putExtra->Crc32 = $array[1];
6462
}
63+
$fields['crc32'] = sprintf('%u', $putExtra->Crc32);
6564
}
6665

6766
$client = new Qiniu_HttpClient;

qiniu/rs_utils.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
require_once("rs.php");
4+
require_once("io.php");
5+
6+
function Qiniu_RS_Put($self, $bucket, $key, $body, $putExtra) // => ($data, $err)
7+
{
8+
$putPolicy = new Qiniu_RS_PutPolicy("$bucket:$key");
9+
$upToken = $putPolicy->Token($self->Mac);
10+
return Qiniu_Put($upToken, $key, $body, $putExtra);
11+
}
12+
13+
function Qiniu_RS_PutFile($self, $bucket, $key, $localFile, $putExtra) // => ($data, $err)
14+
{
15+
$putPolicy = new Qiniu_RS_PutPolicy("$bucket:$key");
16+
$upToken = $putPolicy->Token($self->Mac);
17+
return Qiniu_PutFile($upToken, $key, $localFile, $putExtra);
18+
}
19+

tests/AuthDigestTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require("bootstrap.php");
3+
require_once("bootstrap.php");
44

55
class AuthDigestTest extends PHPUnit_Framework_TestCase
66
{
@@ -16,17 +16,18 @@ public function testEncode()
1616
}
1717
}
1818

19-
public function testSetKeys()
19+
/* public function testSetKeys()
2020
{
21-
$mac = Qiniu_RequireMac(null);
22-
$this->assertTrue(!empty($mac->AccessKey) && !empty($mac->SecretKey), 'please provide keys');
21+
$mac1 = Qiniu_RequireMac(null);
22+
$this->assertTrue(!empty($mac1->AccessKey) && !empty($mac1->SecretKey), 'please provide keys');
2323
2424
Qiniu_SetKeys('abc', 'def');
2525
$mac = Qiniu_RequireMac(null);
2626
$this->assertEquals($mac->AccessKey, 'abc');
2727
$this->assertEquals($mac->SecretKey, 'def');
2828
29-
Qiniu_SetKeys($mac->AccessKey, $mac->SecretKey);
29+
Qiniu_SetKeys($mac1->AccessKey, $mac1->SecretKey);
3030
}
31+
*/
3132
}
3233

tests/FopTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php
22

3-
require_once('bootstrap.php');
3+
require_once("bootstrap.php");
44

55
class FopTest extends PHPUnit_Framework_TestCase
66
{
7-
8-
public $url = 'http://phpsdk.qiniudn.com/f22.jpeg';
7+
public $url = 'http://phpsdk.qiniudn.com/f22.jpeg';
98

109
public function testImageView()
1110
{

tests/IoTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
require("bootstrap.php");
3+
require_once("bootstrap.php");
44

55
class IoTest extends PHPUnit_Framework_TestCase
66
{
@@ -9,13 +9,14 @@ class IoTest extends PHPUnit_Framework_TestCase
99

1010
public function setUp()
1111
{
12+
initKeys();
1213
$this->client = new Qiniu_MacHttpClient(null);
1314
$this->bucket = getenv("QINIU_BUCKET_NAME");
1415
}
1516

1617
public function testPutFile()
1718
{
18-
$key = 'testPutFile';
19+
$key = 'testPutFile' . getTid();
1920
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
2021

2122
$putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
@@ -37,7 +38,7 @@ public function testPutFile()
3738

3839
public function testPut()
3940
{
40-
$key = 'testPut';
41+
$key = 'testPut' . getTid();
4142
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
4243

4344
$putPolicy = new Qiniu_RS_PutPolicy($this->bucket);

tests/RsTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?php
22

3-
require("bootstrap.php");
3+
require_once("bootstrap.php");
44

55
class RsTest extends PHPUnit_Framework_TestCase
66
{
77
public $client;
88
public $bucket;
99
public $key;
10-
public $notExistKey = "not_exist";
10+
public $notExistKey = 'not_exist';
1111

1212
public function setUp()
1313
{
14+
initKeys();
1415
$this->client = new Qiniu_MacHttpClient(null);
1516
$this->bucket = getenv("QINIU_BUCKET_NAME");
1617
$this->key = getenv("QINIU_KEY_NAME");
@@ -28,8 +29,8 @@ public function testStat()
2829

2930
public function testDeleteMoveCopy()
3031
{
31-
$key2 = rand();
32-
$key3 = rand();
32+
$key2 = 'testOp2' . getTid();
33+
$key3 = 'testOp3' . getTid();
3334
Qiniu_RS_Delete($this->client, $this->bucket, $key2);
3435
Qiniu_RS_Delete($this->client, $this->bucket, $key3);
3536

tests/RsUtilsTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
require_once("bootstrap.php");
4+
5+
class RsUtilsTest extends PHPUnit_Framework_TestCase
6+
{
7+
public $bucket;
8+
public $client;
9+
10+
public function setUp()
11+
{
12+
initKeys();
13+
$this->client = new Qiniu_MacHttpClient(null);
14+
$this->bucket = getenv("QINIU_BUCKET_NAME");
15+
}
16+
17+
public function testPutFile()
18+
{
19+
$key = 'testPutFile' . getTid();
20+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
21+
22+
$putExtra = new Qiniu_PutExtra();
23+
$putExtra->CheckCrc = 1;
24+
list($ret, $err) = Qiniu_RS_PutFile($this->client, $this->bucket, $key, __file__, $putExtra);
25+
$this->assertNull($err);
26+
$this->assertArrayHasKey('hash', $ret);
27+
var_dump($ret);
28+
29+
list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
30+
$this->assertNull($err);
31+
var_dump($ret);
32+
33+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
34+
$this->assertNull($err);
35+
}
36+
37+
public function testPut()
38+
{
39+
$key = 'testPut' . getTid();
40+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
41+
42+
list($ret, $err) = Qiniu_RS_Put($this->client, $this->bucket, $key, "hello world!", null);
43+
$this->assertNull($err);
44+
$this->assertArrayHasKey('hash', $ret);
45+
var_dump($ret);
46+
47+
list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
48+
$this->assertNull($err);
49+
var_dump($ret);
50+
51+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
52+
$this->assertNull($err);
53+
}
54+
}
55+

tests/bootstrap.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
<?php
22

3-
require_once("../qiniu/rs.php");
4-
require_once("../qiniu/io.php");
53
require_once("../qiniu/fop.php");
4+
require_once("../qiniu/rs_utils.php");
65

76
$accessKey = getenv("QINIU_ACCESS_KEY");
87
$secretKey = getenv("QINIU_SECRET_KEY");
98

10-
if (!empty($accessKey) && !empty($secretKey)) {
11-
Qiniu_SetKeys($accessKey, $secretKey);
9+
$tid = getenv("TRAVIS_JOB_NUMBER");
10+
if (!empty($tid)) {
11+
$tid = strstr($tid, ".");
12+
}
13+
14+
function initKeys() {
15+
global $accessKey, $secretKey;
16+
if (!empty($accessKey) && !empty($secretKey)) {
17+
Qiniu_SetKeys($accessKey, $secretKey);
18+
}
19+
}
20+
21+
function getTid() {
22+
global $tid;
23+
return $tid;
1224
}
1325

0 commit comments

Comments
 (0)