Completed
Push — master ( 628b41...1577f3 )
by Bai
44:54 queued 33:39
created

UploadManager   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 81.97%

Importance

Changes 0
Metric Value
dl 0
loc 131
ccs 50
cts 61
cp 0.8197
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A put() 0 20 1
A putFile() 0 46 4
A trimParams() 0 15 6
1
<?php
2
namespace Qiniu\Storage;
3
4
use Qiniu\Config;
5
use Qiniu\Http\HttpClient;
6
use Qiniu\Storage\ResumeUploader;
7
use Qiniu\Storage\FormUploader;
8
9
/**
10
 * 主要涉及了资源上传接口的实现
11
 *
12
 * @link http://developer.qiniu.com/docs/v6/api/reference/up/
13
 */
14
final class UploadManager
15
{
16
    private $config;
17
18 12
    public function __construct(Config $config = null)
19
    {
20 12
        if ($config === null) {
21 9
            $config = new Config();
22 9
        }
23 12
        $this->config = $config;
24 12
    }
25
26
    /**
27
     * 上传二进制流到七牛
28
     *
29
     * @param $upToken    上传凭证
30
     * @param $key        上传文件名
31
     * @param $data       上传二进制流
32
     * @param $params     自定义变量,规格参考
33
     *                    http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
34
     * @param $mime       上传数据的mimeType
35
     * @param $checkCrc   是否校验crc32
36
     *
37
     * @return array    包含已上传文件的信息,类似:
38
     *                                              [
39
     *                                                  "hash" => "<Hash string>",
40
     *                                                  "key" => "<Key string>"
41
     *                                              ]
42
     */
43 3
    public function put(
44
        $upToken,
45
        $key,
46
        $data,
47
        $params = null,
48
        $mime = 'application/octet-stream',
49
        $fname = null
50
    ) {
51
    
52 3
        $params = self::trimParams($params);
53 3
        return FormUploader::put(
54 3
            $upToken,
55 3
            $key,
56 3
            $data,
57 3
            $this->config,
58 3
            $params,
59 3
            $mime,
60
            $fname
61 3
        );
62
    }
63
64
65
    /**
66
     * 上传文件到七牛
67
     *
68
     * @param $upToken    上传凭证
69
     * @param $key        上传文件名
70
     * @param $filePath   上传文件的路径
71
     * @param $params     自定义变量,规格参考
72
     *                    http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
73
     * @param $mime       上传数据的mimeType
74
     * @param $checkCrc   是否校验crc32
75
     *
76
     * @return array    包含已上传文件的信息,类似:
77
     *                                              [
78
     *                                                  "hash" => "<Hash string>",
79
     *                                                  "key" => "<Key string>"
80
     *                                              ]
81
     */
82 9
    public function putFile(
83
        $upToken,
84
        $key,
85
        $filePath,
86
        $params = null,
87
        $mime = 'application/octet-stream',
88
        $checkCrc = false
0 ignored issues
show
Unused Code introduced by
The parameter $checkCrc is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    ) {
90
    
91 9
        $file = fopen($filePath, 'rb');
92 9
        if ($file === false) {
93
            throw new \Exception("file can not open", 1);
94
        }
95 9
        $params = self::trimParams($params);
96 9
        $stat = fstat($file);
97 9
        $size = $stat['size'];
98 9
        if ($size <= Config::BLOCK_SIZE) {
99 3
            $data = fread($file, $size);
100 3
            fclose($file);
101 3
            if ($data === false) {
102
                throw new \Exception("file can not read", 1);
103
            }
104 3
            return FormUploader::put(
105 3
                $upToken,
106 3
                $key,
107 3
                $data,
108 3
                $this->config,
109 3
                $params,
110 3
                $mime,
111 3
                basename($filePath)
112 3
            );
113
        }
114
115 6
        $up = new ResumeUploader(
116 6
            $upToken,
117 6
            $key,
118 6
            $file,
119 6
            $size,
120 6
            $params,
121 6
            $mime,
122 6
            $this->config
123 6
        );
124 6
        $ret = $up->upload(basename($filePath));
125 6
        fclose($file);
126 6
        return $ret;
127
    }
128
129 12
    public static function trimParams($params)
130
    {
131 12
        if ($params === null) {
132 12
            return null;
133
        }
134
        $ret = array();
135
        foreach ($params as $k => $v) {
136
            $pos1 = strpos($k, 'x:');
137
            $pos2 = strpos($k, 'x-qn-meta-');
138
            if (($pos1 === 0 || $pos2 === 0) && !empty($v)) {
139
                $ret[$k] = $v;
140
            }
141
        }
142
        return $ret;
143
    }
144
}
145