Backblaze #135 - B2 storage source now removes the current file version before uploading a new one

This commit is contained in:
Andy Heathershaw 2019-09-14 15:35:05 +01:00
parent a6825bcef9
commit 99cafbc9a5

View File

@ -53,12 +53,9 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource
// Create or update our cache record // Create or update our cache record
/** @var BackblazeB2FileIdCache $b2Cache */ $b2Cache = $this->getB2FileFromCache($pathOnStorage);
$b2Cache = BackblazeB2FileIdCache::where('storage_path', $pathOnStorage)->first();
if (is_null($b2Cache)) 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;
} }
@ -78,12 +75,9 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource
// First we need the file ID // First we need the file ID
/** @var BackblazeB2FileIdCache $b2Cache */ $b2Cache = $this->getB2FileFromCache($pathOnStorage);
$b2Cache = BackblazeB2FileIdCache::where('storage_path', $pathOnStorage)->first();
if (is_null($b2Cache)) 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(''); 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 // 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); return sprintf('%s/file/%s/%s?Authorization=%s', $client->getDownloadUrl(), $this->configuration->container_name, $pathOnStorage, $this->downloadToken);
/** @var BackblazeB2FileIdCache $b2Cache */ $b2Cache = $this->getB2FileFromCache($pathOnStorage);
$b2Cache = BackblazeB2FileIdCache::where('storage_path', $pathOnStorage)->first();
if (is_null($b2Cache)) 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 '';
} }
@ -157,11 +148,17 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource
{ {
$pathOnStorage = $this->getPathToPhoto($photo, $thumbnail); $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 // Upload the file to B2
$b2FileID = $this->getClient()->uploadFile($tempFilename, $pathOnStorage); $b2FileID = $this->getClient()->uploadFile($tempFilename, $pathOnStorage);
// Create or update our cache record // Create or update our cache record
$b2Cache = BackblazeB2FileIdCache::where('storage_path', $pathOnStorage)->first();
if (is_null($b2Cache)) if (is_null($b2Cache))
{ {
$b2Cache = new BackblazeB2FileIdCache([ $b2Cache = new BackblazeB2FileIdCache([
@ -183,6 +180,23 @@ class BackblazeB2Source extends AlbumSourceBase implements IAlbumSource
parent::setConfiguration($configuration); 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() private function getClient()
{ {
if (is_null($this->backblaze)) if (is_null($this->backblaze))