#123: The setting tab now only lists compatible storages. Added support for IAnalysisQueueSource to the LocalFileSystemSource driver

This commit is contained in:
Andy Heathershaw 2019-07-13 10:27:35 +01:00
parent 95e79f2d28
commit 07fa9639b5
4 changed files with 126 additions and 7 deletions

View File

@ -3,6 +3,7 @@
namespace App\AlbumSources;
use App\Album;
use App\Helpers\FileHelper;
use App\Helpers\MiscHelper;
use App\Photo;
use App\Services\PhotoService;
@ -13,7 +14,7 @@ use Symfony\Component\HttpFoundation\File\File;
* Driver for managing files on the local filesystem.
* @package App\AlbumSources
*/
class LocalFilesystemSource extends AlbumSourceBase implements IAlbumSource
class LocalFilesystemSource extends AlbumSourceBase implements IAlbumSource, IAnalysisQueueSource
{
public function deleteAlbumContents()
{
@ -120,4 +121,86 @@ class LocalFilesystemSource extends AlbumSourceBase implements IAlbumSource
rmdir($directory);
}
/**
* Deletes a photo to be analysed from the storage source
* @param string $queueToken Queue token holding the photo
* @param string $fileName Filename of the photo to download
* @return void
*/
public function deleteItemFromAnalysisQueue($queueToken, $fileName)
{
$queueFolder = $this->getQueuePath($queueToken);
$filePath = $this->getQueueItemPath($queueToken, $fileName);
@unlink($filePath);
// Delete the parent folder if empty
FileHelper::deleteIfEmpty($queueFolder);
}
/**
* Downloads a photo to be analysed from the storage source to a temporary file
* @param string $queueToken Queue token holding the photo
* @param string $fileName Filename of the photo to download
* @return string Path to the photo that was downloaded
*/
public function fetchItemFromAnalysisQueue($queueToken, $fileName)
{
// Don't actually need to download anything as it's already local
return $this->getQueueItemPath($queueToken, $fileName);
}
/**
* 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)
{
$uploadedFile = new File($sourceFilePath);
$tempFilename = $this->getQueueItemPath(
$queueToken,
is_null($overrideFilename) ? MiscHelper::randomString(20) : basename($overrideFilename)
);
// Only add an extension if an override filename was not given, assume this is present
if (is_null($overrideFilename))
{
$extension = $uploadedFile->guessExtension();
if (!is_null($extension))
{
$tempFilename .= '.' . $extension;
}
}
$uploadedFile->move(dirname($tempFilename), basename($tempFilename));
return $tempFilename;
}
private function getQueueItemPath($queueToken, $fileName)
{
return join(DIRECTORY_SEPARATOR, [$this->getQueuePath($queueToken), $fileName]);
}
private function getQueuePath($queueUid)
{
$path = join(DIRECTORY_SEPARATOR, [
dirname(dirname(__DIR__)),
'storage',
'app',
'analysis-queue',
str_replace(['.', '/', '\\'], '', $queueUid)
]);
if (!file_exists($path))
{
@mkdir($path, 0755, true);
}
return $path;
}
}

View File

@ -16,10 +16,49 @@ class AnalysisQueueHelper
{
$queueStorage = Storage::find(UserConfig::get('analysis_queue_storage_location'));
$queueSource = self::createStorageSource($queueStorage);
if (is_null($queueSource))
{
throw new \Exception(sprintf('Queue storage \'%s\' does not support the analysis queue', $queueStorage->name));
}
return $queueSource;
}
/**
* Gets a list of compatible storage sources for the analysis queue.
* @return array
*/
public static function getCompatibleStorages()
{
$storageSources = [];
foreach (Storage::where('is_active', true)->orderBy('name')->get() as $storage)
{
$queueSource = self::createStorageSource($storage);
if (is_null($queueSource))
{
continue;
}
$storageSources[$storage->id] = $storage->name;
}
return $storageSources;
}
private static function createStorageSource(Storage $queueStorage)
{
$fullClassName = sprintf('App\AlbumSources\%s', $queueStorage->source);
/** @var IAnalysisQueueSource $source */
$source = new $fullClassName;
if (!$source instanceof IAnalysisQueueSource)
{
return null;
}
$source->setConfiguration($queueStorage);
return $source;

View File

@ -7,6 +7,7 @@ use App\Configuration;
use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Group;
use App\Helpers\AnalysisQueueHelper;
use App\Helpers\ConfigHelper;
use App\Helpers\DbHelper;
use App\Helpers\MiscHelper;
@ -370,11 +371,7 @@ class DefaultController extends Controller
$themeNamesLookup = UserConfig::allowedThemeNames();
// Storage sources for the Image Processing tab
$storageSources = [];
foreach (Storage::where('is_active', true)->orderBy('name')->get() as $storage)
{
$storageSources[$storage->id] = $storage->name;
}
$storageSources = AnalysisQueueHelper::getCompatibleStorages();
return Theme::render('admin.settings', [
'config' => $config,

View File

@ -201,7 +201,7 @@
<p>@lang('admin.settings.analysis_queue_storage_intro')</p>
<p>@lang('admin.settings.analysis_queue_storage_intro_2')</p>
<div class="alert alert-warning mb-5">
<div class="alert alert-warning mb-4">
<p class="mb-0">@lang('admin.settings.analysis_queue_storage_warning')</p>
</div>