#12: Added a new table to hold the redirects, and implemented the controller to check this table if an album is not found

This commit is contained in:
Andy Heathershaw 2017-09-03 08:54:49 +01:00
parent 2f1cbfc16c
commit b7a8222ecf
4 changed files with 81 additions and 2 deletions

25
app/AlbumRedirect.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class AlbumRedirect extends Model
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['album_id', 'source_url'];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [];
}

View File

@ -72,6 +72,11 @@ class DbHelper
->withCount('photos'); ->withCount('photos');
} }
public static function getAlbumById($albumID)
{
return Album::where('id', $albumID)->first();
}
public static function getAlbumByPath($urlPath) public static function getAlbumByPath($urlPath)
{ {
return Album::where('url_path', $urlPath)->first(); return Album::where('url_path', $urlPath)->first();

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Gallery; namespace App\Http\Controllers\Gallery;
use App\Album; use App\Album;
use App\AlbumRedirect;
use App\Facade\Theme; use App\Facade\Theme;
use App\Facade\UserConfig; use App\Facade\UserConfig;
use App\Helpers\ConfigHelper; use App\Helpers\ConfigHelper;
@ -21,8 +22,18 @@ class AlbumController extends Controller
$album = DbHelper::getAlbumByPath($albumUrlAlias); $album = DbHelper::getAlbumByPath($albumUrlAlias);
if (is_null($album)) if (is_null($album))
{ {
App::abort(404); // Check redirects
return null; $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); $this->authorizeForUser($this->getUser(), 'view', $album);

View File

@ -0,0 +1,38 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAlbumRedirectsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('album_redirects', function ($table) {
$table->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');
}
}