blue-twilight/app/AlbumSources/LocalFilesystemSource.php

77 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
{
private $parentFolder;
public function __construct($parentFolder)
{
$this->parentFolder = $parentFolder;
}
public function getOriginalsFolder()
{
return '_originals';
}
public function getPathToPhoto(Album $album, Photo $photo, $thumbnail = null)
{
if (is_null($thumbnail))
{
$thumbnail = $this->getOriginalsFolder();
}
return sprintf('%s/%s/%s', $this->getPathToAlbum($album), $thumbnail, $photo->storage_file_name);
}
public function getUrlToPhoto(Album $album, Photo $photo, $thumbnail = null)
{
$photoUrl = route('downloadPhoto', [
'albumUrlAlias' => $album->url_alias,
'photoFilename' => $photo->storage_file_name
]);
if (!is_null($thumbnail))
{
$photoUrl .= sprintf('?t=%s', urlencode($thumbnail));
}
return $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->storage_file_name);
}
public function saveUploadedPhoto(Album $album, File $uploadedFile)
{
$tempFilename = sprintf('%s/%s/%s', $this->getPathToAlbum($album), $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(Album $album)
{
return sprintf('%s/%s', $this->parentFolder, $album->url_alias);
}
}