Skip to content

Commit f224215

Browse files
committed
added tests for some upload files util methods
1 parent db6d8ab commit f224215

File tree

7 files changed

+273
-101
lines changed

7 files changed

+273
-101
lines changed

README.md

Lines changed: 58 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,56 +31,68 @@ $this->b2Client = new BackblazeClient($b2_configs);
3131

3232
* Uploading small files.
3333
```php
34-
// Steps
35-
// Authorize requests
36-
// Get the upload url + the authorizationToken
37-
// Upload the file (b2_upload_file)
38-
$b2_auth = $this->b2Client->b2AuthorizeAccount();
39-
// Required from b2_auth
40-
$auth_token = $b2_auth->authorizationToken;
41-
$api_url = $b2_auth->apiUrl;
42-
43-
$file_name = '51863901_1591128994364639_5533062884642328214_n.jpg';
44-
$key = 'tasks/'. $file_name;
45-
// Assuming your files are stored in the '/uploads/', change to fit your folders.
46-
$local_file = dirname(__DIR__, 1) . '/uploads/'. $file_name;
47-
48-
$upload_details = $this->b2Client->b2GetUploadUrl($api_url, $auth_token);
49-
$upload_file = $this->b2Client->b2UploadFile($key, $local_file, $upload_details->uploadUrl, $upload_details->authorizationToken);
50-
var_dump($upload_details);
51-
var_dump($upload_file);
52-
die('HERE TOO');
34+
// Steps
35+
36+
// Authorize requests
37+
38+
// Get the upload url + the authorizationToken
39+
40+
// Upload the file (b2_upload_file)
41+
42+
$b2_auth = $this->b2Client->b2AuthorizeAccount();
43+
44+
// Required from b2_auth
45+
46+
$auth_token = $b2_auth->authorizationToken;
47+
48+
$api_url = $b2_auth->apiUrl;
49+
50+
51+
$file_name = '51863901_1591128994364639_5533062884642328214_n.jpg';
52+
53+
$key = 'a-file-folder/'. $file_name;
54+
// Assuming your files are stored in the '/uploads/', change to fit your folders.
55+
56+
$local_file = dirname(__DIR__, 1) . '/uploads/'. $file_name;
57+
58+
59+
$upload_details = $this->b2Client->b2GetUploadUrl($api_url, $auth_token);
60+
61+
$upload_file = $this->b2Client->b2UploadFile($key, $local_file, $upload_details->uploadUrl, $upload_details->authorizationToken);
62+
63+
var_dump($upload_details);
64+
65+
var_dump($upload_file);
66+
67+
die('HERE TOO');
5368
```
5469

5570

5671
* Uploading large files (multipart).
5772
```php
58-
// Steps
59-
//
60-
//
61-
//
62-
$b2_auth = $this->b2Client->b2AuthorizeAccount();
63-
// Required from b2_auth
64-
$auth_token = $b2_auth->authorizationToken;
65-
$api_url = $b2_auth->apiUrl;
66-
67-
$file_name = 'BULLET TRAIN - Official Trailer (HD).mp4';
68-
$key = 'tasks/'. $file_name;
69-
// Assuming your files are stored in the '/uploads/', change to fit your folders.
70-
$local_file = dirname(__DIR__, 1) . '/uploads/'. $file_name;
71-
72-
$start_upload_file = $this->b2Client->b2StartLargeFile($api_url, $auth_token, $key);
73-
$fileId = $start_upload_file->fileId;
74-
75-
// From here, you can save the values required in b2UploadChunks($local_file, $auth_token, $api_url, $fileId);
76-
// below, and later fire that action via a cron job, to make content editing easier (take less time) maybe.
77-
78-
// upload chuncks
79-
$large_upload_res = $this->b2Client->b2UploadChunks($local_file, $auth_token, $api_url, $fileId);
80-
// Delete local file maybe
81-
// unlink( $local_file );
82-
83-
var_dump($large_upload_res);
84-
die('DONE for a large file');
73+
// Steps
74+
//
75+
//
76+
//
77+
$b2_auth = $this->b2Client->b2AuthorizeAccount();
78+
// Required from b2_auth
79+
$auth_token = $b2_auth->authorizationToken;
80+
$api_url = $b2_auth->apiUrl;
81+
82+
$file_name = 'BULLET TRAIN - Official Trailer (HD).mp4';
83+
$key = 'a-folder/'. $file_name;
84+
// Assuming your files are stored in the '/uploads/', change to fit your folders.
85+
$local_file = dirname(__DIR__, 1) . '/uploads/'. $file_name;
86+
87+
$start_upload_file = $this->b2Client->b2StartLargeFile($api_url, $auth_token, $key);
88+
$fileId = $start_upload_file->fileId// From here, you can save the values required in b2UploadChunks($local_file, $auth_token, $api_url, $fileId);
89+
// below, and later fire that action via a cron job, to make content editing easier (take less time) maybe.
90+
91+
// upload chunks
92+
$large_upload_res = $this->b2Client->b2UploadChunks($local_file, $auth_token, $api_url, $fileId);
93+
// Delete local file maybe
94+
// unlink( $local_file );
95+
var_dump($large_upload_res);
96+
die('DONE for a large file');
8597

8698
```

