2016-09-02 22:00:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\AlbumSources;
|
|
|
|
|
|
|
|
use App\Album;
|
|
|
|
use App\Helpers\MiscHelper;
|
|
|
|
use App\Photo;
|
2016-09-09 11:09:03 +01:00
|
|
|
use App\Services\PhotoService;
|
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
|
|
|
|
*/
|
2016-09-02 22:00:42 +01:00
|
|
|
class LocalFilesystemSource implements IAlbumSource
|
|
|
|
{
|
2016-09-08 23:22:29 +01:00
|
|
|
/**
|
|
|
|
* @var Album
|
|
|
|
*/
|
|
|
|
private $album;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-09-02 22:00:42 +01:00
|
|
|
private $parentFolder;
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
public function __construct(Album $album, $parentFolder)
|
2016-09-02 22:00:42 +01:00
|
|
|
{
|
2016-09-08 23:22:29 +01:00
|
|
|
$this->album = $album;
|
2016-09-02 22:00:42 +01:00
|
|
|
$this->parentFolder = $parentFolder;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return unlink($this->getPathToPhoto($photo, $thumbnail));
|
|
|
|
}
|
|
|
|
|
2016-09-03 22:13:05 +01:00
|
|
|
public function getOriginalsFolder()
|
2016-09-02 22:00:42 +01:00
|
|
|
{
|
2016-09-03 22:13:05 +01:00
|
|
|
return '_originals';
|
2016-09-02 22:00:42 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
public function getPathToPhoto(Photo $photo, $thumbnail = null)
|
2016-09-03 17:09:49 +01:00
|
|
|
{
|
2016-09-03 22:13:05 +01:00
|
|
|
if (is_null($thumbnail))
|
|
|
|
{
|
|
|
|
$thumbnail = $this->getOriginalsFolder();
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
return sprintf('%s/%s/%s', $this->getPathToAlbum(), $thumbnail, $photo->storage_file_name);
|
2016-09-03 22:13:05 +01:00
|
|
|
}
|
|
|
|
|
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', [
|
2016-09-08 23:22:29 +01:00
|
|
|
'albumUrlAlias' => $this->album->url_alias,
|
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-09-08 23:22:29 +01:00
|
|
|
public function saveThumbnail(Photo $photo, $thumbnailInfo, $tempFilename)
|
2016-09-03 22:13:05 +01:00
|
|
|
{
|
|
|
|
$fileInfo = new File($tempFilename);
|
2016-09-08 23:22:29 +01:00
|
|
|
$fileInfo->move(sprintf('%s/%s', $this->getPathToAlbum(), $thumbnailInfo['name']), $photo->storage_file_name);
|
2016-09-03 17:09:49 +01:00
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
public function saveUploadedPhoto(File $uploadedFile)
|
2016-09-02 22:00:42 +01:00
|
|
|
{
|
2016-09-08 23:22:29 +01:00
|
|
|
$tempFilename = sprintf('%s/%s/%s', $this->getPathToAlbum(), $this->getOriginalsFolder(), MiscHelper::randomString(20));
|
2016-09-02 22:00:42 +01:00
|
|
|
|
|
|
|
$extension = $uploadedFile->guessExtension();
|
|
|
|
if (!is_null($extension))
|
|
|
|
{
|
|
|
|
$tempFilename .= '.' . $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
$uploadedFile->move(dirname($tempFilename), basename($tempFilename));
|
|
|
|
return new File($tempFilename);
|
|
|
|
}
|
|
|
|
|
2016-09-08 23:22:29 +01:00
|
|
|
private function getPathToAlbum()
|
2016-09-02 22:00:42 +01:00
|
|
|
{
|
2016-09-08 23:22:29 +01:00
|
|
|
return sprintf('%s/%s', $this->parentFolder, $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);
|
|
|
|
}
|
2016-09-02 22:00:42 +01:00
|
|
|
}
|