Skip to content

Commit 28cddf7

Browse files
committed
fop 元编程
1 parent 4225ed2 commit 28cddf7

File tree

10 files changed

+195
-40
lines changed

10 files changed

+195
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
* 代码覆盖度报告
1111
* policy改为array, 便于灵活增加,并加入过期字段检查
1212
* 文件列表支持目录形式
13+
* 利用元编程方式支持 fop 和 pfop

src/Qiniu/Http/Response.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ public function __construct($code, $duration, array $headers = array(), $body =
8989
}
9090

9191
if ($body == null) {
92-
if ($code != 200) {
92+
if ($code >= 400) {
9393
$this->error = self::$statusTexts[$code];
9494
}
9595
return;
9696
}
97-
if ($headers['Content-Type'] == 'application/json') {
97+
if (self::isJson($headers)) {
9898
try {
9999
$jsonData = self::bodyJson($body);
100100
if ($code >=400) {
@@ -117,12 +117,9 @@ public function __construct($code, $duration, array $headers = array(), $body =
117117
}
118118
}
119119

120-
public function json(array $config = array())
120+
public function json()
121121
{
122-
if ($this->jsonData != null) {
123-
return $this->jsonData;
124-
}
125-
return self::bodyJson($this->body);
122+
return $this->jsonData;
126123
}
127124

128125
private static function bodyJson($body, array $config = array())
@@ -157,11 +154,21 @@ public function xReqId()
157154
return $this->headers['X-Reqid'];
158155
}
159156

157+
public function ok()
158+
{
159+
return $this->statusCode >= 200 && $this->statusCode < 300 && $this->error == null;
160+
}
161+
160162
public function needRetry()
161163
{
162164
$code = $this->statusCode;
163165
if ($code< 0 || ($code / 100 == 5 and $code != 579) || $code == 996) {
164166
return true;
165167
}
166168
}
169+
170+
private static function isJson($headers)
171+
{
172+
return isset($headers['Content-Type']) && $headers['Content-Type'] == 'application/json';
173+
}
167174
}

src/Qiniu/Processing/Operation.php

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<?php
22
namespace Qiniu\Processing;
33

4+
use Qiniu\Http\Client;
5+
use Qiniu\Http\Error;
6+
47
final class Operation
58
{
6-
public static function buildOp($cmd, $first_arg, array $args = array())
9+
private $auth;
10+
private $token_expire;
11+
private $domain;
12+
public static function buildOp($cmd, $mode = null, array $args = array())
713
{
814
$op = array($cmd);
9-
if (!empty($first_arg)) {
10-
array_push($op, $first_arg);
15+
if ($mode !== null) {
16+
array_push($op, $mode);
1117
}
1218
foreach ($args as $key => $value) {
1319
array_push($op, "$key/$value");
@@ -24,4 +30,46 @@ public static function saveas($op, $bucket, $key)
2430
{
2531
return self::pipeCmd(array($op, 'saveas/' . \Qiniu\entry($bucket, $key)));
2632
}
33+
34+
public function __construct($domain, $auth = null, $token_expire = 3600)
35+
{
36+
$this->auth = $auth;
37+
$this->domain = $domain;
38+
$this->token_expire = $token_expire;
39+
}
40+
41+
public function buildUrl($key, $cmd, $mod = null, array $args = array())
42+
{
43+
$fop = self::buildOp($cmd, $mod, $args);
44+
$baseUrl = "http://$this->domain/$key?$fop";
45+
$url = $baseUrl;
46+
if ($this->auth != null) {
47+
$url = $this->auth->privateDownloadUrl($baseUrl, $this->token_expire);
48+
}
49+
return $url;
50+
}
51+
52+
public function __call($method, $args)
53+
{
54+
$key = $args[0];
55+
$cmd = $method;
56+
$mode = null;
57+
if (count($args)>1) {
58+
$mode = $args[1];
59+
}
60+
61+
if (count($args)>2) {
62+
$options = $args[2];
63+
}
64+
$options = array();
65+
$url = $this->buildUrl($key, $cmd, $mode, $options);
66+
$r = Client::get($url);
67+
if (!$r->ok()) {
68+
return array(null, new Error($url, $r));
69+
}
70+
if ($r->json() != null) {
71+
return array($r->json(), null);
72+
}
73+
return array($r->body, null);
74+
}
2775
}

src/Qiniu/Processing/PersistentFop.php

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@
44
use Qiniu\Config;
55
use Qiniu\Http\Client;
66
use Qiniu\Http\Error;
7+
use Qiniu\Processing\Operation;
78

89
final class PersistentFop
910
{
1011
private $auth;
11-
private $bucket;
12-
private $pipeline;
13-
private $notify_url;
12+
private $bucket;
13+
private $pipeline;
14+
private $notify_url;
1415

15-
public function __construct($auth, $bucket, $pipeline = null, $notify_url = null)
16+
public function __construct($auth, $bucket, $pipeline = null, $notify_url = null, $force = false)
1617
{
1718
$this->auth = $auth;
1819
$this->bucket = $bucket;
1920
$this->pipeline = $pipeline;
2021
$this->notify_url = $notify_url;
22+
$this->force = $force;
2123
}
2224

23-
public function execute($key, array $fops, $force = false)
25+
public function execute($key, array $fops)
2426
{
2527
$ops = implode(';', $fops);
2628
$params = array('bucket' => $this->bucket, 'key' => $key, 'fops' => $ops);
@@ -30,15 +32,15 @@ public function execute($key, array $fops, $force = false)
3032
if (!empty($this->notify_url)) {
3133
$params['notifyURL'] = $this->notify_url;
3234
}
33-
if ($force) {
35+
if ($this->force) {
3436
$params['force'] = 1;
3537
}
3638
$data = http_build_query($params);
3739
$url = Config::API_HOST . '/pfop/';
3840
$headers = $this->auth->authorization($url, $data, 'application/x-www-form-urlencoded');
3941
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
4042
$response = Client::post($url, $data, $headers);
41-
if ($response->statusCode != 200) {
43+
if (!$response->ok()) {
4244
return array(null, new Error($url, $response));
4345
}
4446
$r = $response->json();
@@ -50,9 +52,43 @@ public static function status($id)
5052
{
5153
$url = Config::API_HOST . "/status/get/prefop?id=$id";
5254
$response = Client::get($url);
53-
if ($response->statusCode != 200) {
55+
if (!$response->ok()) {
5456
return array(null, new Error($url, $response));
5557
}
5658
return array($response->json(), null);
5759
}
60+
61+
public function __call($method, $args)
62+
{
63+
$key = $args[0];
64+
$cmd = $method;
65+
$mod = null;
66+
if (count($args)>1) {
67+
$mod = $args[1];
68+
}
69+
70+
$options = array();
71+
if (count($args)>2) {
72+
$options = $args[2];
73+
}
74+
75+
$target_bucket = null;
76+
if (count($args)>3) {
77+
$target_bucket = $args[3];
78+
}
79+
80+
$target_key = null;
81+
if (count($args)>4) {
82+
$target_key = $args[4];
83+
}
84+
85+
$pfop = Operation::buildOp($cmd, $mod, $options);
86+
if ($target_bucket != null) {
87+
$pfop = Operation::saveas($pfop, $target_bucket, $target_key);
88+
}
89+
90+
$ops = array();
91+
array_push($ops, $pfop);
92+
return $this->execute($key, $ops);
93+
}
5894
}

src/Qiniu/Storage/BucketManager.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,21 @@ private function get($url)
134134
{
135135
$headers = $this->auth->authorization($url);
136136
$ret = Client::get($url, $headers);
137-
if ($ret->statusCode == 200 && $ret->error == null) {
138-
return array($ret->json(), null);
137+
if (!$ret->ok()) {
138+
return array(null, new Error($url, $ret));
139139
}
140-
return array(null, new Error($url, $ret));
140+
return array($ret->json(), null);
141141
}
142142

143143
private function post($url, $body)
144144
{
145145
$headers = $this->auth->authorization($url, $body, 'application/x-www-form-urlencoded');
146146
$ret = Client::post($url, $body, $headers);
147-
if ($ret->statusCode == 200 && $ret->error == null) {
148-
$r = $ret->body == null ? array() : $ret->json();
149-
return array($r, null);
147+
if (!$ret->ok()) {
148+
return array(null, new Error($url, $ret));
150149
}
151-
return array(null, new Error($url, $ret));
150+
$r = $ret->body == null ? array() : $ret->json();
151+
return array($r, null);
152152
}
153153

154154
public static function buildBatchCopy($source_bucket, $key_pairs, $target_bucket)

src/Qiniu/Storage/FormUploader.php

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

3434
$response = Client::multipartPost(Config::$defaultHost, $fields, 'file', $fname, $data, $mime);
35-
if ($response->statusCode == 200 && $response->json() != null) {
36-
return array($response->json(), null);
35+
if (!$response->ok()) {
36+
return array(null, new Error(Config::$defaultHost, $response));
3737
}
38-
return array(null, new Error(Config::$defaultHost, $response));
38+
return array($response->json(), null);
3939
}
4040

4141
public static function putFile(
@@ -64,10 +64,10 @@ public static function putFile(
6464
}
6565
$headers =array('Content-Type' => 'multipart/form-data');
6666
$response = client::post(Config::$defaultHost, $fields, $headers);
67-
if ($response->statusCode == 200 && $response->json() != null) {
68-
return array($response->json(), null);
67+
if (!$response->ok()) {
68+
return array(null, new Error(Config::$defaultHost, $response));
6969
}
70-
return array(null, new Error(Config::$defaultHost, $response));
70+
return array($response->json(), null);
7171
}
7272

7373
private static function createFile($filename, $mime)

src/Qiniu/Storage/ResumeUploader.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,18 @@ public function upload()
4949
$crc = \Qiniu\crc32_data($data);
5050
$response = $this->makeBlock($data, $blockSize);
5151
$ret = null;
52-
if ($response->statusCode == 200 && $response->error == null
53-
&& $response->json() != null) {
52+
if ($response->ok() && $response->json() != null) {
5453
$ret = $response->json();
5554
}
5655
if ($response->statusCode < 0) {
5756
$this->host = Config::UPBACKUP_HOST;
5857
}
59-
if ($response->needRetry() || $crc != $ret['crc32']) {
58+
if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
6059
$response = $this->makeBlock($data, $blockSize);
6160
$ret = $response->json();
6261
}
6362

64-
if ($response->statusCode != 200 || $crc != $ret['crc32']) {
63+
if (! $response->ok() || !isset($ret['crc32'])|| $crc != $ret['crc32']) {
6564
fclose($this->inputStream);
6665
return array(null, new Error($this->currentUrl, $response));
6766
}
@@ -102,7 +101,7 @@ private function makeFile()
102101
if ($response->needRetry()) {
103102
$response = $this->post($url, $body);
104103
}
105-
if ($response->statusCode != 200) {
104+
if (! $response->ok()) {
106105
return array(null, new Error($this->currentUrl, $response));
107106
}
108107
return array($response->json(), null);

src/Qiniu/functions.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ function json_decode($json, $assoc = false, $depth = 512)
7575

7676
function entry($bucket, $key)
7777
{
78-
return base64_urlSafeEncode($bucket . ':' . $key);
78+
$en = $bucket;
79+
if (!empty($key)) {
80+
$en = $bucket . ':' . $key;
81+
}
82+
return base64_urlSafeEncode($en);
7983
}
8084
}

tests/Qiniu/Tests/FopTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
namespace Qiniu\Tests;
3+
4+
use Qiniu\Processing\Operation;
5+
use Qiniu\Processing\PersistentFop;
6+
7+
class FopTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testExifPub()
10+
{
11+
$fop = new Operation('testres.qiniudn.com');
12+
list($exif, $error) = $fop->exif('gogopher.jpg');
13+
$this->assertNull($error);
14+
$this->assertNotNull($exif);
15+
}
16+
17+
public function testExifPrivate()
18+
{
19+
global $testAuth;
20+
$fop = new Operation('private-res.qiniudn.com', $testAuth);
21+
list($exif, $error) = $fop->exif('noexif.jpg');
22+
$this->assertNotNull($error);
23+
$this->assertNull($exif);
24+
}
25+
}

0 commit comments

Comments
 (0)