blue-twilight/app/AlbumSources/LocalFilesystemSource.php

114 lines
2.9 KiB
PHP
Raw Normal View History

<?php
namespace App\AlbumSources;
use App\Album;
use App\Helpers\MiscHelper;
use App\Photo;
use App\Services\PhotoService;
use Symfony\Component\HttpFoundation\File\File;
/**
* Driver for managing files on the local filesystem.
* @package App\AlbumSources
*/
class LocalFilesystemSource extends AlbumSourceBase implements IAlbumSource
{
public function deleteAlbumContents()
{
if (file_exists($this->getPathToAlbum()) && is_dir($this->getPathToAlbum()))
{
$this->recursiveDelete($this->getPathToAlbum());
}
}
public function deleteThumbnail(Photo $photo, $thumbnail = null)
{
return @unlink($this->getPathToPhoto($photo, $thumbnail));
}
public function getOriginalsFolder()
{
return '_originals';
}
public function getName()
{
return 'global.album_sources.filesystem';
}
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->configuration->location, $this->album->url_alias);
}
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);
}
}