blue-twilight/app/AlbumSources/LocalFilesystemSource.php

205 lines
5.9 KiB
PHP

<?php
namespace App\AlbumSources;
use App\Helpers\FileHelper;
use App\Helpers\MiscHelper;
use App\Photo;
use GuzzleHttp\Psr7\Stream;
use Symfony\Component\HttpFoundation\File\File;
use function GuzzleHttp\Psr7\stream_for;
/**
* Driver for managing files on the local filesystem.
* @package App\AlbumSources
*/
class LocalFilesystemSource extends AlbumSourceBase implements IAlbumSource, IAnalysisQueueSource
{
public function deleteAlbumContents()
{
if (file_exists($this->getPathToAlbum()) && is_dir($this->getPathToAlbum()))
{
$this->recursiveDelete($this->getPathToAlbum());
}
}
public function deleteThumbnail(Photo $photo, $thumbnail = null)
{
return @unlink(
join(DIRECTORY_SEPARATOR, [
$this->getPathToAlbum(),
is_null($thumbnail) ? $this->getOriginalsFolder() : $thumbnail,
$photo->storage_file_name
])
);
}
/**
* Fetches the contents of a thumbnail for a photo.
* @param Photo $photo Photo to fetch the thumbnail for.
* @param string $thumbnail Thumbnail to fetch (or null to fetch the original.)
* @return Stream
*/
public function fetchPhotoContent(Photo $photo, $thumbnail = null)
{
$fh = fopen(
join(DIRECTORY_SEPARATOR, [
$this->getPathToAlbum(),
is_null($thumbnail) ? $this->getOriginalsFolder() : $thumbnail,
$photo->storage_file_name
]),
'r+'
);
return stream_for($fh);
}
public function getName()
{
return 'global.album_sources.filesystem';
}
public function getUrlToPhoto(Photo $photo, $thumbnail = null)
{
$photoUrl = route('downloadPhoto', [
'albumUrlAlias' => $this->album->url_path,
'photoFilename' => $photo->storage_file_name
]);
if (!is_null($thumbnail))
{
$photoUrl .= sprintf('?t=%s', urlencode($thumbnail));
}
return $photoUrl;
}
public function saveThumbnail(Photo $photo, $tempFilename, $thumbnail = null)
{
$fileInfo = new File($tempFilename);
$fileInfo->move(
join(DIRECTORY_SEPARATOR, [
$this->getPathToAlbum(),
is_null($thumbnail) ? $this->getOriginalsFolder() : $thumbnail
]),
$photo->storage_file_name
);
}
private function getOriginalsFolder()
{
return '_originals';
}
private function getPathToAlbum()
{
return sprintf('%s/%s', $this->configuration->location, $this->album->url_alias);
}
private function recursiveDelete($directory)
{
$result = scandir($directory);
foreach ($result as $file)
{
if ($file == '.' || $file == '..')
{
continue;
}
$fullPath = sprintf('%s/%s', $directory, $file);
if (is_dir($fullPath))
{
$this->recursiveDelete($fullPath);
}
else
{
unlink($fullPath);
}
}
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;
}
}
copy($uploadedFile->getRealPath(), $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;
}
}