Skip to content

Commit

Permalink
Add metadata and optimize list files (#4)
Browse files Browse the repository at this point in the history
* Support all file metadata and implement fileExists

Add support for the missing metadata from the `getFile()` method:
uploadTimestamp, action and bucketId.

Optimize file list retrieval (`listFiles()`) to avoid fetching information
about _all_ the files to get information about a single one. `listFiles`
now support an optional parameter, `FileName`, which will only retrieve
information about that file if it exists. This speeds up the call to
`getFileIdFromBucketAndFileName`.

* Code style fix

* Add metadata to example response test as well
  • Loading branch information
matslindh authored and Chris White committed Jun 16, 2016
1 parent d4357fa commit ec5a083
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
40 changes: 35 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,22 @@ public function download(array $options)
*/
public function listFiles(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 ($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_names', [
Expand All @@ -282,15 +291,18 @@ public function listFiles(array $options)
'json' => [
'bucketId' => $options['BucketId'],
'startFileName' => $nextFileName,
'maxFileCount' => 1000
'maxFileCount' => $maxFileCount,
]
]);

foreach ($response['files'] as $file) {
$files[] = new File($file['fileId'], $file['fileName'], null, $file['size']);
// 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 ($response['nextFileName'] === null) {
if ($fileName || $response['nextFileName'] === null) {
// We've got all the files - break out of loop.
break;
}
Expand All @@ -301,6 +313,20 @@ public function listFiles(array $options)
return $files;
}

/**
* Test whether a file exists in B2 for the given bucket.
*
* @param array $options
* @return boolean
*/
public function fileExists(array $options)
{
$files = $this->listFiles($options);

return !empty($files);
}


/**
* Returns a single File object representing a file stored on B2.
*
Expand Down Expand Up @@ -328,7 +354,10 @@ public function getFile(array $options)
$response['contentSha1'],
$response['contentLength'],
$response['contentType'],
$response['fileInfo']
$response['fileInfo'],
$response['bucketId'],
$response['action'],
$response['uploadTimestamp']
);
}

Expand Down Expand Up @@ -422,7 +451,8 @@ protected function getBucketNameFromId($id)
protected function getFileIdFromBucketAndFileName($bucketName, $fileName)
{
$files = $this->listFiles([
'BucketName' => $bucketName
'BucketName' => $bucketName,
'FileName' => $fileName,
]);

foreach ($files as $file) {
Expand Down
35 changes: 34 additions & 1 deletion src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class File
protected $size;
protected $type;
protected $info;
protected $bucketId;
protected $action;
protected $uploadTimestamp;

/**
* File constructor.
Expand All @@ -20,15 +23,21 @@ class File
* @param $size
* @param $type
* @param $info
* @param $bucketId
* @param $action
* @param $uploadTimestamp
*/
public function __construct($id, $name, $hash = null, $size = null, $type = null, $info = null)
public function __construct($id, $name, $hash = null, $size = null, $type = null, $info = null, $bucketId = null, $action = null, $uploadTimestamp = null)
{
$this->id = $id;
$this->name = $name;
$this->hash = $hash;
$this->size = $size;
$this->type = $type;
$this->info = $info;
$this->bucketId = $bucketId;
$this->action = $action;
$this->uploadTimestamp = $uploadTimestamp;
}

/**
Expand Down Expand Up @@ -78,4 +87,28 @@ public function getInfo()
{
return $this->info;
}

/**
* @return string
*/
public function getBucketId()
{
return $this->bucketId;
}

/**
* @return string
*/
public function getAction()
{
return $this->action;
}

/**
* @return string
*/
public function getUploadTimestamp()
{
return $this->uploadTimestamp;
}
}
2 changes: 2 additions & 0 deletions tests/responses/get_file.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
"fileInfo": {
"src_last_modified_millis": "1454721688784"
},
"action": "upload",
"uploadTimestamp": "1465983870000",
"fileName": "Test file.bin"
}

0 comments on commit ec5a083

Please sign in to comment.