Completed
Push — master ( 652ef6...d33d89 )
by Bai
10:23
created

FormUploader::put()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.8

Importance

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