2016-09-02 22:00:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\AlbumSources;
|
|
|
|
|
2019-07-13 10:27:35 +01:00
|
|
|
use App\Helpers\FileHelper;
|
2016-09-02 22:00:42 +01:00
|
|
|
use App\Helpers\MiscHelper;
|
|
|
|
use App\Photo;
|
2016-10-28 12:59:36 +01:00
|
|
|
use Guzzle\Http\EntityBody;
|
2016-09-02 22:00:42 +01:00
|
|
|
use Symfony\Component\HttpFoundation\File\File;
|
|
|
|
|
2016-09-03 22:13:05 +01:00
|
|
|
/**
|
|
|
|
* Driver for managing files on the local filesystem.
|
|
|
|
* @package App\AlbumSources
|
|
|
|
*/
|
2019-07-13 10:27:35 +01:00
|
|
|
class LocalFilesystemSource extends AlbumSourceBase implements IAlbumSource, IAnalysisQueueSource
|
2016-09-02 22:00:42 +01:00
|
|
|
{
|
2016-09-09 11:09:03 +01:00
|
|
|
public function deleteAlbumContents()
|
|
|
|
{
|
2016-09-09 15:06:34 +01:00
|
|
|
if (file_exists($this->getPathToAlbum()) && is_dir($this->getPathToAlbum()))
|
|
|
|
{
|
|
|
|
$this->recursiveDelete($this->getPathToAlbum());
|
|
|
|
}
|
2016-09-09 11:09:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteThumbnail(Photo $photo, $thumbnail = null)
|
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
return @unlink(
|
|
|
|
join(DIRECTORY_SEPARATOR, [
|
|
|
|
$this->getPathToAlbum(),
|
|
|
|
is_null($thumbnail) ? $this->getOriginalsFolder() : $thumbnail,
|
|
|
|
$photo->storage_file_name
|
|
|
|
])
|
|
|
|
);
|
2016-09-09 11:09:03 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
/**
|
|
|
|
* 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 EntityBody
|
|
|
|
*/
|
|
|
|
public function fetchPhotoContent(Photo $photo, $thumbnail = null)
|
2016-09-02 22:00:42 +01:00
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
$fh = fopen(
|
|
|
|
join(DIRECTORY_SEPARATOR, [
|
|
|
|
$this->getPathToAlbum(),
|
|
|
|
is_null($thumbnail) ? $this->getOriginalsFolder() : $thumbnail,
|
|
|
|
$photo->storage_file_name
|
|
|
|
]),
|
|
|
|
'r+'
|
|
|
|
);
|
|
|
|
|
|
|
|
return EntityBody::factory($fh);
|
2016-09-02 22:00:42 +01:00
|
|
|
}
|
|
|
|
|
2016-09-24 09:34:08 +01:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return 'global.album_sources.filesystem';
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
public function getUrlToPhoto(Photo $photo, $thumbnail = null)
|
2016-09-03 22:13:05 +01:00
|
|
|
{
|
2016-09-04 21:59:32 +01:00
|
|
|
$photoUrl = route('downloadPhoto', [
|
2017-04-17 21:31:45 +01:00
|
|
|
'albumUrlAlias' => $this->album->url_path,
|
2016-09-06 19:47:25 +01:00
|
|
|
'photoFilename' => $photo->storage_file_name
|
2016-09-04 21:59:32 +01:00
|
|
|
]);
|
2016-09-03 22:13:05 +01:00
|
|
|
|
|
|
|
if (!is_null($thumbnail))
|
|
|
|
{
|
|
|
|
$photoUrl .= sprintf('?t=%s', urlencode($thumbnail));
|
|
|
|
}
|
|
|
|
|
2016-09-04 21:59:32 +01:00
|
|
|
return $photoUrl;
|
2016-09-03 22:13:05 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
public function saveThumbnail(Photo $photo, $tempFilename, $thumbnail = null)
|
2016-09-03 22:13:05 +01:00
|
|
|
{
|
|
|
|
$fileInfo = new File($tempFilename);
|
2016-10-28 12:59:36 +01:00
|
|
|
$fileInfo->move(
|
|
|
|
join(DIRECTORY_SEPARATOR, [
|
|
|
|
$this->getPathToAlbum(),
|
|
|
|
is_null($thumbnail) ? $this->getOriginalsFolder() : $thumbnail
|
|
|
|
]),
|
|
|
|
$photo->storage_file_name
|
|
|
|
);
|
2016-09-03 17:09:49 +01:00
|
|
|
}
|
|
|
|
|
2016-10-28 12:59:36 +01:00
|
|
|
private function getOriginalsFolder()
|
2016-09-02 22:00:42 +01:00
|
|
|
{
|
2016-10-28 12:59:36 +01:00
|
|
|
return '_originals';
|
2016-09-02 22:00:42 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
private function getPathToAlbum()
|
2016-09-02 22:00:42 +01:00
|
|
|
{
|
2016-09-24 09:34:08 +01:00
|
|
|
return sprintf('%s/%s', $this->configuration->location, $this->album->url_alias);
|
2016-09-02 22:00:42 +01:00
|
|
|
}
|
2016-09-09 11:09:03 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2019-07-13 10:27:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-27 13:50:27 +01:00
|
|
|
copy($uploadedFile->getRealPath(), $tempFilename);
|
2019-07-13 10:27:35 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-09-02 22:00:42 +01:00
|
|
|
}
|