Photos and album contents are now physically deleted from disk

This commit is contained in:
Andy Heathershaw 2016-09-09 11:09:03 +01:00
parent 71f6ed8979
commit 504134caa7
6 changed files with 91 additions and 5 deletions

View File

@ -8,7 +8,19 @@ use Symfony\Component\HttpFoundation\File\File;
interface IAlbumSource interface IAlbumSource
{ {
function getOriginalsFolder(); /**
* Deletes an entire album's media contents.
* @return void
*/
function deleteAlbumContents();
/**
* Deletes a thumbnail file for a photo.
* @param Photo $photo Photo to delete the thumbnail from.
* @param string $thumbnail Thumbnail to delete (or null to delete the original.)
* @return void
*/
function deleteThumbnail(Photo $photo, $thumbnail = null);
/** /**
* Gets the absolute path to the given photo file. * Gets the absolute path to the given photo file.

View File

@ -5,6 +5,7 @@ namespace App\AlbumSources;
use App\Album; use App\Album;
use App\Helpers\MiscHelper; use App\Helpers\MiscHelper;
use App\Photo; use App\Photo;
use App\Services\PhotoService;
use Symfony\Component\HttpFoundation\File\File; use Symfony\Component\HttpFoundation\File\File;
/** /**
@ -29,6 +30,16 @@ class LocalFilesystemSource implements IAlbumSource
$this->parentFolder = $parentFolder; $this->parentFolder = $parentFolder;
} }
public function deleteAlbumContents()
{
$this->recursiveDelete($this->getPathToAlbum());
}
public function deleteThumbnail(Photo $photo, $thumbnail = null)
{
return unlink($this->getPathToPhoto($photo, $thumbnail));
}
public function getOriginalsFolder() public function getOriginalsFolder()
{ {
return '_originals'; return '_originals';
@ -83,4 +94,29 @@ class LocalFilesystemSource implements IAlbumSource
{ {
return sprintf('%s/%s', $this->parentFolder, $this->album->url_alias); return sprintf('%s/%s', $this->parentFolder, $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);
}
} }

View File

@ -20,7 +20,7 @@ class ImageHelper
$thumbnailHeight = intval($thumbnailInfo['height']); $thumbnailHeight = intval($thumbnailInfo['height']);
} }
$thumbnailImageResource = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight); /*$thumbnailImageResource = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight);
imagecopyresized( imagecopyresized(
$thumbnailImageResource, $thumbnailImageResource,
@ -33,7 +33,8 @@ class ImageHelper
$thumbnailHeight, $thumbnailHeight,
$photo->width, $photo->width,
$photo->height $photo->height
); );*/
$thumbnailImageResource = imagescale($gdImageResource, $thumbnailWidth, $thumbnailHeight, $this->getInterpolationMethod());
// TODO make the /tmp folder configurable // TODO make the /tmp folder configurable
$tempName = tempnam('/tmp', 'btw_thumb_'); $tempName = tempnam('/tmp', 'btw_thumb_');
@ -92,7 +93,7 @@ class ImageHelper
public function rotateImage($imageResource, $angle) public function rotateImage($imageResource, $angle)
{ {
imagesetinterpolation($imageResource, IMG_SINC); imagesetinterpolation($imageResource, $this->getInterpolationMethod());
return imagerotate($imageResource, $angle, 0); return imagerotate($imageResource, $angle, 0);
} }
@ -118,4 +119,9 @@ class ImageHelper
break; break;
} }
} }
private function getInterpolationMethod()
{
return IMG_SINC;
}
} }

View File

@ -7,6 +7,8 @@ use App\Facade\Theme;
use App\Facade\UserConfig; use App\Facade\UserConfig;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Requests; use App\Http\Requests;
use App\Photo;
use App\Services\PhotoService;
use App\Upload; use App\Upload;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
@ -59,6 +61,16 @@ class AlbumController extends Controller
$this->authorize('admin-access'); $this->authorize('admin-access');
$album = $this->loadAlbum($id); $album = $this->loadAlbum($id);
// Delete all the photo files
/** @var Photo $photo */
foreach ($album->photos as $photo)
{
$photoService = new PhotoService($photo);
$photoService->delete();
}
$album->getAlbumSource()->deleteAlbumContents();
$album->delete(); $album->delete();
return redirect(route('albums.index')); return redirect(route('albums.index'));

View File

@ -92,7 +92,8 @@ class PhotoController extends Controller
return null; return null;
} }
$photo->delete(); $photoService = new PhotoService($photo);
$photoService->delete();
return back(); return back();
} }

View File

@ -118,6 +118,25 @@ class PhotoService
$this->regenerateThumbnails($originalPhotoResource); $this->regenerateThumbnails($originalPhotoResource);
} }
public function delete()
{
// Remove all thumbnails first - so if any fail, we don't delete the original
$themeInfo = $this->themeHelper->info();
$thumbnailsRequired = $themeInfo['thumbnails'];
/** @var mixed $thumbnail */
foreach ($thumbnailsRequired as $thumbnail)
{
$this->albumSource->deleteThumbnail($this->photo, $thumbnail['name']);
}
// Next remove the original photo
$this->albumSource->deleteThumbnail($this->photo);
// Finally remove the database record
$this->photo->delete();
}
public function regenerateThumbnails($originalPhotoResource = null) public function regenerateThumbnails($originalPhotoResource = null)
{ {
if (is_null($originalPhotoResource)) if (is_null($originalPhotoResource))