Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

two features: 1) add "prefix" to file query 2) add "listVersions" to list all versions of file #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ public function listFiles(array $options)
if (!isset($options['BucketId']) && isset($options['BucketName'])) {
$options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
}
if(!isset($options['Prefix'])){
$options['Prefix'] = NULL;
}

if ($fileName) {
$nextFileName = $fileName;
Expand All @@ -293,6 +296,66 @@ public function listFiles(array $options)
'bucketId' => $options['BucketId'],
'startFileName' => $nextFileName,
'maxFileCount' => $maxFileCount,
'prefix' => $options['Prefix'],
]
]);

foreach ($response['files'] as $file) {
// if we have a file name set, only retrieve information if the file name matches
if (!$fileName || ($fileName === $file['fileName'])) {
$files[] = new File($file['fileId'], $file['fileName'], null, $file['size']);
}
}

if ($fileName || $response['nextFileName'] === null) {
// We've got all the files - break out of loop.
break;
}

$nextFileName = $response['nextFileName'];
}

return $files;
}

/**
* Lists all of the versions of all of the files contained in one bucket, in alphabetical order by file name, and by reverse of date/time uploaded for versions of files with the same name.
*
* @param array $options
* @return array
*/
public function listVersions(array $options)
{
// if FileName is set, we only attempt to retrieve information about that single file.
$fileName = !empty($options['FileName']) ? $options['FileName'] : null;

$nextFileName = null;
$maxFileCount = 1000;
$files = [];

if (!isset($options['BucketId']) && isset($options['BucketName'])) {
$options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
}
if(!isset($options['Prefix'])){
$options['Prefix'] = NULL;
}

if ($fileName) {
$nextFileName = $fileName;
$maxFileCount = 1;
}

// B2 returns, at most, 1000 files per "page". Loop through the pages and compile an array of File objects.
while (true) {
$response = $this->client->request('POST', $this->apiUrl.'/b2_list_file_versions', [
'headers' => [
'Authorization' => $this->authToken
],
'json' => [
'bucketId' => $options['BucketId'],
'startFileName' => $nextFileName,
'maxFileCount' => $maxFileCount,
'prefix' => $options['Prefix'],
]
]);

Expand Down