#123: Images are now uploaded to the storage driver specified using the new IAnalysisQueueSource interface

This commit is contained in:
Andy Heathershaw 2019-07-12 16:21:30 +01:00
parent bb5ed4d68d
commit 7a71a06e4e
4 changed files with 96 additions and 8 deletions

View File

@ -2,12 +2,13 @@
namespace App\AlbumSources; namespace App\AlbumSources;
use App\Helpers\MiscHelper;
use App\Photo; use App\Photo;
use Guzzle\Http\EntityBody; use Guzzle\Http\EntityBody;
use Guzzle\Http\Exception\ClientErrorResponseException; use Guzzle\Http\Exception\ClientErrorResponseException;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class AmazonS3Source extends AlbumSourceBase implements IAlbumSource class AmazonS3Source extends AlbumSourceBase implements IAlbumSource, IAnalysisQueueSource
{ {
/** /**
* Deletes an entire album's media contents. * Deletes an entire album's media contents.
@ -103,6 +104,28 @@ class AmazonS3Source extends AlbumSourceBase implements IAlbumSource
$this->getClient()->upload($this->configuration->container_name, $photoPath, fopen($tempFilename, 'r+'), 'public-read'); $this->getClient()->upload($this->configuration->container_name, $photoPath, fopen($tempFilename, 'r+'), 'public-read');
} }
/**
* Uploads a new file to the analysis queue specified by queue token.
*
* @param string $sourceFilePath Path to the file to upload to the analysis queue
* @param string $queueToken Queue token to hold the photo
* @param string $overrideFilename Use a specific filename, or false to set a specific name
* @return string Path to the file
*/
public function uploadToAnalysisQueue($sourceFilePath, $queueToken, $overrideFilename = null)
{
$targetPath = sprintf(
'%s/%s',
$this->getPathToAnalysisQueue($queueToken),
is_null($overrideFilename) ? MiscHelper::randomString(20) : $overrideFilename
);
// Note: we don't use "public-read" here as it will not be publicly-accessible, and will be retrieved by an authenticated client
$this->getClient()->upload($this->configuration->container_name, $targetPath, fopen($sourceFilePath, 'r+'));
return $targetPath;
}
private function getClient() private function getClient()
{ {
$config = [ $config = [
@ -127,6 +150,11 @@ class AmazonS3Source extends AlbumSourceBase implements IAlbumSource
return '_originals'; return '_originals';
} }
private function getPathToAnalysisQueue($queueToken)
{
return sprintf('analysis-queue/%s', $queueToken);
}
private function getPathToPhoto(Photo $photo, $thumbnail = null) private function getPathToPhoto(Photo $photo, $thumbnail = null)
{ {
return sprintf( return sprintf(

View File

@ -0,0 +1,24 @@
<?php
namespace App\AlbumSources;
use App\Storage;
interface IAnalysisQueueSource
{
/**
* @param Storage $configuration
* @return mixed
*/
function setConfiguration(Storage $configuration);
/**
* Uploads a new file to the analysis queue specified by queue token.
*
* @param string $sourceFilePath Path to the file to upload to the analysis queue
* @param string $queueToken Queue token to hold the photo
* @param string $overrideFilename Use a specific filename, or false to set a specific name
* @return string Path to the file
*/
function uploadToAnalysisQueue($sourceFilePath, $queueToken, $overrideFilename = null);
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Helpers;
use App\AlbumSources\IAnalysisQueueSource;
use App\Facade\UserConfig;
use App\Storage;
class AnalysisQueueHelper
{
/**
* Gets the storage queue source in use.
* @return IAnalysisQueueSource
*/
public static function getStorageQueueSource()
{
$queueStorage = Storage::find(UserConfig::get('analysis_queue_storage_location'));
$fullClassName = sprintf('App\AlbumSources\%s', $queueStorage->source);
/** @var IAnalysisQueueSource $source */
$source = new $fullClassName;
$source->setConfiguration($queueStorage);
return $source;
}
}

View File

@ -7,6 +7,7 @@ use App\AlbumSources\IAlbumSource;
use App\Facade\Image; use App\Facade\Image;
use App\Facade\Theme; use App\Facade\Theme;
use App\Facade\UserConfig; use App\Facade\UserConfig;
use App\Helpers\AnalysisQueueHelper;
use App\Helpers\FileHelper; use App\Helpers\FileHelper;
use App\Helpers\ImageHelper; use App\Helpers\ImageHelper;
use App\Helpers\MiscHelper; use App\Helpers\MiscHelper;
@ -16,6 +17,7 @@ use App\Photo;
use App\QueueItem; use App\QueueItem;
use App\Services\PhotoService; use App\Services\PhotoService;
use App\Services\RabbitMQService; use App\Services\RabbitMQService;
use App\Storage;
use App\Upload; use App\Upload;
use App\UploadPhoto; use App\UploadPhoto;
use App\User; use App\User;
@ -298,7 +300,7 @@ class PhotoController extends Controller
throw new \Exception('No queue_token value was provided!'); throw new \Exception('No queue_token value was provided!');
} }
$queueFolder = FileHelper::getQueuePath($queueUid); $queueStorage = AnalysisQueueHelper::getStorageQueueSource();
foreach ($photoFiles as $photoFile) foreach ($photoFiles as $photoFile)
{ {
@ -316,26 +318,33 @@ class PhotoController extends Controller
$photo->raw_exif_data = null; $photo->raw_exif_data = null;
/** @var File $savedFile */ /** @var File $savedFile */
$queueFolder = FileHelper::getQueuePath($queueUid);
$savedFile = FileHelper::saveUploadedFile($photoFile, $queueFolder, $photo->storage_file_name); $savedFile = FileHelper::saveUploadedFile($photoFile, $queueFolder, $photo->storage_file_name);
$this->removeExistingActivityRecords($photo, 'photo.uploaded'); $this->removeExistingActivityRecords($photo, 'photo.uploaded');
$this->removeExistingActivityRecords($photo, 'photo.taken'); $this->removeExistingActivityRecords($photo, 'photo.taken');
$photo->file_name = $photoFile->getClientOriginalName();
$photo->mime_type = $savedFile->getMimeType();
$photo->file_size = $savedFile->getSize();
$photo->storage_file_name = $savedFile->getFilename();
} }
else else
{ {
/** @var File $savedFile */ $queuedFileName = $queueStorage->uploadToAnalysisQueue($photoFile, $queueUid);
$savedFile = FileHelper::saveUploadedFile($photoFile, $queueFolder); $uploadedTempFile = new File($photoFile);
$photo = new Photo(); $photo = new Photo();
$photo->album_id = $album->id; $photo->album_id = $album->id;
$photo->user_id = Auth::user()->id; $photo->user_id = Auth::user()->id;
$photo->name = pathinfo($photoFile->getClientOriginalName(), PATHINFO_FILENAME); $photo->name = pathinfo($photoFile->getClientOriginalName(), PATHINFO_FILENAME);
$photo->storage_file_name = $savedFile->getFilename();
}
$photo->file_name = $photoFile->getClientOriginalName(); $photo->file_name = $photoFile->getClientOriginalName();
$photo->mime_type = $savedFile->getMimeType(); $photo->mime_type = $uploadedTempFile->getMimeType();
$photo->file_size = $savedFile->getSize(); $photo->file_size = $uploadedTempFile->getSize();
$photo->storage_file_name = basename($queuedFileName);
}
$photo->is_analysed = false; $photo->is_analysed = false;
$photo->save(); $photo->save();