Completed
Push — master ( 810079...4da4eb )
by Bai
10s
created

FormUploader::put()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 36
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 9.3554

Importance

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