@@ -51,6 +51,10 @@ discard block |
||
51 | 51 | } |
52 | 52 | } |
53 | 53 | |
54 | + /** |
|
55 | + * @param string $path |
|
56 | + * @param string $fileHash |
|
57 | + */ |
|
54 | 58 | private function initRequest($path, Psr7\Stream $stream, $fileHash, $params) { |
55 | 59 | $metaData = array( |
56 | 60 | 'expiration' => time() + $this->config->blockExpiration, |
@@ -78,6 +82,9 @@ discard block |
||
78 | 82 | return $initInfo; |
79 | 83 | } |
80 | 84 | |
85 | + /** |
|
86 | + * @param integer $blockId |
|
87 | + */ |
|
81 | 88 | private function blockUpload($blocksInfo, $blockId, Psr7\Stream $stream, $params = []) { |
82 | 89 | $startPosition = $blockId * $this->config->maxBlockSize; |
83 | 90 | $endPosition = $blockId >= $blocksInfo->blocks - 1 ? $stream->getSize() : $startPosition + $this->config->maxBlockSize; |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | public function __construct(Config $config) { |
19 | 19 | $this->config = $config; |
20 | 20 | $this->url = ($this->config->useSsl ? 'https://' : 'http://') . Config::ED_FORM . '/'. |
21 | - $this->config->bucketName; |
|
21 | + $this->config->bucketName; |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | |
104 | 104 | $multipart = []; |
105 | 105 | foreach($postData as $key => $value) { |
106 | - $multipart[] = ['name' => $key, 'contents' => $value]; |
|
106 | + $multipart[] = ['name' => $key, 'contents' => $value]; |
|
107 | 107 | } |
108 | 108 | $multipart[] = [ |
109 | 109 | 'name' => 'file', |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | |
18 | 18 | public function __construct(Config $config) { |
19 | 19 | $this->config = $config; |
20 | - $this->url = ($this->config->useSsl ? 'https://' : 'http://') . Config::ED_FORM . '/'. |
|
20 | + $this->url = ($this->config->useSsl ? 'https://' : 'http://').Config::ED_FORM.'/'. |
|
21 | 21 | $this->config->bucketName; |
22 | 22 | } |
23 | 23 | |
@@ -31,20 +31,20 @@ discard block |
||
31 | 31 | * @throws \Exception |
32 | 32 | */ |
33 | 33 | public function upload($path, $stream, $fileHash, $params = []) { |
34 | - $path = '/' . ltrim($path, '/'); |
|
34 | + $path = '/'.ltrim($path, '/'); |
|
35 | 35 | $initInfo = $this->initRequest($path, $stream, $fileHash, $params); |
36 | 36 | $blockStatus = $initInfo->status; |
37 | 37 | |
38 | 38 | $newBlockStatus = $blockStatus; |
39 | 39 | |
40 | - for($blockId = 0; $blockId < $initInfo->blocks; $blockId++) { |
|
41 | - if($blockStatus[$blockId] === 0) { |
|
40 | + for ($blockId = 0; $blockId < $initInfo->blocks; $blockId++) { |
|
41 | + if ($blockStatus[$blockId] === 0) { |
|
42 | 42 | $return = $this->blockUpload($initInfo, $blockId, $stream); |
43 | 43 | $newBlockStatus = $return->status; |
44 | 44 | } |
45 | 45 | } |
46 | 46 | |
47 | - if(array_sum($newBlockStatus) === $initInfo->blocks) { |
|
47 | + if (array_sum($newBlockStatus) === $initInfo->blocks) { |
|
48 | 48 | return $this->endRequest($initInfo, $params); |
49 | 49 | } else { |
50 | 50 | throw new \Exception(sprintf("chunk upload failed! current every block status is : [%s]", implode(',', $newBlockStatus))); |
@@ -102,13 +102,13 @@ discard block |
||
102 | 102 | ); |
103 | 103 | |
104 | 104 | $multipart = []; |
105 | - foreach($postData as $key => $value) { |
|
105 | + foreach ($postData as $key => $value) { |
|
106 | 106 | $multipart[] = ['name' => $key, 'contents' => $value]; |
107 | 107 | } |
108 | 108 | $multipart[] = [ |
109 | 109 | 'name' => 'file', |
110 | 110 | 'contents' => $fileBlock, |
111 | - 'filename' => 'file', //this value must be file |
|
111 | + 'filename' => 'file', //this value must be file |
|
112 | 112 | 'headers' => ['Content-Type' => 'application/octet-stream'] |
113 | 113 | ]; |
114 | 114 | $postData['file'] = $fileBlock; |
@@ -16,6 +16,9 @@ discard block |
||
16 | 16 | $this->config = $config; |
17 | 17 | } |
18 | 18 | |
19 | + /** |
|
20 | + * @param string $source |
|
21 | + */ |
|
19 | 22 | public function process($source, $tasks) { |
20 | 23 | $encodedTasks = Util::base64Json($tasks); |
21 | 24 | |
@@ -43,6 +46,9 @@ discard block |
||
43 | 46 | } |
44 | 47 | |
45 | 48 | |
49 | + /** |
|
50 | + * @param string $path |
|
51 | + */ |
|
46 | 52 | public function query($taskIds, $path) { |
47 | 53 | $client = new Client([ |
48 | 54 | 'timeout' => $this->config->timeout, |
@@ -1,7 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | namespace Upyun\Api; |
3 | 3 | |
4 | -use GuzzleHttp\Psr7; |
|
5 | 4 | use GuzzleHttp\Client; |
6 | 5 | use Upyun\Config; |
7 | 6 | use Upyun\Signature; |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | $method = 'POST'; |
36 | 36 | $signedHeaders = Signature::getHeaderSign($this->config, $method, $path); |
37 | 37 | |
38 | - $response = $client->request($method, $this->url . $path, [ |
|
38 | + $response = $client->request($method, $this->url.$path, [ |
|
39 | 39 | 'headers' => $signedHeaders, |
40 | 40 | 'form_params' => $params |
41 | 41 | ]); |
@@ -54,10 +54,10 @@ discard block |
||
54 | 54 | 'service' => $this->config->bucketName, |
55 | 55 | 'task_ids' => implode(',', $taskIds) |
56 | 56 | ); |
57 | - $path = $path . '?' . http_build_query($params); |
|
57 | + $path = $path.'?'.http_build_query($params); |
|
58 | 58 | |
59 | 59 | $method = 'GET'; |
60 | - $url = $this->url . $path; |
|
60 | + $url = $this->url.$path; |
|
61 | 61 | $signedHeaders = Signature::getHeaderSign($this->config, $method, $path); |
62 | 62 | $response = $client->request($method, $url, [ |
63 | 63 | 'headers' => $signedHeaders |
@@ -29,6 +29,9 @@ discard block |
||
29 | 29 | $this->endpoint = Config::$restApiEndPoint . '/' . $config->bucketName; |
30 | 30 | } |
31 | 31 | |
32 | + /** |
|
33 | + * @param string $method |
|
34 | + */ |
|
32 | 35 | public function request($method, $storagePath) { |
33 | 36 | $this->method = strtoupper($method); |
34 | 37 | $this->storagePath = '/' . ltrim($storagePath, '/'); |
@@ -37,7 +40,7 @@ discard block |
||
37 | 40 | |
38 | 41 | |
39 | 42 | /** |
40 | - * @param string|resource $file |
|
43 | + * @param Psr7\Stream $file |
|
41 | 44 | * |
42 | 45 | * @return $this |
43 | 46 | */ |
@@ -49,7 +52,7 @@ discard block |
||
49 | 52 | } |
50 | 53 | |
51 | 54 | /** |
52 | - * @return mixed|\Psr\Http\Message\ResponseInterface |
|
55 | + * @return \Psr\Http\Message\ResponseInterface |
|
53 | 56 | */ |
54 | 57 | public function send() { |
55 | 58 | $client = new Client([ |
@@ -27,12 +27,12 @@ discard block |
||
27 | 27 | |
28 | 28 | public function __construct(Config $config) { |
29 | 29 | $this->config = $config; |
30 | - $this->endpoint = Config::$restApiEndPoint . '/' . $config->bucketName; |
|
30 | + $this->endpoint = Config::$restApiEndPoint.'/'.$config->bucketName; |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | public function request($method, $storagePath) { |
34 | 34 | $this->method = strtoupper($method); |
35 | - $this->storagePath = '/' . ltrim($storagePath, '/'); |
|
35 | + $this->storagePath = '/'.ltrim($storagePath, '/'); |
|
36 | 36 | return $this; |
37 | 37 | } |
38 | 38 | |
@@ -57,9 +57,9 @@ discard block |
||
57 | 57 | 'timeout' => $this->config->timeout, |
58 | 58 | ]); |
59 | 59 | |
60 | - $url = ($this->config->useSsl ? 'https://' : 'http://') . $this->endpoint . $this->storagePath; |
|
60 | + $url = ($this->config->useSsl ? 'https://' : 'http://').$this->endpoint.$this->storagePath; |
|
61 | 61 | $body = null; |
62 | - if($this->file && $this->method === 'PUT') { |
|
62 | + if ($this->file && $this->method === 'PUT') { |
|
63 | 63 | $body = $this->file; |
64 | 64 | } |
65 | 65 | |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | $this->method, |
74 | 74 | $request->getUri()->getPath() |
75 | 75 | ); |
76 | - foreach($authHeader as $head => $value) { |
|
76 | + foreach ($authHeader as $head => $value) { |
|
77 | 77 | $request = $request->withHeader($head, $value); |
78 | 78 | } |
79 | 79 | $response = $client->send($request, [ |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | } |
92 | 92 | |
93 | 93 | public function withHeaders($headers) { |
94 | - if(is_array($headers)) { |
|
94 | + if (is_array($headers)) { |
|
95 | 95 | foreach ($headers as $header => $value) { |
96 | 96 | $this->withHeader($header, $value); |
97 | 97 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | use Upyun\Config; |
4 | 4 | use Upyun\Upyun; |
5 | 5 | |
6 | -class UpyunTest extends \PHPUnit_Framework_TestCase{ |
|
6 | +class UpyunTest extends \PHPUnit_Framework_TestCase { |
|
7 | 7 | |
8 | 8 | /** |
9 | 9 | * @var Upyun |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | $config->setFormApiKey('Mv83tlocuzkmfKKUFbz2s04FzTw='); |
21 | 21 | $config->processNotifyUrl = 'http://localhost:9999'; |
22 | 22 | self::$upyun = new Upyun($config); |
23 | - self::$tempFilePath = __DIR__ . '/assets/test.txt'; |
|
23 | + self::$tempFilePath = __DIR__.'/assets/test.txt'; |
|
24 | 24 | touch(self::$tempFilePath); |
25 | 25 | } |
26 | 26 | |
@@ -38,8 +38,8 @@ discard block |
||
38 | 38 | |
39 | 39 | public function testWriteStream() { |
40 | 40 | $filename = 'test.jpeg'; |
41 | - $f = fopen(__DIR__ . '/assets/sample.jpeg', 'rb'); |
|
42 | - if(!$f) { |
|
41 | + $f = fopen(__DIR__.'/assets/sample.jpeg', 'rb'); |
|
42 | + if (!$f) { |
|
43 | 43 | throw new \Exception('open test file failed!'); |
44 | 44 | } |
45 | 45 | self::$upyun->write($filename, $f); |
@@ -50,8 +50,8 @@ discard block |
||
50 | 50 | public function testWriteWithAsyncProcess() { |
51 | 51 | $filename = 'test_async.jpeg'; |
52 | 52 | $newFilename = 'test_async.png'; |
53 | - $f = fopen(__DIR__ . '/assets/sample.jpeg', 'rb'); |
|
54 | - if(!$f) { |
|
53 | + $f = fopen(__DIR__.'/assets/sample.jpeg', 'rb'); |
|
54 | + if (!$f) { |
|
55 | 55 | throw new \Exception('open test file failed!'); |
56 | 56 | } |
57 | 57 | $result = self::$upyun->write($filename, $f, array( |
@@ -72,8 +72,8 @@ discard block |
||
72 | 72 | $fs = new Upyun(new Config(BUCKET, USER_NAME, 'error-password')); |
73 | 73 | try { |
74 | 74 | $fs->write('test.txt', 'test file content'); |
75 | - } catch(\Exception $e) { |
|
76 | - return ; |
|
75 | + } catch (\Exception $e) { |
|
76 | + return; |
|
77 | 77 | } |
78 | 78 | throw new \Exception('should get sign error.'); |
79 | 79 | } |
@@ -104,8 +104,8 @@ discard block |
||
104 | 104 | $r = self::$upyun->delete('test-delete.txt'); |
105 | 105 | try { |
106 | 106 | self::$upyun->read('test-delete.txt'); |
107 | - } catch(\Exception $e) { |
|
108 | - return ; |
|
107 | + } catch (\Exception $e) { |
|
108 | + return; |
|
109 | 109 | } |
110 | 110 | throw new \Exception('delete file failed'); |
111 | 111 | } |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | |
190 | 190 | public function testProcess() { |
191 | 191 | $source = 'php-sdk-sample.mp4'; |
192 | - self::$upyun->write($source, fopen(__DIR__ . '/assets/SampleVideo_640x360_1mb.mp4', 'r')); |
|
192 | + self::$upyun->write($source, fopen(__DIR__.'/assets/SampleVideo_640x360_1mb.mp4', 'r')); |
|
193 | 193 | $result = self::$upyun->process($source, array( |
194 | 194 | array('type' => 'video', 'avopts' => '/s/240p(4:3)/as/1/r/30', 'return_info' => true, 'save_as' => '/video/result.mp4') |
195 | 195 | )); |
@@ -3,5 +3,5 @@ |
||
3 | 3 | 'user_name' => 'tester', |
4 | 4 | 'pwd' => 'grjxv2mxELR3', |
5 | 5 | 'bucket' => 'sdkimg', |
6 | - 'picture_path' => dirname(__FILE__) . '/assets/sample.jpeg' |
|
6 | + 'picture_path' => dirname(__FILE__).'/assets/sample.jpeg' |
|
7 | 7 | ); |
8 | 8 | \ No newline at end of file |
@@ -1,15 +1,15 @@ discard block |
||
1 | 1 | <?php |
2 | -require dirname(__DIR__) . '/vendor/autoload.php'; |
|
3 | -$config = require_once __DIR__ . '/config.php'; |
|
2 | +require dirname(__DIR__).'/vendor/autoload.php'; |
|
3 | +$config = require_once __DIR__.'/config.php'; |
|
4 | 4 | |
5 | 5 | define('USER_NAME', $config['user_name']); |
6 | 6 | define('PWD', $config['pwd']); |
7 | 7 | define('BUCKET', $config['bucket']); |
8 | -define('PIC_PATH' , $config['picture_path']); |
|
9 | -define('PIC_SIZE' , filesize(PIC_PATH)); |
|
8 | +define('PIC_PATH', $config['picture_path']); |
|
9 | +define('PIC_SIZE', filesize(PIC_PATH)); |
|
10 | 10 | |
11 | 11 | function getFileUrl($path) { |
12 | - return "http://" . BUCKET . ".b0.upaiyun.com/" . ltrim($path, '/'); |
|
12 | + return "http://".BUCKET.".b0.upaiyun.com/".ltrim($path, '/'); |
|
13 | 13 | } |
14 | 14 | |
15 | 15 | function getUpyunFileSize($path) { |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | preg_match('~^HTTP/1.1 (\d{3})~', $return, $match1); |
23 | 23 | preg_match('~Content-Length: (\d+)~', $return, $match2); |
24 | 24 | |
25 | - if(isset($match1[1]) && $match1[1] == 200) { |
|
25 | + if (isset($match1[1]) && $match1[1] == 200) { |
|
26 | 26 | return isset($match2[1]) ? intval($match2[1]) : false; |
27 | 27 | } else { |
28 | 28 | return false; |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | use GuzzleHttp\Psr7; |
7 | 7 | use Upyun\Upyun; |
8 | 8 | |
9 | -class MultiTest extends \PHPUnit_Framework_TestCase{ |
|
9 | +class MultiTest extends \PHPUnit_Framework_TestCase { |
|
10 | 10 | |
11 | 11 | /** |
12 | 12 | * @var Multi; |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | } |
21 | 21 | |
22 | 22 | public function testUpload() { |
23 | - $filePath = __DIR__ . '/../assets/sample.jpeg'; |
|
23 | + $filePath = __DIR__.'/../assets/sample.jpeg'; |
|
24 | 24 | $stream = Psr7\stream_for(fopen($filePath, 'rb')); |
25 | 25 | $r = $this->multiPart->upload('test-sample.jpeg', $stream, md5_file($filePath)); |
26 | 26 | $this->assertEquals($r->getStatusCode(), 200); |
@@ -31,9 +31,9 @@ |
||
31 | 31 | if(! $useBlock) { |
32 | 32 | $req = new Rest($this->config); |
33 | 33 | return $req->request('PUT', $path) |
34 | - ->withHeaders($params) |
|
35 | - ->withFile($stream) |
|
36 | - ->send(); |
|
34 | + ->withHeaders($params) |
|
35 | + ->withFile($stream) |
|
36 | + ->send(); |
|
37 | 37 | } else { |
38 | 38 | return $this->pointUpload($path, $stream, $params); |
39 | 39 | } |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | return $req->upload($path, $stream, $params); |
29 | 29 | } |
30 | 30 | |
31 | - if(! $useBlock) { |
|
31 | + if (!$useBlock) { |
|
32 | 32 | $req = new Rest($this->config); |
33 | 33 | return $req->request('PUT', $path) |
34 | 34 | ->withHeaders($params) |
@@ -53,7 +53,7 @@ discard block |
||
53 | 53 | $headers = array(); |
54 | 54 | if (is_array($params)) { |
55 | 55 | foreach ($params as $key => $val) { |
56 | - $headers['X-Upyun-Meta-' . $key] = $val; |
|
56 | + $headers['X-Upyun-Meta-'.$key] = $val; |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 | $res = $req->request('PUT', $path) |
@@ -103,10 +103,10 @@ discard block |
||
103 | 103 | } |
104 | 104 | |
105 | 105 | private function needUseBlock($fileSize) { |
106 | - if($this->config->uploadType === 'BLOCK') { |
|
106 | + if ($this->config->uploadType === 'BLOCK') { |
|
107 | 107 | return true; |
108 | - } else if($this->config->uploadType === 'AUTO' && |
|
109 | - $fileSize >= $this->config->sizeBoundary ) { |
|
108 | + } else if ($this->config->uploadType === 'AUTO' && |
|
109 | + $fileSize >= $this->config->sizeBoundary) { |
|
110 | 110 | return true; |
111 | 111 | } else { |
112 | 112 | return false; |
@@ -111,7 +111,7 @@ |
||
111 | 111 | throw new \Exception('form api key is empty.'); |
112 | 112 | } |
113 | 113 | |
114 | - return $this->formApiKey; |
|
114 | + return $this->formApiKey; |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | public function setFormApiKey($key) { |
@@ -82,17 +82,17 @@ discard block |
||
82 | 82 | /** |
83 | 83 | * 分块上传接口请求地址 |
84 | 84 | */ |
85 | - const ED_FORM = 'm0.api.upyun.com'; |
|
85 | + const ED_FORM = 'm0.api.upyun.com'; |
|
86 | 86 | |
87 | 87 | /** |
88 | 88 | * 异步云处理接口地址 |
89 | 89 | */ |
90 | - const ED_VIDEO = 'p0.api.upyun.com'; |
|
90 | + const ED_VIDEO = 'p0.api.upyun.com'; |
|
91 | 91 | |
92 | 92 | /** |
93 | 93 | * 刷新接口地址 |
94 | 94 | */ |
95 | - const ED_PURGE = 'http://purge.upyun.com/purge/'; |
|
95 | + const ED_PURGE = 'http://purge.upyun.com/purge/'; |
|
96 | 96 | |
97 | 97 | public function __construct($bucketName, $operatorName, $operatorPassword) { |
98 | 98 | $this->bucketName = $bucketName; |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | } |
108 | 108 | |
109 | 109 | public function getFormApiKey() { |
110 | - if(! $this->formApiKey) { |
|
110 | + if (!$this->formApiKey) { |
|
111 | 111 | throw new \Exception('form api key is empty.'); |
112 | 112 | } |
113 | 113 |