diff --git a/app/AlbumSources/BackblazeB2Source.php b/app/AlbumSources/BackblazeB2Source.php index b7e3986..c9a4c9e 100644 --- a/app/AlbumSources/BackblazeB2Source.php +++ b/app/AlbumSources/BackblazeB2Source.php @@ -102,30 +102,39 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource public function getUrlToPhoto(Photo $photo, $thumbnail = null) { $client = $this->getClient(); - $storagePathToFile = $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); + $pathOnStorage = $this->getPathToPhoto($photo, $thumbnail); switch ($this->bucketType) { 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)) { $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: - 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); } }