blue-twilight/app/AlbumSources/LocalFilesystemSource.php

86 lines
2.1 KiB
PHP

<?php
namespace App\AlbumSources;
use App\Album;
use App\Helpers\MiscHelper;
use App\Photo;
use Symfony\Component\HttpFoundation\File\File;
/**
* Driver for managing files on the local filesystem.
* @package App\AlbumSources
*/
class LocalFilesystemSource implements IAlbumSource
{
/**
* @var Album
*/
private $album;
/**
* @var string
*/
private $parentFolder;
public function __construct(Album $album, $parentFolder)
{
$this->album = $album;
$this->parentFolder = $parentFolder;
}
public function getOriginalsFolder()
{
return '_originals';
}
public function getPathToPhoto(Photo $photo, $thumbnail = null)
{
if (is_null($thumbnail))
{
$thumbnail = $this->getOriginalsFolder();
}
return sprintf('%s/%s/%s', $this->getPathToAlbum(), $thumbnail, $photo->storage_file_name);
}
public function getUrlToPhoto(Photo $photo, $thumbnail = null)
{
$photoUrl = route('downloadPhoto', [
'albumUrlAlias' => $this->album->url_alias,
'photoFilename' => $photo->storage_file_name
]);
if (!is_null($thumbnail))
{
$photoUrl .= sprintf('?t=%s', urlencode($thumbnail));
}
return $photoUrl;
}
public function saveThumbnail(Photo $photo, $thumbnailInfo, $tempFilename)
{
$fileInfo = new File($tempFilename);
$fileInfo->move(sprintf('%s/%s', $this->getPathToAlbum(), $thumbnailInfo['name']), $photo->storage_file_name);
}
public function saveUploadedPhoto(File $uploadedFile)
{
$tempFilename = sprintf('%s/%s/%s', $this->getPathToAlbum(), $this->getOriginalsFolder(), MiscHelper::randomString(20));
$extension = $uploadedFile->guessExtension();
if (!is_null($extension))
{
$tempFilename .= '.' . $extension;
}
$uploadedFile->move(dirname($tempFilename), basename($tempFilename));
return new File($tempFilename);
}
private function getPathToAlbum()
{
return sprintf('%s/%s', $this->parentFolder, $this->album->url_alias);
}
}