From 99cafbc9a5afbcb17ba80844aef440204fbdf888 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Sat, 14 Sep 2019 15:35:05 +0100 Subject: [PATCH] Backblaze #135 - B2 storage source now removes the current file version before uploading a new one --- app/AlbumSources/BackblazeB2Source.php | 40 +++++++++++++++++--------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/app/AlbumSources/BackblazeB2Source.php b/app/AlbumSources/BackblazeB2Source.php index 5223d32..b206c1a 100644 --- a/app/AlbumSources/BackblazeB2Source.php +++ b/app/AlbumSources/BackblazeB2Source.php @@ -53,12 +53,9 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource // Create or update our cache record - /** @var BackblazeB2FileIdCache $b2Cache */ - $b2Cache = BackblazeB2FileIdCache::where('storage_path', $pathOnStorage)->first(); + $b2Cache = $this->getB2FileFromCache($pathOnStorage); 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; } @@ -78,12 +75,9 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource // First we need the file ID - /** @var BackblazeB2FileIdCache $b2Cache */ - $b2Cache = BackblazeB2FileIdCache::where('storage_path', $pathOnStorage)->first(); + $b2Cache = $this->getB2FileFromCache($pathOnStorage); 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 EntityBody::fromString(''); } @@ -123,12 +117,9 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource // 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(); + $b2Cache = $this->getB2FileFromCache($pathOnStorage); 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 ''; } @@ -157,11 +148,17 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource { $pathOnStorage = $this->getPathToPhoto($photo, $thumbnail); + $b2Cache = $this->getB2FileFromCache($pathOnStorage); + if (!is_null($b2Cache)) + { + // Delete the current file version if we're replacing a file that already exists + $this->getClient()->deleteFile($b2Cache->b2_file_id, $pathOnStorage); + } + // Upload the file to B2 $b2FileID = $this->getClient()->uploadFile($tempFilename, $pathOnStorage); // Create or update our cache record - $b2Cache = BackblazeB2FileIdCache::where('storage_path', $pathOnStorage)->first(); if (is_null($b2Cache)) { $b2Cache = new BackblazeB2FileIdCache([ @@ -183,6 +180,23 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource parent::setConfiguration($configuration); } + /** + * @param $pathOnStorage + * @return BackblazeB2FileIdCache|null + */ + private function getB2FileFromCache($pathOnStorage) + { + $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 null; + } + + return $b2Cache; + } + private function getClient() { if (is_null($this->backblaze))