Backblaze #135 - tried to implement b2_download_file_by_id for private buckets, but this doesn't work correctly, logged with Backblaze

This commit is contained in:
Andy Heathershaw 2019-09-10 16:24:26 +01:00
parent ce03b2596f
commit fb6754b8e9

View File

@ -102,30 +102,39 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource
public function getUrlToPhoto(Photo $photo, $thumbnail = null) public function getUrlToPhoto(Photo $photo, $thumbnail = null)
{ {
$client = $this->getClient(); $client = $this->getClient();
$storagePathToFile = $this->getPathToPhoto($photo, $thumbnail); $pathOnStorage = $this->getPathToPhoto($photo, $thumbnail);
/*
* From https://www.backblaze.com/b2/docs/b2_download_file_by_name.html:
* The base URL to use comes from the b2_authorize_account call, and looks something like
* https://f345.backblazeb2.com. The "f" in the URL stands for "file", and the number is the cluster
* number containing your account. To this base, you add "file/", your bucket name, a "/", and then the
* name of the file. The file name may itself include more "/" characters.
*/
$fileDownloadUrl = sprintf('%s/file/%s/%s', $client->getDownloadUrl(), $this->configuration->container_name, $storagePathToFile);
switch ($this->bucketType) switch ($this->bucketType)
{ {
case self::BUCKET_TYPE_PRIVATE: case self::BUCKET_TYPE_PRIVATE:
// TODO: use the B2 b2_download_file_by_id method so filenames are harder to guess
if (is_null($this->downloadToken)) if (is_null($this->downloadToken))
{ {
$this->downloadToken = $client->getDownloadAuthToken(); $this->downloadToken = $client->getDownloadAuthToken();
} }
return sprintf('%s?Authorization=%s', $fileDownloadUrl, $this->downloadToken); // Once I sort out the issue with b2_download_file_by_id, this line can be removed
return sprintf('%s/file/%s/%s?Authorization=%s', $client->getDownloadUrl(), $this->configuration->container_name, $pathOnStorage, $this->downloadToken);
/** @var BackblazeB2FileIdCache $b2Cache */
$b2Cache = BackblazeB2FileIdCache::where('storage_path', $pathOnStorage)->first();
if (is_null($b2Cache))
{
// TODO: lookup the file on B2 to get the file ID
Log::warning(sprintf('B2 file ID not found in cache: %s', $pathOnStorage));
return '';
}
return sprintf('%s/b2api/v2/b2_download_file_by_id?fileId=%s&Authorization=%s', $client->getDownloadUrl(), urlencode($b2Cache->b2_file_id), urlencode($this->downloadToken));
case self::BUCKET_TYPE_PUBLIC: case self::BUCKET_TYPE_PUBLIC:
return $fileDownloadUrl; /*
* From https://www.backblaze.com/b2/docs/b2_download_file_by_name.html:
* The base URL to use comes from the b2_authorize_account call, and looks something like
* https://f345.backblazeb2.com. The "f" in the URL stands for "file", and the number is the cluster
* number containing your account. To this base, you add "file/", your bucket name, a "/", and then the
* name of the file. The file name may itself include more "/" characters.
*/
return sprintf('%s/file/%s/%s', $client->getDownloadUrl(), $this->configuration->container_name, $pathOnStorage);
} }
} }