Completed
Push — master ( 464f3e...afe7d8 )
by r
13s queued 10s
created

FormUploader::put()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.7999

Importance

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