Skip to content

Commit 5d8d875

Browse files
committed
Merge pull request #137 from longbai/config_uphost
upload backup host
2 parents 610f947 + 5381fe6 commit 5d8d875

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

src/Qiniu/Config.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@ final class Config
1212
const RSF_HOST = 'http://rsf.qbox.me'; // 列举操作Host
1313
const API_HOST = 'http://api.qiniu.com'; // 数据处理操作Host
1414

15-
public static $upHost; // 上传Host
16-
public static $upHostBackup; // 上传备用Host
15+
private $upHost; // 上传Host
16+
private $upHostBackup; // 上传备用Host
1717

18-
public function __construct() // 构造函数,默认为zone0
18+
public function __construct(Zone $z = null) // 构造函数,默认为zone0
1919
{
20-
self::setZone(Zone::zone0());
20+
if ($z === null) {
21+
$z = Zone::zone0();
22+
}
23+
$this->upHost = $z->upHost;
24+
$this->upHostBackup = $z->upHostBackup;
2125
}
22-
23-
public static function setZone(Zone $z)
26+
27+
public function getUpHost()
28+
{
29+
return $this->upHost;
30+
}
31+
32+
public function getUpHostBackup()
2433
{
25-
self::$upHost = $z->upHost;
26-
self::$upHostBackup = $z->upHostBackup;
34+
return $this->upHostBackup;
2735
}
2836
}

src/Qiniu/Storage/FormUploader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public static function put(
3232
}
3333
}
3434

35-
$response = Client::multipartPost($config::$upHost, $fields, 'file', $fname, $data, $mime);
35+
$response = Client::multipartPost($config->getUpHost(), $fields, 'file', $fname, $data, $mime);
3636
if (!$response->ok()) {
37-
return array(null, new Error($config::$upHost, $response));
37+
return array(null, new Error($config->getUpHost(), $response));
3838
}
3939
return array($response->json(), null);
4040
}
@@ -65,9 +65,9 @@ public static function putFile(
6565
}
6666
}
6767
$headers =array('Content-Type' => 'multipart/form-data');
68-
$response = client::post($config::$upHost, $fields, $headers);
68+
$response = client::post($config->getUpHost(), $fields, $headers);
6969
if (!$response->ok()) {
70-
return array(null, new Error($config::$upHost, $response));
70+
return array(null, new Error($config->getUpHost(), $response));
7171
}
7272
return array($response->json(), null);
7373
}

src/Qiniu/Storage/ResumeUploader.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class ResumeUploader
2424
private $contexts;
2525
private $host;
2626
private $currentUrl;
27+
private $config;
2728

2829
/**
2930
* 上传二进制流到七牛
@@ -52,11 +53,11 @@ public function __construct(
5253
$this->size = $size;
5354
$this->params = $params;
5455
$this->mime = $mime;
55-
$this->host = $config::$upHost;
5656
$this->contexts = array();
57+
$this->config = $config;
58+
$this->host = $config->getUpHost();
5759
}
5860

59-
6061
/**
6162
* 上传操作
6263
*/
@@ -77,7 +78,7 @@ public function upload()
7778
$ret = $response->json();
7879
}
7980
if ($response->statusCode < 0) {
80-
$this->host = $config::$upHostBackup;
81+
$this->host = $this->config->getUpHostBackup();
8182
}
8283
if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
8384
$response = $this->makeBlock($data, $blockSize);

src/Qiniu/Storage/UploadManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
*/
1414
final class UploadManager
1515
{
16-
public $config;
16+
private $config;
1717

18-
public function __construct($config = null)
18+
public function __construct(Config $config = null)
1919
{
2020
if ($config === null) {
2121
$config = new Config();

tests/Qiniu/Tests/ResumeUpTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
use Qiniu\Storage\ResumeUploader;
55
use Qiniu\Storage\UploadManager;
6+
use Qiniu\Config;
7+
use Qiniu\Zone;
68

79
class ResumeUpTest extends \PHPUnit_Framework_TestCase
810
{
@@ -29,6 +31,19 @@ public function test4ML()
2931
unlink($tempFile);
3032
}
3133

34+
public function test4ML2()
35+
{
36+
$key = 'resumePutFile4ML';
37+
$zone = new Zone('http://up.fake.com', 'http://up.qiniu.com');
38+
$cfg = new Config($zone);
39+
$upManager = new UploadManager($cfg);
40+
$token = $this->auth->uploadToken($this->bucketName, $key);
41+
$tempFile = qiniuTempFile(4*1024*1024+10);
42+
list($ret, $error) = $upManager->putFile($token, $key, $tempFile);
43+
$this->assertNull($error);
44+
$this->assertNotNull($ret['hash']);
45+
unlink($tempFile);
46+
}
3247
// public function test8M()
3348
// {
3449
// $key = 'resumePutFile8M';

0 commit comments

Comments
 (0)