Skip to content

Commit a89f7fe

Browse files
committed
Rio support
1 parent aa8b10d commit a89f7fe

File tree

3 files changed

+114
-9
lines changed

3 files changed

+114
-9
lines changed

qiniu/resumable_io.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class Qiniu_Rio_PutExtra
1616
public $Progresses = null; // 可选。上传进度:[]BlkputRet
1717
public $Notify = null; // 进度通知:func(blkIdx int, blkSize int, ret *BlkputRet)
1818
public $NotifyErr = null; // 错误通知:func(blkIdx int, blkSize int, err error)
19+
20+
public function __construct($bucket = null) {
21+
$this->Bucket = $bucket;
22+
}
1923
}
2024

2125
// ----------------------------------------------------------
@@ -34,12 +38,19 @@ function Qiniu_Rio_BlockCount($fsize) // => $blockCnt
3438

3539
function Qiniu_Rio_Mkblock($self, $host, $reader, $size) // => ($blkputRet, $err)
3640
{
37-
$body = fread($reader, $size);
38-
if ($body === false) {
39-
$err = Qiniu_NewError(0, 'fread failed');
40-
return array(null, $err);
41+
if (is_resource($reader)) {
42+
$body = fread($reader, $size);
43+
if ($body === false) {
44+
$err = Qiniu_NewError(0, 'fread failed');
45+
return array(null, $err);
46+
}
47+
} else {
48+
list($body, $err) = $reader->Read($size);
49+
if ($err !== null) {
50+
return array(null, $err);
51+
}
4152
}
42-
if (strlen($body) != $fsize) {
53+
if (strlen($body) != $size) {
4354
$err = Qiniu_NewError(0, 'fread failed: unexpected eof');
4455
return array(null, $err);
4556
}
@@ -58,7 +69,7 @@ function Qiniu_Rio_Mkfile($self, $host, $key, $fsize, $extra) // => ($putRet, $e
5869
}
5970

6071
$ctxs = array();
61-
for ($extra->Progresses as $prog) {
72+
foreach ($extra->Progresses as $prog) {
6273
$ctxs []= $prog['ctx'];
6374
}
6475
$body = implode(',', $ctxs);
@@ -98,8 +109,8 @@ function Qiniu_Rio_Put($upToken, $key, $body, $fsize, $putExtra) // => ($putRet,
98109
$progresses = array();
99110
$host = $QINIU_UP_HOST;
100111
$uploaded = 0;
101-
while($uploaded < $fsize) {
102-
if ($uploaded + QINIU_RIO_BLOCK_SIZE < $fsize) {
112+
while ($uploaded < $fsize) {
113+
if ($fsize < $uploaded + QINIU_RIO_BLOCK_SIZE) {
103114
$bsize = $fsize - $uploaded;
104115
} else {
105116
$bsize = QINIU_RIO_BLOCK_SIZE;
@@ -110,12 +121,12 @@ function Qiniu_Rio_Put($upToken, $key, $body, $fsize, $putExtra) // => ($putRet,
110121
$progresses []= $blkputRet;
111122
}
112123

124+
$putExtra->Progresses = $progresses;
113125
return Qiniu_Rio_Mkfile($self, $host, $key, $fsize, $putExtra);
114126
}
115127

116128
function Qiniu_Rio_PutFile($upToken, $key, $localFile, $putExtra) // => ($putRet, $err)
117129
{
118-
119130
$fp = fopen($localFile, 'rb');
120131
if ($fp === false) {
121132
$err = Qiniu_NewError(0, 'fopen failed');

qiniu/rs_utils.php

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

33
require_once("rs.php");
44
require_once("io.php");
5+
require_once("resumable_io.php");
56

67
function Qiniu_RS_Put($self, $bucket, $key, $body, $putExtra) // => ($putRet, $err)
78
{

tests/RioTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
require_once("bootstrap.php");
4+
5+
class MockReader
6+
{
7+
private $off = 0;
8+
9+
public function __construct($off = 0)
10+
{
11+
$this->off = $off;
12+
}
13+
14+
public function Read($bytes) // => ($data, $err)
15+
{
16+
$off = $this->off;
17+
$data = '';
18+
for ($i = 0; $i < $bytes; $i++) {
19+
$data .= chr(65 + ($off % 26)); // ord('A') = 65
20+
$off++;
21+
}
22+
$this->off = $off;
23+
return array($data, null);
24+
}
25+
}
26+
27+
class RioTest extends PHPUnit_Framework_TestCase
28+
{
29+
public $bucket;
30+
public $client;
31+
32+
public function setUp()
33+
{
34+
initKeys();
35+
$this->client = new Qiniu_MacHttpClient(null);
36+
$this->bucket = getenv("QINIU_BUCKET_NAME");
37+
}
38+
39+
public function testMockReader()
40+
{
41+
$reader = new MockReader;
42+
list($data) = $reader->Read(5);
43+
$this->assertEquals($data, "ABCDE");
44+
45+
list($data) = $reader->Read(27);
46+
$this->assertEquals($data, "FGHIJKLMNOPQRSTUVWXYZABCDEF");
47+
}
48+
49+
public function testPut()
50+
{
51+
$key = 'testRioPut' . getTid();
52+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
53+
54+
$putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
55+
$upToken = $putPolicy->Token(null);
56+
$putExtra = new Qiniu_Rio_PutExtra($this->bucket);
57+
$reader = new MockReader;
58+
list($ret, $err) = Qiniu_Rio_Put($upToken, $key, $reader, 5, $putExtra);
59+
$this->assertNull($err);
60+
$this->assertEquals($ret['hash'], "Fnvgeq9GDVk6Mj0Nsz2gW2S_3LOl");
61+
var_dump($ret);
62+
63+
list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
64+
$this->assertNull($err);
65+
var_dump($ret);
66+
67+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
68+
$this->assertNull($err);
69+
}
70+
71+
public function testLargePut()
72+
{
73+
$key = 'testRioLargePut' . getTid();
74+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
75+
76+
$putPolicy = new Qiniu_RS_PutPolicy($this->bucket);
77+
$upToken = $putPolicy->Token(null);
78+
$putExtra = new Qiniu_Rio_PutExtra($this->bucket);
79+
$reader = new MockReader;
80+
list($ret, $err) = Qiniu_Rio_Put($upToken, $key, $reader, QINIU_RIO_BLOCK_SIZE + 5, $putExtra);
81+
$this->assertNull($err);
82+
$this->assertEquals($ret['hash'], "lgQEOCZ8Ievliq8XOfZmWTndgOll");
83+
var_dump($ret);
84+
85+
list($ret, $err) = Qiniu_RS_Stat($this->client, $this->bucket, $key);
86+
$this->assertNull($err);
87+
var_dump($ret);
88+
89+
$err = Qiniu_RS_Delete($this->client, $this->bucket, $key);
90+
$this->assertNull($err);
91+
}
92+
}
93+

0 commit comments

Comments
 (0)