diff --git a/src/Client.php b/src/Client.php index f6141f0..405f0aa 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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', [ @@ -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; } @@ -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. * @@ -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'] ); } @@ -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) { diff --git a/src/File.php b/src/File.php index a837cc7..8013586 100644 --- a/src/File.php +++ b/src/File.php @@ -10,6 +10,9 @@ class File protected $size; protected $type; protected $info; + protected $bucketId; + protected $action; + protected $uploadTimestamp; /** * File constructor. @@ -20,8 +23,11 @@ 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; @@ -29,6 +35,9 @@ public function __construct($id, $name, $hash = null, $size = null, $type = null $this->size = $size; $this->type = $type; $this->info = $info; + $this->bucketId = $bucketId; + $this->action = $action; + $this->uploadTimestamp = $uploadTimestamp; } /** @@ -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; + } } diff --git a/tests/responses/get_file.json b/tests/responses/get_file.json index 222a25d..61df0eb 100644 --- a/tests/responses/get_file.json +++ b/tests/responses/get_file.json @@ -8,5 +8,7 @@ "fileInfo": { "src_last_modified_millis": "1454721688784" }, + "action": "upload", + "uploadTimestamp": "1465983870000", "fileName": "Test file.bin" }