composer.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
"Flaircore\\Backblaze\\": "src/"
1414
}
1515
},
16+
"autoload-dev": {
17+
"psr-4": {
18+
"Flaircore\\Backblaze\\Tests": "tests/"
19+
}
20+
},
1621
"authors": [
1722
{
1823
"name": "Nicholas Njogu",
@@ -25,6 +30,7 @@
2530
"ext-json": "*"
2631
},
2732
"require-dev": {
28-
"monolog/monolog": "^3.3"
33+
"monolog/monolog": "^3.3",
34+
"phpunit/phpunit": "^10.0"
2935
}
3036
}

example.phpunit.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit colors="true" bootstrap="vendor/autoload.php"
3+
cacheResult="false"
4+
>
5+
<testsuites>
6+
<testsuite name="Unit Tests">
7+
<directory>tests/Unit</directory>
8+
</testsuite>
9+
</testsuites>
10+
<php>
11+
<ini name="display_errors" value="On" />
12+
<ini name="display_warnings" value="On" />
13+
<ini name="display_startup_errors" value="On" />
14+
</php>
15+
</phpunit>

src/Clients/BackblazeClient.php

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ final public function b2ListUnfinishedLargeFiles($api_url, $account_auth_token){
5050
* @throws \Exception
5151
*/
5252
final public function b2AuthorizeAccount(){
53-
$application_key_id = B2_API_KEY;
54-
$application_key = B2_API_SECRET;
53+
$application_key_id = $this->b2Configs['B2_API_KEY'];
54+
$application_key = $this->b2Configs['B2_API_SECRET'];
5555
$credentials = base64_encode($application_key_id . ":" . $application_key);
5656
$url = self::BASE_URL ."b2_authorize_account";
5757

@@ -78,51 +78,6 @@ final public function b2AuthorizeAccount(){
7878
}
7979
}
8080

81-
/**
82-
* @param $api_url
83-
* @param $auth_token
84-
* @param $key
85-
* The file name of the file to be uploaded
86-
*
87-
* @return mixed
88-
* @throws \Exception
89-
*/
90-
final public function b2StartLargeFile($api_url, $auth_token, $key) {
91-
// The content type of the file. See b2_start_large_file documentation for more information.
92-
$content_type = "b2/x-auto";
93-
94-
// Provided by b2_create_bucket, b2_list_buckets
95-
$bucket_id = $this->b2Configs['B2_BUCKET_ID'];
96-
97-
// Construct the JSON to post
98-
$data = array("fileName" => $key, "bucketId" => $bucket_id, "contentType" => $content_type);
99-
$post_fields = json_encode($data);
100-
$headers = [
101-
'Accept' => 'application/json',
102-
'Authorization' => [$auth_token]
103-
];
104-
105-
try {
106-
$client = $this->client();
107-
$res = $client->postAsync($api_url . "/b2api/v2/b2_start_large_file", [
108-
'headers' => $headers,
109-
'body' => $post_fields,
110-
]
111-
)->wait();
112-
113-
if ($res->getStatusCode() == 200) {
114-
115-
return json_decode($res->getBody());
116-
} else {
117-
throw new \Exception("Error making request ". $res->getBody());
118-
}
119-
120-
} catch (\Throwable $ex) {
121-
throw new \Exception("Error making request ". $ex->getMessage());
122-
}
123-
124-
}
125-
12681
/**
12782
* Gets an URL to use for uploading files.
12883
* https://www.backblaze.com/b2/docs/b2_get_upload_url.html
@@ -208,7 +163,7 @@ final public function b2GetUploadPartUrl($api_url, $auth_token, $file_id) {
208163
*
209164
* @return mixed
210165
*/
211-
final public function b2UploadFile($file_name, $local_file, $upload_url, $upload_auth_token, ) {
166+
final public function b2UploadFile($file_name, $local_file, $upload_url, $upload_auth_token ) {
212167

213168
$client = $this->client();
214169

@@ -241,6 +196,56 @@ final public function b2UploadFile($file_name, $local_file, $upload_url, $upload
241196

242197
}
243198

199+
/**
200+
* @param $api_url
201+
* @param $auth_token
202+
* @param $key
203+
* The file name of the file to be uploaded
204+
*
205+
* @return mixed
206+
* @throws \Exception
207+
*/
208+
final public function b2StartLargeFile($api_url, $auth_token, $key) {
209+
// The content type of the file. See b2_start_large_file documentation for more information.
210+
$content_type = "b2/x-auto";
211+
212+
// Provided by b2_create_bucket, b2_list_buckets
213+
$bucket_id = $this->b2Configs['B2_BUCKET_ID'];
214+
215+
// Construct the JSON to post
216+
$data = [
217+
"fileName" => $key,
218+
"bucketId" => $bucket_id,
219+
"contentType" => $content_type
220+
];
221+
222+
$post_fields = json_encode($data);
223+
$headers = [
224+
'Accept' => 'application/json',
225+
'Authorization' => [$auth_token]
226+
];
227+
228+
try {
229+
$client = $this->client();
230+
$res = $client->postAsync($api_url . "/b2api/v2/b2_start_large_file", [
231+
'headers' => $headers,
232+
'body' => $post_fields,
233+
]
234+
)->wait();
235+
236+
if ($res->getStatusCode() == 200) {
237+
238+
return json_decode($res->getBody());
239+
} else {
240+
throw new \Exception("Error making request ". $res->getBody());
241+
}
242+
243+
} catch (\Throwable $ex) {
244+
throw new \Exception("Error making request ". $ex->getMessage());
245+
}
246+
247+
}
248+
244249
/**
245250
* Uploads chucks as well as finish the upload.
246251
* https://www.backblaze.com/b2/docs/b2_upload_part.html
@@ -331,10 +336,10 @@ final public function b2UploadChunks($local_file, $auth_token, $api_url, $file_i
331336

332337
// Finish the upload
333338

334-
$data = array(
339+
$data = [
335340
"fileId" => $file_id,
336341
"partSha1Array" => $sha1_of_parts
337-
);
342+
];
338343

339344
$headers = [
340345
'Accept' => 'application/json',
@@ -364,4 +369,5 @@ private function curlReadFile($curl_rsrc, $file_pointer, $length) {
364369
return fread($file_pointer, $length);
365370
}
366371

372+
367373
}

src/Clients/HttpClient.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
trait HttpClient {
1313
private $MAX_RETRIES = 5;
1414

15-
private $BASE_URL = 'https://api.backblazeb2.com/b2api/v2/';
16-
1715
/**
1816
* @var Client
1917
*/
@@ -46,8 +44,8 @@ public function __construct() {
4644
$stack->push(Middleware::retry($decider, $delay));
4745

4846
$this->httpClient = new Client([
49-
'base_uri' => 'https://api.backblazeb2.com/b2api/v2/',
50-
//'handler' => $stack,
47+
//'base_uri' => 'https://api.backblazeb2.com/b2api/v2/',
48+
'handler' => $stack,
5149
]);
5250
}
5351

0 commit comments

Comments
 (0)