Completed
Push — master ( bfc0cc...f0e226 )
by sabaku
04:05
created

Uploader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 108
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A upload() 0 20 3
B pointUpload() 0 53 8
A needUseBlock() 0 10 4
1
<?php
2
namespace Upyun;
3
4
use Upyun\Api\Rest;
5
use Upyun\Api\Form;
6
use GuzzleHttp\Psr7;
7
8
class Uploader {
9
    /**
10
     * @var Config
11
     */
12
    protected $config;
13
14
    protected $useBlock = false;
15
16
17
    public function __construct(Config $config) {
18
        $this->config = $config;
19
    }
20
21
    public function upload($path, $file, $params, $withAsyncProcess) {
22
        $stream = Psr7\stream_for($file);
23
        $size = $stream->getSize();
24
        $useBlock = $this->needUseBlock($size);
25
26
        if ($withAsyncProcess) {
27
            $req = new Form($this->config);
28
            return $req->upload($path, $stream, $params);
29
        }
30
31
        if(! $useBlock) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
32
            $req = new Rest($this->config);
33
            return $req->request('PUT', $path)
34
                       ->withHeaders($params)
35
                       ->withFile($stream)
36
                       ->send();
37
        } else {
38
            return $this->pointUpload($path, $stream, $params);
39
        }
40
    }
41
42
    /**
43
     *  断点续传
44
     * @param $path
45
     * @param $stream
46
     * @param $params
47
     *
48
     * @return mixed|\Psr\Http\Message\ResponseInterface
49
     * @throws \Exception
50
     */
51
    private function pointUpload($path, $stream, $params) {
52
        $req = new Rest($this->config);
53
        $headers = array();
54
        if (is_array($params)) {
55
            foreach($params as $key => $val) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
56
                $headers['X-Upyun-Meta-' . $key] = $val;
57
            }
58
        }
59
        $res = $req->request('PUT', $path)
60
            ->withHeaders(array_merge(array(
61
                'X-Upyun-Multi-Stage' => 'initiate',
62
                'X-Upyun-Multi-Type' => Psr7\mimetype_from_filename($path),
63
                'X-Upyun-Multi-Length' => $stream->getSize(),
64
            ), $headers))
65
            ->send();
66
        if ($res->getStatusCode() !== 204) {
67
            throw new \Exception('init request failed when poinit upload!');
68
        }
69
70
        $init      = Util::getHeaderParams($res->getHeaders());
71
        $uuid      = $init['x-upyun-multi-uuid'];
72
        $blockSize = 1024 * 1024;
73
        $partId    = 0;
74
        do {
75
            $fileBlock = $stream->read($blockSize);
76
            $res = $req->request('PUT', $path)
77
                ->withHeaders(array(
78
                    'X-Upyun-Multi-Stage' => 'upload',
79
                    'X-Upyun-Multi-Uuid' => $uuid,
80
                    'X-Upyun-Part-Id' => $partId
81
                ))
82
                ->withFile(Psr7\stream_for($fileBlock))
83
                ->send();
84
85
            if ($res->getStatusCode() !== 204) {
86
                throw new \Exception('upload request failed when poinit upload!');
87
            }
88
            $data   = Util::getHeaderParams($res->getHeaders());
89
            $partId = $data['x-upyun-next-part-id'];
90
        } while($partId != -1);
0 ignored issues
show
Coding Style introduced by
Expected 1 space after WHILE keyword; 0 found
Loading history...
91
92
        $res = $req->request('PUT', $path)
93
            ->withHeaders(array(
94
                'X-Upyun-Multi-Uuid' => $uuid,
95
                'X-Upyun-Multi-Stage' => 'complete'
96
            ))
97
            ->send();
98
99
        if ($res->getStatusCode() != 204 && $res->getStatusCode() != 201) {
100
            throw new \Exception('end request failed when poinit upload!');
101
        }
102
        return $res;
103
    }
104
105
    private function needUseBlock($fileSize) {
106
        if($this->config->uploadType === 'BLOCK') {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
107
            return true;
108
        } else if($this->config->uploadType === 'AUTO' &&
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
109
                  $fileSize >= $this->config->sizeBoundary ) {
110
            return true;
111
        } else {
112
            return false;
113
        }
114
    }
115
}
116