Passed
Pull Request — master (#369)
by
unknown
21:13
created

FormUploader::put()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 40
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9.8821

Importance

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