42 lines
1.1 KiB
PHP
42 lines
1.1 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\AlbumSources;
|
||
|
|
||
|
use App\Album;
|
||
|
use App\Helpers\MiscHelper;
|
||
|
use App\Photo;
|
||
|
use Symfony\Component\HttpFoundation\File\File;
|
||
|
|
||
|
class LocalFilesystemSource implements IAlbumSource
|
||
|
{
|
||
|
private $parentFolder;
|
||
|
|
||
|
public function __construct($parentFolder)
|
||
|
{
|
||
|
$this->parentFolder = $parentFolder;
|
||
|
}
|
||
|
|
||
|
public function getPathToPhoto(Album $album, Photo $photo)
|
||
|
{
|
||
|
return sprintf('%s/%s', $this->getPathToAlbum($album), $photo->file_name);
|
||
|
}
|
||
|
|
||
|
public function saveUploadedPhoto(Album $album, File $uploadedFile)
|
||
|
{
|
||
|
$tempFilename = sprintf('%s/photo_%s', $this->getPathToAlbum($album), 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/albums/%s', $this->parentFolder, $album->url_alias);
|
||
|
}
|
||
|
}
|