Completed
Pull Request — master (#346)
by huang
21:21
created

FormUploader::putFile()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 37
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.8395

Importance

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