Skip to content

Commit

Permalink
Allow getFile() via a bucket name and file name
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris White committed Feb 18, 2016
1 parent 39eac63 commit b7b0117
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
21 changes: 20 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ public function listFiles(array $options)
*/
public function getFile(array $options)
{
if (!isset($options['FileId']) && isset($options['BucketName']) && isset($options['FileName'])) {
$options['FileId'] = $this->getFileIdFromBucketAndFileName($options['BucketName'], $options['FileName']);
}

$response = $this->client->request('POST', $this->apiUrl.'/b2_get_file_info', [
'headers' => [
'Authorization' => $this->authToken
Expand Down Expand Up @@ -338,7 +342,7 @@ public function deleteFile(array $options)
if (!isset($options['FileName'])) {
$file = $this->getFile($options);

$options['FileName'] = $file->getPath();
$options['FileName'] = $file->getName();
}

$this->client->request('POST', $this->apiUrl.'/b2_delete_file_version', [
Expand Down Expand Up @@ -407,4 +411,19 @@ protected function getBucketNameFromId($id)

return null;
}

protected function getFileIdFromBucketAndFileName($bucketName, $fileName)
{
$files = $this->listFiles([
'BucketName' => $bucketName
]);

foreach ($files as $file) {
if ($file->getName() === $fileName) {
return $file->getId();
}
}

return null;
}
}
14 changes: 6 additions & 8 deletions src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
class File
{
protected $id;

// TODO: I reckon path should be name instead, to keep it consistent with B2's terminology.
protected $path;
protected $name;
protected $hash;
protected $size;
protected $type;
Expand All @@ -17,16 +15,16 @@ class File
* File constructor.
*
* @param $id
* @param $path
* @param $name
* @param $hash
* @param $size
* @param $type
* @param $info
*/
public function __construct($id, $path, $hash = null, $size = null, $type = null, $info = null)
public function __construct($id, $name, $hash = null, $size = null, $type = null, $info = null)
{
$this->id = $id;
$this->path = $path;
$this->name = $name;
$this->hash = $hash;
$this->size = $size;
$this->type = $type;
Expand All @@ -44,9 +42,9 @@ public function getId()
/**
* @return string
*/
public function getPath()
public function getName()
{
return $this->path;
return $this->name;
}

/**
Expand Down

0 comments on commit b7b0117

Please sign in to comment.