2016-09-02 22:00:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\AlbumSources;
|
|
|
|
|
|
|
|
use App\Album;
|
|
|
|
use App\Helpers\MiscHelper;
|
|
|
|
use App\Photo;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
private $parentFolder;
|
|
|
|
|
|
|
|
public function __construct($parentFolder)
|
|
|
|
{
|
|
|
|
$this->parentFolder = $parentFolder;
|
|
|
|
}
|
|
|
|
|
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-03 22:13:05 +01:00
|
|
|
public function getPathToPhoto(Album $album, 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('%s/%s/%s', $this->getPathToAlbum($album), $thumbnail, $photo->file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUrlToPhoto(Album $album, Photo $photo, $thumbnail = null)
|
|
|
|
{
|
|
|
|
$photoUrl = sprintf('%s/%s', urlencode($album->url_alias), urlencode($photo->file_name));
|
|
|
|
|
|
|
|
if (!is_null($thumbnail))
|
|
|
|
{
|
|
|
|
$photoUrl .= sprintf('?t=%s', urlencode($thumbnail));
|
|
|
|
}
|
|
|
|
|
|
|
|
return url($photoUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function saveThumbnail(Album $album, Photo $photo, $thumbnailInfo, $tempFilename)
|
|
|
|
{
|
|
|
|
$fileInfo = new File($tempFilename);
|
|
|
|
$fileInfo->move(sprintf('%s/%s', $this->getPathToAlbum($album), $thumbnailInfo['name']), $photo->file_name);
|
2016-09-03 17:09:49 +01:00
|
|
|
}
|
|
|
|
|
2016-09-02 22:00:42 +01:00
|
|
|
public function saveUploadedPhoto(Album $album, File $uploadedFile)
|
|
|
|
{
|
2016-09-03 22:13:05 +01:00
|
|
|
$tempFilename = sprintf('%s/%s/%s', $this->getPathToAlbum($album), $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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPathToAlbum(Album $album)
|
|
|
|
{
|
2016-09-03 22:13:05 +01:00
|
|
|
return sprintf('%s/%s', $this->parentFolder, $album->url_alias);
|
2016-09-02 22:00:42 +01:00
|
|
|
}
|
|
|
|
}
|