@@ -87,7 +87,6 @@ |
||
87 | 87 | * @param string $filePath The path to the local file |
88 | 88 | * @param string $fileName The name/path on B2 |
89 | 89 | * @param string $contentType |
90 | - * @param null $uploadUrl |
|
91 | 90 | * @return array |
92 | 91 | */ |
93 | 92 | public function uploadFile($bucketId, $filePath, $fileName, $contentType, $uploadUrlResponse = []) |
@@ -11,111 +11,111 @@ |
||
11 | 11 | class Files |
12 | 12 | { |
13 | 13 | |
14 | - /** |
|
15 | - * @var B2Client |
|
16 | - */ |
|
17 | - protected $B2Client; |
|
18 | - |
|
19 | - /** |
|
20 | - * Files constructor. |
|
21 | - * @param B2Client $B2Client |
|
22 | - */ |
|
23 | - public function __construct(B2Client $B2Client) |
|
24 | - { |
|
25 | - $this->B2Client = $B2Client; |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * @param $bucketId |
|
30 | - * @param $startFileName |
|
31 | - * @return mixed |
|
32 | - * @throws \Exception |
|
33 | - */ |
|
34 | - public function listFileNames($bucketId, $startFileName = null, $maxFileCount = 100) |
|
35 | - { |
|
36 | - return $this->B2Client->call('b2_list_file_names', 'POST', [ |
|
37 | - 'startFileName' => $startFileName, |
|
38 | - 'bucketId' => $bucketId, |
|
39 | - 'maxFileCount' => $maxFileCount |
|
40 | - ]); |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Lists all files in a bucket and starting filename. For very large buckets this may fail, use listFileNames() directly |
|
45 | - * |
|
46 | - * @param $bucketId |
|
47 | - * @param $startFileName |
|
48 | - * @return array |
|
49 | - */ |
|
50 | - public function listAllFileNames($bucketId, $startFileName) |
|
51 | - { |
|
52 | - |
|
53 | - $allresults = []; |
|
54 | - $result = $this->listFileNames($bucketId, $startFileName); |
|
55 | - $allresults = array_merge($allresults, $result['responseBody']['files']); |
|
56 | - |
|
57 | - if ($result['responseBody']['nextFileName'] !== null) { |
|
58 | - $allresults = array_merge($allresults, |
|
59 | - $this->listAllFileNames($bucketId, $result['responseBody']['nextFileName'])); |
|
60 | - } |
|
61 | - |
|
62 | - return $allresults; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @param $fileID |
|
67 | - * @return mixed |
|
68 | - * @throws \Exception |
|
69 | - */ |
|
70 | - public function getFileInfo($fileId) |
|
71 | - { |
|
72 | - return $this->B2Client->call('b2_get_file_info', 'POST', ['fileId' => $fileId]); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @param $bucketId |
|
77 | - * @return mixed |
|
78 | - * @throws \Exception |
|
79 | - */ |
|
80 | - public function getUploadUrl($bucketId) |
|
81 | - { |
|
82 | - return $this->B2Client->call('b2_get_upload_url', 'POST', ['bucketId' => $bucketId]); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @param $bucketId |
|
87 | - * @param string $filePath The path to the local file |
|
88 | - * @param string $fileName The name/path on B2 |
|
89 | - * @param string $contentType |
|
90 | - * @param null $uploadUrl |
|
91 | - * @return array |
|
92 | - */ |
|
93 | - public function uploadFile($bucketId, $filePath, $fileName, $contentType, $uploadUrlResponse = []) |
|
94 | - { |
|
95 | - |
|
96 | - if (!$uploadUrlResponse) { |
|
97 | - $uploadUrlResponse = $this->getUploadUrl($bucketId); |
|
98 | - } |
|
99 | - |
|
100 | - $handle = fopen($filePath, 'r'); |
|
101 | - $fileData = fread($handle, filesize($filePath)); |
|
102 | - $fileDataSha1 = sha1_file($filePath); |
|
103 | - |
|
104 | - return $this->B2Client->uploadData($fileData, $fileDataSha1, $fileName, $contentType, |
|
105 | - $uploadUrlResponse['uploadUrl'], |
|
106 | - $uploadUrlResponse['authorizationToken']); |
|
107 | - |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @param $bucketId |
|
112 | - * @param $fileName |
|
113 | - * @return mixed |
|
114 | - * @throws \Exception |
|
115 | - */ |
|
116 | - public function downloadFileByName($bucketName, $fileName) |
|
117 | - { |
|
118 | - return $this->B2Client->downloadFileByName($bucketName . '/' . $fileName); |
|
119 | - } |
|
14 | + /** |
|
15 | + * @var B2Client |
|
16 | + */ |
|
17 | + protected $B2Client; |
|
18 | + |
|
19 | + /** |
|
20 | + * Files constructor. |
|
21 | + * @param B2Client $B2Client |
|
22 | + */ |
|
23 | + public function __construct(B2Client $B2Client) |
|
24 | + { |
|
25 | + $this->B2Client = $B2Client; |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * @param $bucketId |
|
30 | + * @param $startFileName |
|
31 | + * @return mixed |
|
32 | + * @throws \Exception |
|
33 | + */ |
|
34 | + public function listFileNames($bucketId, $startFileName = null, $maxFileCount = 100) |
|
35 | + { |
|
36 | + return $this->B2Client->call('b2_list_file_names', 'POST', [ |
|
37 | + 'startFileName' => $startFileName, |
|
38 | + 'bucketId' => $bucketId, |
|
39 | + 'maxFileCount' => $maxFileCount |
|
40 | + ]); |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Lists all files in a bucket and starting filename. For very large buckets this may fail, use listFileNames() directly |
|
45 | + * |
|
46 | + * @param $bucketId |
|
47 | + * @param $startFileName |
|
48 | + * @return array |
|
49 | + */ |
|
50 | + public function listAllFileNames($bucketId, $startFileName) |
|
51 | + { |
|
52 | + |
|
53 | + $allresults = []; |
|
54 | + $result = $this->listFileNames($bucketId, $startFileName); |
|
55 | + $allresults = array_merge($allresults, $result['responseBody']['files']); |
|
56 | + |
|
57 | + if ($result['responseBody']['nextFileName'] !== null) { |
|
58 | + $allresults = array_merge($allresults, |
|
59 | + $this->listAllFileNames($bucketId, $result['responseBody']['nextFileName'])); |
|
60 | + } |
|
61 | + |
|
62 | + return $allresults; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @param $fileID |
|
67 | + * @return mixed |
|
68 | + * @throws \Exception |
|
69 | + */ |
|
70 | + public function getFileInfo($fileId) |
|
71 | + { |
|
72 | + return $this->B2Client->call('b2_get_file_info', 'POST', ['fileId' => $fileId]); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @param $bucketId |
|
77 | + * @return mixed |
|
78 | + * @throws \Exception |
|
79 | + */ |
|
80 | + public function getUploadUrl($bucketId) |
|
81 | + { |
|
82 | + return $this->B2Client->call('b2_get_upload_url', 'POST', ['bucketId' => $bucketId]); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @param $bucketId |
|
87 | + * @param string $filePath The path to the local file |
|
88 | + * @param string $fileName The name/path on B2 |
|
89 | + * @param string $contentType |
|
90 | + * @param null $uploadUrl |
|
91 | + * @return array |
|
92 | + */ |
|
93 | + public function uploadFile($bucketId, $filePath, $fileName, $contentType, $uploadUrlResponse = []) |
|
94 | + { |
|
95 | + |
|
96 | + if (!$uploadUrlResponse) { |
|
97 | + $uploadUrlResponse = $this->getUploadUrl($bucketId); |
|
98 | + } |
|
99 | + |
|
100 | + $handle = fopen($filePath, 'r'); |
|
101 | + $fileData = fread($handle, filesize($filePath)); |
|
102 | + $fileDataSha1 = sha1_file($filePath); |
|
103 | + |
|
104 | + return $this->B2Client->uploadData($fileData, $fileDataSha1, $fileName, $contentType, |
|
105 | + $uploadUrlResponse['uploadUrl'], |
|
106 | + $uploadUrlResponse['authorizationToken']); |
|
107 | + |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @param $bucketId |
|
112 | + * @param $fileName |
|
113 | + * @return mixed |
|
114 | + * @throws \Exception |
|
115 | + */ |
|
116 | + public function downloadFileByName($bucketName, $fileName) |
|
117 | + { |
|
118 | + return $this->B2Client->downloadFileByName($bucketName . '/' . $fileName); |
|
119 | + } |
|
120 | 120 | |
121 | 121 | } |
@@ -8,223 +8,223 @@ |
||
8 | 8 | class B2Client |
9 | 9 | { |
10 | 10 | |
11 | - /** |
|
12 | - * @var string |
|
13 | - */ |
|
14 | - protected $accountId; |
|
15 | - |
|
16 | - /** |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - protected $applicationKey; |
|
20 | - |
|
21 | - /** |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $apiUrl; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $authorizationToken; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var string |
|
33 | - */ |
|
34 | - protected $downloadUrl; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var CurlRequest |
|
38 | - */ |
|
39 | - protected $CurlRequest; |
|
40 | - |
|
41 | - /** |
|
42 | - * @var Files |
|
43 | - */ |
|
44 | - public $Files; |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * B2Client constructor. |
|
49 | - * @param string $accountId |
|
50 | - * @param string $applicationKey |
|
51 | - */ |
|
52 | - public function __construct($accountId, $applicationKey, CurlRequest $curlRequest = null) |
|
53 | - { |
|
54 | - |
|
55 | - if (!$curlRequest) { |
|
56 | - $this->CurlRequest = new CurlRequest(); |
|
57 | - } else { |
|
58 | - $this->CurlRequest = $curlRequest; |
|
59 | - } |
|
60 | - |
|
61 | - $this->accountId = $accountId; |
|
62 | - $this->applicationKey = $applicationKey; |
|
63 | - $this->Files = new Files($this); |
|
64 | - |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @param array $result |
|
69 | - */ |
|
70 | - public function setToken($result) |
|
71 | - { |
|
72 | - $this->authorizationToken = $result['authorizationToken']; |
|
73 | - $this->apiUrl = $result['apiUrl']; |
|
74 | - $this->downloadUrl = $result['downloadUrl']; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * |
|
79 | - */ |
|
80 | - public function requestToken() |
|
81 | - { |
|
82 | - |
|
83 | - $results = $this->curl('https://api.backblaze.com/b2api/v1/b2_authorize_account', 'GET', [ |
|
84 | - $this->buildBasicAuthHeader() |
|
85 | - ]); |
|
86 | - |
|
87 | - if ($results['statusCode'] === 200) { |
|
88 | - $this->setToken($results['responseBody']); |
|
89 | - } else { |
|
90 | - throw new \RuntimeException('Failed to get token: ' . $results['responseBody']['message']); |
|
91 | - } |
|
92 | - |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @param $endpoint |
|
97 | - * @param $method |
|
98 | - * @param array $data |
|
99 | - * @return mixed |
|
100 | - * @throws \Exception |
|
101 | - */ |
|
102 | - public function call($endpoint, $method, $data = []) |
|
103 | - { |
|
104 | - |
|
105 | - if (empty($this->authorizationToken)) { |
|
106 | - throw new \Exception('You must set or generate a token'); |
|
107 | - } |
|
108 | - |
|
109 | - $headers = [ |
|
110 | - $this->buildTokenAuthHeader() |
|
111 | - ]; |
|
112 | - |
|
113 | - $result = $this->curl($this->apiUrl . '/b2api/v1/' . $endpoint, $method, $headers, $data); |
|
114 | - |
|
115 | - if ($result['statusCode'] >= 200 && $result['statusCode'] < 300) { |
|
116 | - return $result['responseBody']; |
|
117 | - } |
|
118 | - |
|
119 | - if ($result['statusCode'] >= 400) { |
|
120 | - throw new \RuntimeException('Error ' . $result['statusCode'] . ' - ' . $result['responseBody']['message']); |
|
121 | - } |
|
122 | - |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param $uri |
|
127 | - * @param string $method |
|
128 | - * @param array $headers |
|
129 | - * @param array $body |
|
130 | - * @return array |
|
131 | - * @throws \Exception |
|
132 | - */ |
|
133 | - public function curl($uri, $method = 'GET', $headers = [], $body = []) |
|
134 | - { |
|
135 | - |
|
136 | - $this->CurlRequest->setOption(CURLOPT_URL, $uri); |
|
137 | - $this->CurlRequest->setOption(CURLOPT_CUSTOMREQUEST, $method); |
|
138 | - $this->CurlRequest->setOption(CURLOPT_RETURNTRANSFER, 1); |
|
139 | - |
|
140 | - $headers[] = 'Content-Type: application/json'; |
|
141 | - $headers[] = "Accept: application/json"; |
|
142 | - |
|
143 | - $this->CurlRequest->setOption(CURLOPT_POST, 1); |
|
144 | - $body = json_encode($body); |
|
145 | - $this->CurlRequest->setOption(CURLOPT_POSTFIELDS, $body); |
|
146 | - $this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers); |
|
147 | - |
|
148 | - $resp = $this->CurlRequest->execute(); |
|
149 | - if ($this->CurlRequest->getErrorNo() !== 0) { |
|
150 | - throw new \RuntimeException('curl error ' . $this->CurlRequest->getError() . '" - Code: ' . $this->CurlRequest->getErrorNo()); |
|
151 | - } else { |
|
152 | - return [ |
|
153 | - 'statusCode' => $this->CurlRequest->getInfo(CURLINFO_HTTP_CODE), |
|
154 | - 'responseBody' => json_decode($resp, true) |
|
155 | - ]; |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - public function buildBasicAuthHeader() |
|
163 | - { |
|
164 | - return 'Authorization: Basic ' . base64_encode($this->accountId . ':' . $this->applicationKey); |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * @return string |
|
169 | - */ |
|
170 | - public function buildTokenAuthHeader() |
|
171 | - { |
|
172 | - return 'Authorization: ' . $this->authorizationToken; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * @param $data |
|
177 | - * @param $sha1 |
|
178 | - * @param $fileName |
|
179 | - * @param $url |
|
180 | - * @param $token |
|
181 | - */ |
|
182 | - public function uploadData($fileData, $fileDataSha1, $fileName, $contentType, $uploadUrl, $uploadToken) |
|
183 | - { |
|
184 | - $headers = []; |
|
185 | - $headers[] = "Authorization: " . $uploadToken; |
|
186 | - $headers[] = "X-Bz-File-Name: " . $fileName; |
|
187 | - $headers[] = "Content-Type: " . $contentType; |
|
188 | - $headers[] = "X-Bz-Content-Sha1: " . $fileDataSha1; |
|
189 | - |
|
190 | - $this->CurlRequest->setOption(CURLOPT_URL, $uploadUrl); |
|
191 | - $this->CurlRequest->setOption(CURLOPT_POST, true); |
|
192 | - $this->CurlRequest->setOption(CURLOPT_POSTFIELDS, $fileData); |
|
193 | - $this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers); |
|
194 | - |
|
195 | - $resp = $this->CurlRequest->execute(); |
|
196 | - if ($this->CurlRequest->getErrorNo() !== 0) { |
|
197 | - throw new \RuntimeException('curl error ' . $this->CurlRequest->getError() . '" - Code: ' . $this->CurlRequest->getErrorNo()); |
|
198 | - } else { |
|
199 | - return [ |
|
200 | - 'statusCode' => $this->CurlRequest->getInfo(CURLINFO_HTTP_CODE), |
|
201 | - 'responseBody' => json_decode($resp, true) |
|
202 | - ]; |
|
203 | - } |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * @param $url |
|
208 | - */ |
|
209 | - public function downloadFileByName($uri) { |
|
210 | - |
|
211 | - $uri = $this->downloadUrl . "/file/" . $uri; |
|
212 | - $this->CurlRequest->setOption(CURLOPT_URL, $uri); |
|
213 | - $this->CurlRequest->setOption(CURLOPT_CUSTOMREQUEST, 'GET'); |
|
214 | - $this->CurlRequest->setOption(CURLOPT_RETURNTRANSFER, 1); |
|
215 | - |
|
216 | - $headers = [ |
|
217 | - $this->buildTokenAuthHeader() |
|
218 | - ]; |
|
219 | - |
|
220 | - $this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers); |
|
221 | - |
|
222 | - $resp = $this->CurlRequest->execute(); |
|
223 | - if ($this->CurlRequest->getErrorNo() !== 0) { |
|
224 | - throw new \RuntimeException('curl error ' . $this->CurlRequest->getError() . '" - Code: ' . $this->CurlRequest->getErrorNo()); |
|
225 | - } else { |
|
226 | - return $resp; |
|
227 | - } |
|
228 | - } |
|
11 | + /** |
|
12 | + * @var string |
|
13 | + */ |
|
14 | + protected $accountId; |
|
15 | + |
|
16 | + /** |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + protected $applicationKey; |
|
20 | + |
|
21 | + /** |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $apiUrl; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $authorizationToken; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var string |
|
33 | + */ |
|
34 | + protected $downloadUrl; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var CurlRequest |
|
38 | + */ |
|
39 | + protected $CurlRequest; |
|
40 | + |
|
41 | + /** |
|
42 | + * @var Files |
|
43 | + */ |
|
44 | + public $Files; |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * B2Client constructor. |
|
49 | + * @param string $accountId |
|
50 | + * @param string $applicationKey |
|
51 | + */ |
|
52 | + public function __construct($accountId, $applicationKey, CurlRequest $curlRequest = null) |
|
53 | + { |
|
54 | + |
|
55 | + if (!$curlRequest) { |
|
56 | + $this->CurlRequest = new CurlRequest(); |
|
57 | + } else { |
|
58 | + $this->CurlRequest = $curlRequest; |
|
59 | + } |
|
60 | + |
|
61 | + $this->accountId = $accountId; |
|
62 | + $this->applicationKey = $applicationKey; |
|
63 | + $this->Files = new Files($this); |
|
64 | + |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @param array $result |
|
69 | + */ |
|
70 | + public function setToken($result) |
|
71 | + { |
|
72 | + $this->authorizationToken = $result['authorizationToken']; |
|
73 | + $this->apiUrl = $result['apiUrl']; |
|
74 | + $this->downloadUrl = $result['downloadUrl']; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * |
|
79 | + */ |
|
80 | + public function requestToken() |
|
81 | + { |
|
82 | + |
|
83 | + $results = $this->curl('https://api.backblaze.com/b2api/v1/b2_authorize_account', 'GET', [ |
|
84 | + $this->buildBasicAuthHeader() |
|
85 | + ]); |
|
86 | + |
|
87 | + if ($results['statusCode'] === 200) { |
|
88 | + $this->setToken($results['responseBody']); |
|
89 | + } else { |
|
90 | + throw new \RuntimeException('Failed to get token: ' . $results['responseBody']['message']); |
|
91 | + } |
|
92 | + |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @param $endpoint |
|
97 | + * @param $method |
|
98 | + * @param array $data |
|
99 | + * @return mixed |
|
100 | + * @throws \Exception |
|
101 | + */ |
|
102 | + public function call($endpoint, $method, $data = []) |
|
103 | + { |
|
104 | + |
|
105 | + if (empty($this->authorizationToken)) { |
|
106 | + throw new \Exception('You must set or generate a token'); |
|
107 | + } |
|
108 | + |
|
109 | + $headers = [ |
|
110 | + $this->buildTokenAuthHeader() |
|
111 | + ]; |
|
112 | + |
|
113 | + $result = $this->curl($this->apiUrl . '/b2api/v1/' . $endpoint, $method, $headers, $data); |
|
114 | + |
|
115 | + if ($result['statusCode'] >= 200 && $result['statusCode'] < 300) { |
|
116 | + return $result['responseBody']; |
|
117 | + } |
|
118 | + |
|
119 | + if ($result['statusCode'] >= 400) { |
|
120 | + throw new \RuntimeException('Error ' . $result['statusCode'] . ' - ' . $result['responseBody']['message']); |
|
121 | + } |
|
122 | + |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param $uri |
|
127 | + * @param string $method |
|
128 | + * @param array $headers |
|
129 | + * @param array $body |
|
130 | + * @return array |
|
131 | + * @throws \Exception |
|
132 | + */ |
|
133 | + public function curl($uri, $method = 'GET', $headers = [], $body = []) |
|
134 | + { |
|
135 | + |
|
136 | + $this->CurlRequest->setOption(CURLOPT_URL, $uri); |
|
137 | + $this->CurlRequest->setOption(CURLOPT_CUSTOMREQUEST, $method); |
|
138 | + $this->CurlRequest->setOption(CURLOPT_RETURNTRANSFER, 1); |
|
139 | + |
|
140 | + $headers[] = 'Content-Type: application/json'; |
|
141 | + $headers[] = "Accept: application/json"; |
|
142 | + |
|
143 | + $this->CurlRequest->setOption(CURLOPT_POST, 1); |
|
144 | + $body = json_encode($body); |
|
145 | + $this->CurlRequest->setOption(CURLOPT_POSTFIELDS, $body); |
|
146 | + $this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers); |
|
147 | + |
|
148 | + $resp = $this->CurlRequest->execute(); |
|
149 | + if ($this->CurlRequest->getErrorNo() !== 0) { |
|
150 | + throw new \RuntimeException('curl error ' . $this->CurlRequest->getError() . '" - Code: ' . $this->CurlRequest->getErrorNo()); |
|
151 | + } else { |
|
152 | + return [ |
|
153 | + 'statusCode' => $this->CurlRequest->getInfo(CURLINFO_HTTP_CODE), |
|
154 | + 'responseBody' => json_decode($resp, true) |
|
155 | + ]; |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + public function buildBasicAuthHeader() |
|
163 | + { |
|
164 | + return 'Authorization: Basic ' . base64_encode($this->accountId . ':' . $this->applicationKey); |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * @return string |
|
169 | + */ |
|
170 | + public function buildTokenAuthHeader() |
|
171 | + { |
|
172 | + return 'Authorization: ' . $this->authorizationToken; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * @param $data |
|
177 | + * @param $sha1 |
|
178 | + * @param $fileName |
|
179 | + * @param $url |
|
180 | + * @param $token |
|
181 | + */ |
|
182 | + public function uploadData($fileData, $fileDataSha1, $fileName, $contentType, $uploadUrl, $uploadToken) |
|
183 | + { |
|
184 | + $headers = []; |
|
185 | + $headers[] = "Authorization: " . $uploadToken; |
|
186 | + $headers[] = "X-Bz-File-Name: " . $fileName; |
|
187 | + $headers[] = "Content-Type: " . $contentType; |
|
188 | + $headers[] = "X-Bz-Content-Sha1: " . $fileDataSha1; |
|
189 | + |
|
190 | + $this->CurlRequest->setOption(CURLOPT_URL, $uploadUrl); |
|
191 | + $this->CurlRequest->setOption(CURLOPT_POST, true); |
|
192 | + $this->CurlRequest->setOption(CURLOPT_POSTFIELDS, $fileData); |
|
193 | + $this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers); |
|
194 | + |
|
195 | + $resp = $this->CurlRequest->execute(); |
|
196 | + if ($this->CurlRequest->getErrorNo() !== 0) { |
|
197 | + throw new \RuntimeException('curl error ' . $this->CurlRequest->getError() . '" - Code: ' . $this->CurlRequest->getErrorNo()); |
|
198 | + } else { |
|
199 | + return [ |
|
200 | + 'statusCode' => $this->CurlRequest->getInfo(CURLINFO_HTTP_CODE), |
|
201 | + 'responseBody' => json_decode($resp, true) |
|
202 | + ]; |
|
203 | + } |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * @param $url |
|
208 | + */ |
|
209 | + public function downloadFileByName($uri) { |
|
210 | + |
|
211 | + $uri = $this->downloadUrl . "/file/" . $uri; |
|
212 | + $this->CurlRequest->setOption(CURLOPT_URL, $uri); |
|
213 | + $this->CurlRequest->setOption(CURLOPT_CUSTOMREQUEST, 'GET'); |
|
214 | + $this->CurlRequest->setOption(CURLOPT_RETURNTRANSFER, 1); |
|
215 | + |
|
216 | + $headers = [ |
|
217 | + $this->buildTokenAuthHeader() |
|
218 | + ]; |
|
219 | + |
|
220 | + $this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers); |
|
221 | + |
|
222 | + $resp = $this->CurlRequest->execute(); |
|
223 | + if ($this->CurlRequest->getErrorNo() !== 0) { |
|
224 | + throw new \RuntimeException('curl error ' . $this->CurlRequest->getError() . '" - Code: ' . $this->CurlRequest->getErrorNo()); |
|
225 | + } else { |
|
226 | + return $resp; |
|
227 | + } |
|
228 | + } |
|
229 | 229 | |
230 | 230 | } |