Completed
Pull Request — master (#293)
by
unknown
10:51 queued 09:27
created

FormUploader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 60.42%

Importance

Changes 0
Metric Value
dl 0
loc 132
ccs 29
cts 48
cp 0.6042
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createFile() 0 16 3
B put() 0 38 6
B putFile() 0 38 6
1
<?php
2
namespace Qiniu\Storage;
3
4
use Qiniu\Http\Client;
5
use Qiniu\Http\Error;
6
7
final class FormUploader
8
{
9
10
    /**
11
     * 上传二进制流到七牛, 内部使用
12
     *
13
     * @param $upToken    上传凭证
14
     * @param $key        上传文件名
15
     * @param $data       上传二进制流
16
     * @param $config     上传配置
17
     * @param $params     自定义变量,规格参考
18
     *                    http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
19
     * @param $mime       上传数据的mimeType
20
     *
21
     * @return array    包含已上传文件的信息,类似:
22
     *                                              [
23
     *                                                  "hash" => "<Hash string>",
24
     *                                                  "key" => "<Key string>"
25
     *                                              ]
26
     */
27 8
    public static function put(
28
        $upToken,
29
        $key,
30
        $data,
31
        $config,
32
        $params,
33
        $mime,
34
        $fname,
35
        $checkCrc
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...
36
    ) {
37 6
        $fields = array('token' => $upToken);
38 6
        if ($key === null) {
39
        } else {
40 6
            $fields['key'] = $key;
41
        }
42
43
        //enable crc32 check by default
44 6
        $fields['crc32'] = \Qiniu\crc32_data($data);
45
46 8
        if ($params) {
47
            foreach ($params as $k => $v) {
48
                $fields[$k] = $v;
49
            }
50
        }
51
52 6
        list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
53 6
        if ($err != null) {
54
            return array(null, $err);
55
        }
56
57 6
        $upHost = $config->getUpHost($accessKey, $bucket);
58
59
        $response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime);
60
        if (!$response->ok()) {
61
            return array(null, new Error($upHost, $response));
62
        }
63
        return array($response->json(), null);
64
    }
65
66
    /**
67
     * 上传文件到七牛,内部使用
68
     *
69
     * @param $upToken    上传凭证
70
     * @param $key        上传文件名
71
     * @param $filePath   上传文件的路径
72
     * @param $config     上传配置
73
     * @param $params     自定义变量,规格参考
74
     *                    http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar
75
     * @param $mime       上传数据的mimeType
76
     *
77
     * @return array    包含已上传文件的信息,类似:
78
     *                                              [
79
     *                                                  "hash" => "<Hash string>",
80
     *                                                  "key" => "<Key string>"
81
     *                                              ]
82
     */
83 3
    public static function putFile(
84
        $upToken,
85
        $key,
86
        $filePath,
87
        $config,
88
        $params,
89
        $mime
90
    ) {
91
92
93 3
        $fields = array('token' => $upToken, 'file' => self::createFile($filePath, $mime));
94 3
        if ($key !== null) {
95 3
            $fields['key'] = $key;
96 2
        }
97
98 3
        $fields['crc32'] = \Qiniu\crc32_file($filePath);
99
100 3
        if ($params) {
101
            foreach ($params as $k => $v) {
102
                $fields[$k] = $v;
103
            }
104
        }
105 3
        $fields['key'] = $key;
106 3
        $headers = array('Content-Type' => 'multipart/form-data');
107
108 3
        list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken);
109 3
        if ($err != null) {
110
            return array(null, $err);
111
        }
112
113 3
        $upHost = $config->getUpHost($accessKey, $bucket);
114
115
        $response = Client::post($upHost, $fields, $headers);
116
        if (!$response->ok()) {
117
            return array(null, new Error($upHost, $response));
118
        }
119
        return array($response->json(), null);
120
    }
121
122 3
    private static function createFile($filename, $mime)
123
    {
124
        // PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
125
        // See: https://wiki.php.net/rfc/curl-file-upload
126 3
        if (function_exists('curl_file_create')) {
127 2
            return curl_file_create($filename, $mime);
128
        }
129
130
        // Use the old style if using an older version of PHP
131 1
        $value = "@{$filename}";
132 1
        if (!empty($mime)) {
133 1
            $value .= ';type=' . $mime;
134 1
        }
135
136 1
        return $value;
137
    }
138
}
139