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