From b7a8222ecf263a9e0cf34ab2628a5f394206317c Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Sun, 3 Sep 2017 08:54:49 +0100 Subject: [PATCH] #12: Added a new table to hold the redirects, and implemented the controller to check this table if an album is not found --- app/AlbumRedirect.php | 25 ++++++++++++ app/Helpers/DbHelper.php | 5 +++ .../Controllers/Gallery/AlbumController.php | 15 +++++++- ...03_084118_create_album_redirects_table.php | 38 +++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 app/AlbumRedirect.php create mode 100644 database/migrations/2017_09_03_084118_create_album_redirects_table.php diff --git a/app/AlbumRedirect.php b/app/AlbumRedirect.php new file mode 100644 index 0000000..7227ddf --- /dev/null +++ b/app/AlbumRedirect.php @@ -0,0 +1,25 @@ +withCount('photos'); } + public static function getAlbumById($albumID) + { + return Album::where('id', $albumID)->first(); + } + public static function getAlbumByPath($urlPath) { return Album::where('url_path', $urlPath)->first(); diff --git a/app/Http/Controllers/Gallery/AlbumController.php b/app/Http/Controllers/Gallery/AlbumController.php index 68e6b01..b705e1a 100644 --- a/app/Http/Controllers/Gallery/AlbumController.php +++ b/app/Http/Controllers/Gallery/AlbumController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Gallery; use App\Album; +use App\AlbumRedirect; use App\Facade\Theme; use App\Facade\UserConfig; use App\Helpers\ConfigHelper; @@ -21,8 +22,18 @@ class AlbumController extends Controller $album = DbHelper::getAlbumByPath($albumUrlAlias); if (is_null($album)) { - App::abort(404); - return null; + // Check redirects + $redirect = AlbumRedirect::where(DB::raw('\'/a\' + source_url'), $request->path())->first(); + + if (is_null($redirect)) + { + // No redirect found either - bail out + App::abort(404); + return null; + } + + $album = DbHelper::getAlbumById($redirect->album_id); + return redirect($album->url()); } $this->authorizeForUser($this->getUser(), 'view', $album); diff --git a/database/migrations/2017_09_03_084118_create_album_redirects_table.php b/database/migrations/2017_09_03_084118_create_album_redirects_table.php new file mode 100644 index 0000000..e0f1d54 --- /dev/null +++ b/database/migrations/2017_09_03_084118_create_album_redirects_table.php @@ -0,0 +1,38 @@ +increments('id'); + $table->unsignedInteger('album_id'); + $table->string('source_url'); + + $table->foreign('album_id') + ->references('id')->on('albums') + ->onDelete('cascade'); + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('album_redirects'); + } +}