From 504134caa73c4918bf3fd818c1dff7bc59dc030c Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Fri, 9 Sep 2016 11:09:03 +0100 Subject: [PATCH] Photos and album contents are now physically deleted from disk --- app/AlbumSources/IAlbumSource.php | 14 +++++++- app/AlbumSources/LocalFilesystemSource.php | 36 +++++++++++++++++++ app/Helpers/ImageHelper.php | 12 +++++-- .../Controllers/Admin/AlbumController.php | 12 +++++++ .../Controllers/Admin/PhotoController.php | 3 +- app/Services/PhotoService.php | 19 ++++++++++ 6 files changed, 91 insertions(+), 5 deletions(-) diff --git a/app/AlbumSources/IAlbumSource.php b/app/AlbumSources/IAlbumSource.php index e24c142..f61206c 100644 --- a/app/AlbumSources/IAlbumSource.php +++ b/app/AlbumSources/IAlbumSource.php @@ -8,7 +8,19 @@ use Symfony\Component\HttpFoundation\File\File; 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. diff --git a/app/AlbumSources/LocalFilesystemSource.php b/app/AlbumSources/LocalFilesystemSource.php index d9b10cb..c071b40 100644 --- a/app/AlbumSources/LocalFilesystemSource.php +++ b/app/AlbumSources/LocalFilesystemSource.php @@ -5,6 +5,7 @@ namespace App\AlbumSources; use App\Album; use App\Helpers\MiscHelper; use App\Photo; +use App\Services\PhotoService; use Symfony\Component\HttpFoundation\File\File; /** @@ -29,6 +30,16 @@ class LocalFilesystemSource implements IAlbumSource $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() { return '_originals'; @@ -83,4 +94,29 @@ class LocalFilesystemSource implements IAlbumSource { 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); + } } \ No newline at end of file diff --git a/app/Helpers/ImageHelper.php b/app/Helpers/ImageHelper.php index fd54a43..d4ade8e 100644 --- a/app/Helpers/ImageHelper.php +++ b/app/Helpers/ImageHelper.php @@ -20,7 +20,7 @@ class ImageHelper $thumbnailHeight = intval($thumbnailInfo['height']); } - $thumbnailImageResource = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight); + /*$thumbnailImageResource = imagecreatetruecolor($thumbnailWidth, $thumbnailHeight); imagecopyresized( $thumbnailImageResource, @@ -33,7 +33,8 @@ class ImageHelper $thumbnailHeight, $photo->width, $photo->height - ); + );*/ + $thumbnailImageResource = imagescale($gdImageResource, $thumbnailWidth, $thumbnailHeight, $this->getInterpolationMethod()); // TODO make the /tmp folder configurable $tempName = tempnam('/tmp', 'btw_thumb_'); @@ -92,7 +93,7 @@ class ImageHelper public function rotateImage($imageResource, $angle) { - imagesetinterpolation($imageResource, IMG_SINC); + imagesetinterpolation($imageResource, $this->getInterpolationMethod()); return imagerotate($imageResource, $angle, 0); } @@ -118,4 +119,9 @@ class ImageHelper break; } } + + private function getInterpolationMethod() + { + return IMG_SINC; + } } \ No newline at end of file diff --git a/app/Http/Controllers/Admin/AlbumController.php b/app/Http/Controllers/Admin/AlbumController.php index 99cc8ac..6a00afd 100644 --- a/app/Http/Controllers/Admin/AlbumController.php +++ b/app/Http/Controllers/Admin/AlbumController.php @@ -7,6 +7,8 @@ use App\Facade\Theme; use App\Facade\UserConfig; use App\Http\Controllers\Controller; use App\Http\Requests; +use App\Photo; +use App\Services\PhotoService; use App\Upload; use Illuminate\Http\Request; use Illuminate\Support\Facades\App; @@ -59,6 +61,16 @@ class AlbumController extends Controller $this->authorize('admin-access'); $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(); return redirect(route('albums.index')); diff --git a/app/Http/Controllers/Admin/PhotoController.php b/app/Http/Controllers/Admin/PhotoController.php index 2d2801e..770d1e1 100644 --- a/app/Http/Controllers/Admin/PhotoController.php +++ b/app/Http/Controllers/Admin/PhotoController.php @@ -92,7 +92,8 @@ class PhotoController extends Controller return null; } - $photo->delete(); + $photoService = new PhotoService($photo); + $photoService->delete(); return back(); } diff --git a/app/Services/PhotoService.php b/app/Services/PhotoService.php index bcafae0..096269f 100644 --- a/app/Services/PhotoService.php +++ b/app/Services/PhotoService.php @@ -118,6 +118,25 @@ class PhotoService $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) { if (is_null($originalPhotoResource))