blue-twilight/app/AlbumSources/LocalFilesystemSource.php

128 lines
3.4 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 saveOriginal(Photo $photo, $tempFilename)
{
$this->saveThumbnail($photo, $tempFilename, $this->getOriginalsFolder());
}
public function saveThumbnail(Photo $photo, $tempFilename, $thumbnail)
{
$fileInfo = new File($tempFilename);
$fileInfo->move(sprintf('%s/%s', $this->getPathToAlbum(), $thumbnail), $photo->storage_file_name);
}
public function saveUploadedPhoto(File $uploadedFile, $overrideFilename = null)
{
$tempFilename = sprintf(
'%s/%s/%s',
$this->getPathToAlbum(),
$this->getOriginalsFolder(),
is_null($overrideFilename) ? MiscHelper::randomString(20) : basename($overrideFilename)
);
// Only add an extension if an override filename was not given, assume this is present
if (is_null($overrideFilename))
{
$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);
}
}