Skip to content

Commit

Permalink
[ADD] Method to get download url from File instance
Browse files Browse the repository at this point in the history
  • Loading branch information
robregonm committed Oct 12, 2018
1 parent cdaff57 commit 247fec9
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,13 @@ public function getDownloadAuthorization($bucket, $path, $validDuration = 60)
return $response['authorizationToken'];
}

/**
* @param Bucket|string $bucket
* @param string $filePath
* @param bool $appendToken
* @param int $tokenTimeout
* @return string
*/
public function getDownloadUrl($bucket, string $filePath, $appendToken = false, $tokenTimeout = 60)
{
if (!$bucket instanceof Bucket) {
Expand All @@ -273,13 +280,6 @@ public function getDownloadUrl($bucket, string $filePath, $appendToken = false,

$baseUrl = strtr($this->downloadUrl, $this->domainAliases);
$path = $baseUrl . '/file/' . $bucket->getName() . '/' . $filePath;
// Based on: https://github.com/yakovkhalinsky/backblaze-b2/issues/12
/*$fullDownloadUrl = implode('/', [
$this->downloadUrl, //https://d02.instaid.co/
'b2api',
'v1',
'b2_download_file_by_id?fileId=' . $fileId
]);*/

if ($appendToken) {
$path .= '?Authorization='
Expand All @@ -289,6 +289,17 @@ public function getDownloadUrl($bucket, string $filePath, $appendToken = false,
return $path;
}

/**
* @param File $file
* @param bool $appendToken
* @param int $tokenTimeout
* @return string
*/
public function getDownloadUrlForFile(File $file, $appendToken = false, $tokenTimeout = 60)
{
return $this->getDownloadUrl($file->getBucketId(), $file->getFileName(), $appendToken, $tokenTimeout);
}

/**
* Download a file from a B2 bucket.
*
Expand Down

0 comments on commit 247fec9

Please sign in to comment.