Completed
Pull Request — master (#288)
by Bai
13:51
created

ResumeUploader::upload()   C

Complexity

Conditions 13
Paths 20

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 16.2059

Importance

Changes 0
Metric Value
cc 13
nc 20
nop 1
dl 0
loc 37
ccs 22
cts 30
cp 0.7332
crap 16.2059
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Qiniu\Storage;
3
4
use Qiniu\Config;
5
use Qiniu\Http\Client;
6
use Qiniu\Http\Error;
7
8
/**
9
 * 断点续上传类, 该类主要实现了断点续上传中的分块上传,
10
 * 以及相应地创建块和创建文件过程.
11
 *
12
 * @link http://developer.qiniu.com/docs/v6/api/reference/up/mkblk.html
13
 * @link http://developer.qiniu.com/docs/v6/api/reference/up/mkfile.html
14
 */
15
final class ResumeUploader
16
{
17
    private $upToken;
18
    private $key;
19
    private $inputStream;
20
    private $size;
21
    private $params;
22
    private $mime;
23
    private $contexts;
24
    private $host;
25
    private $currentUrl;
26
    private $config;
27
28
    /**
29
     * 上传二进制流到七牛
30
     *
31
     * @param $upToken    上传凭证
32
     * @param $key        上传文件名
33
     * @param $inputStream 上传二进制流
34
     * @param $size       上传流的大小
35
     * @param $params     自定义变量
36
     * @param $mime       上传数据的mimeType
37
     *
38
     * @link http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
39
     */
40 6
    public function __construct(
41
        $upToken,
42
        $key,
43
        $inputStream,
44
        $size,
45 3
        $params,
46
        $mime,
47
        $config
48
    ) {
49
50 6
        $this->upToken = $upToken;
51 6
        $this->key = $key;
52 6
        $this->inputStream = $inputStream;
53 6
        $this->size = $size;
54 6
        $this->params = $params;
55 6
        $this->mime = $mime;
56 6
        $this->contexts = array();
57 6
        $this->config = $config;
58
59 6
        list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
60 6
        if ($err != null) {
61
            return array(null, $err);
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
62
        }
63
64 6
        $upHost = $config->getUpHost($accessKey, $bucket);
65 3
        if ($err != null) {
66
            throw new \Exception($err->message(), 1);
67
        }
68 3
        $this->host = $upHost;
69 3
    }
70
71
    /**
72
     * 上传操作
73
     */
74 3
    public function upload($fname)
75
    {
76 3
        $uploaded = 0;
77 3
        while ($uploaded < $this->size) {
78 3
            $blockSize = $this->blockSize($uploaded);
79 3
            $data = fread($this->inputStream, $blockSize);
80 3
            if ($data === false) {
81
                throw new \Exception("file read failed", 1);
82
            }
83 3
            $crc = \Qiniu\crc32_data($data);
84 3
            $response = $this->makeBlock($data, $blockSize);
85 3
            $ret = null;
86 3
            if ($response->ok() && $response->json() != null) {
87
                $ret = $response->json();
88
            }
89 3
            if ($response->statusCode < 0) {
90 3
                list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($this->upToken);
91 3
                if ($err != null) {
92
                    return array(null, $err);
93
                }
94
95 3
                $upHostBackup = $this->config->getUpBackupHost($accessKey, $bucket);
96 3
                $this->host = $upHostBackup;
97 3
            }
98 3
            if ($response->needRetry() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
99 3
                $response = $this->makeBlock($data, $blockSize);
100 3
                $ret = $response->json();
101 3
            }
102
103 3
            if (!$response->ok() || !isset($ret['crc32']) || $crc != $ret['crc32']) {
104 3
                return array(null, new Error($this->currentUrl, $response));
105
            }
106
            array_push($this->contexts, $ret['ctx']);
107
            $uploaded += $blockSize;
108
        }
109
        return $this->makeFile($fname);
110
    }
111
112
    /**
113
     * 创建块
114
     */
115 3
    private function makeBlock($block, $blockSize)
116
    {
117 3
        $url = $this->host . '/mkblk/' . $blockSize;
118 3
        return $this->post($url, $block);
119
    }
120
121
    private function fileUrl($fname)
122
    {
123
        $url = $this->host . '/mkfile/' . $this->size;
124
        $url .= '/mimeType/' . \Qiniu\base64_urlSafeEncode($this->mime);
125
        if ($this->key != null) {
126
            $url .= '/key/' . \Qiniu\base64_urlSafeEncode($this->key);
127
        }
128
        $url .= '/fname/' . \Qiniu\base64_urlSafeEncode($fname);
129
        if (!empty($this->params)) {
130
            foreach ($this->params as $key => $value) {
131
                $val = \Qiniu\base64_urlSafeEncode($value);
132
                $url .= "/$key/$val";
133
            }
134
        }
135
        return $url;
136
    }
137
138
    /**
139
     * 创建文件
140
     */
141
    private function makeFile($fname)
142
    {
143
        $url = $this->fileUrl($fname);
144
        $body = implode(',', $this->contexts);
145
        $response = $this->post($url, $body);
146
        if ($response->needRetry()) {
147
            $response = $this->post($url, $body);
148
        }
149
        if (!$response->ok()) {
150
            return array(null, new Error($this->currentUrl, $response));
151
        }
152
        return array($response->json(), null);
153
    }
154
155 3
    private function post($url, $data)
156
    {
157 3
        $this->currentUrl = $url;
158 3
        $headers = array('Authorization' => 'UpToken ' . $this->upToken);
159 3
        return Client::post($url, $data, $headers);
160
    }
161
162 3
    private function blockSize($uploaded)
163
    {
164 3
        if ($this->size < $uploaded + Config::BLOCK_SIZE) {
165
            return $this->size - $uploaded;
166
        }
167 3
        return Config::BLOCK_SIZE;
168
    }
169
}
170