Completed
Push — master ( 79290f...5ae020 )
by
unknown
12s
created

UploadManager::put()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
nc 1
nop 6
dl 0
loc 20
ccs 11
cts 11
cp 1
crap 1
rs 9.4285
c 1
b 0
f 0
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
        $checkCrc = false
50
    ) {
51 3
        $params = self::trimParams($params);
52 3
        return FormUploader::put(
53 3
            $upToken,
54 3
            $key,
55 3
            $data,
56 3
            $this->config,
57 3
            $params,
58 3
            $mime,
59 3
            $checkCrc,
60
            $filePath
0 ignored issues
show
Bug introduced by
The variable $filePath does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
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
89
    ) {
90 9
        $file = fopen($filePath, 'rb');
91 9
        if ($file === false) {
92
            throw new \Exception("file can not open", 1);
93
        }
94 9
        $params = self::trimParams($params);
95 9
        $stat = fstat($file);
96 9
        $size = $stat['size'];
97 9
        if ($size <= Config::BLOCK_SIZE) {
98 3
            $data = fread($file, $size);
99 3
            fclose($file);
100 3
            if ($data === false) {
101
                throw new \Exception("file can not read", 1);
102
            }
103 3
            return FormUploader::put(
104 3
                $upToken,
105 3
                $key,
106 3
                $data,
107 3
                $this->config,
108 3
                $params,
109 3
                $mime,
110 3
                $checkCrc,
111
                $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 3
        $ret = $up->upload($filePath);
125 3
        fclose($file);
126 3
        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
            $pos = strpos($k, 'x:');
137
            if ($pos === 0 && !empty($v)) {
138
                $ret[$k] = $v;
139
            }
140
        }
141
        return $ret;
142
    }
143
}
144