Completed
Pull Request — master (#288)
by Bai
13:51
created

FormUploader   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 59.18%

Importance

Changes 0
Metric Value
dl 0
loc 133
ccs 29
cts 49
cp 0.5918
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 3

3 Methods

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