Skip to content
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,6 @@ class Kernel extends HttpKernel
'resolve_album_slug' => \App\Http\Middleware\ResolveAlbumSlug::class,
'response_cache' => \App\Http\Middleware\Caching\ResponseCache::class,
'album_cache_refresher' => \App\Http\Middleware\Caching\AlbumRouteCacheRefresher::class,
'legacy_id_redirect' => \App\Http\Middleware\LegacyLocalIdRedirect::class,
];
}
47 changes: 47 additions & 0 deletions app/Http/Middleware/LegacyLocalIdRedirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2026 LycheeOrg.
*/

namespace App\Http\Middleware;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use function Safe\preg_match;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class LegacyLocalIdRedirect
{
/**
* Handles redirection from old albums (Lychee v3 format).
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
*
* @return mixed
*/
public function handle(Request $request, \Closure $next)
{
// Also skip if the column does not exists.
// That way we don't crash on database which had this column removed.
$album_id = $request->route('albumId');
if ($album_id !== null &&
preg_match('/^\d{14}$/', (string) $album_id) === 1 &&
Schema::hasColumn('base_albums', 'legacy_id')
) {
$album = DB::table('base_albums')->where('legacy_id', '=', $album_id)->first();
if ($album === null) {
throw new NotFoundHttpException();
}

// redirect to new gallery route with the resolved id
return redirect()->route('gallery', $album->id, 301);
}

return $next($request);
}
}
12 changes: 8 additions & 4 deletions database/migrations/2025_05_27_081037_drop_legacy_id.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function up(): void
Schema::table('photos', function (Blueprint $table) {
$table->dropColumn(self::LEGACY_ID_NAME);
});

Schema::table('base_albums', function (Blueprint $table) {
$table->dropColumn(self::LEGACY_ID_NAME);
$table->unsignedBigInteger(self::LEGACY_ID_NAME)->nullable(true)->change();
});

DB::table('configs')
Expand All @@ -45,9 +46,12 @@ public function up(): void
*/
public function down(): void
{
Schema::table('base_albums', function (Blueprint $table) {
$table->unsignedBigInteger('legacy_id')->after('id')->nullable(false);
});
// Do not re-add the column... if it already exists.
if (!Schema::hasColumn('base_albums', self::LEGACY_ID_NAME)) {
Schema::table('base_albums', function (Blueprint $table) {
$table->unsignedBigInteger('legacy_id')->after('id')->nullable(false);
});
}
Comment thread
ildyria marked this conversation as resolved.

Schema::table('photos', function (Blueprint $table) {
$table->unsignedBigInteger('legacy_id')->after('id')->nullable(false);
Expand Down
2 changes: 1 addition & 1 deletion routes/web_v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
Route::get('/', VueController::class)->name('home')->middleware(['migration:complete']);
Route::get('/home', VueController::class)->name('homepage')->middleware(['migration:complete']);
Route::get('/flow/{albumId?}/{photoId?}', [VueController::class, 'gallery'])->name('flow')->middleware(['migration:complete', 'unlock_with_password', 'resolve_album_slug']);
Route::get('/gallery/{albumId?}/{photoId?}', [VueController::class, 'gallery'])->name('gallery')->middleware(['migration:complete', 'unlock_with_password', 'resolve_album_slug']);
Route::get('/gallery/{albumId?}/{photoId?}', [VueController::class, 'gallery'])->name('gallery')->middleware(['migration:complete', 'legacy_id_redirect', 'unlock_with_password', 'resolve_album_slug']);
Route::get('/frame/{albumId?}', [VueController::class, 'gallery'])->name('frame')->middleware(['migration:complete', 'resolve_album_slug']);
Route::get('/map/{albumId?}', [VueController::class, 'gallery'])->name('map')->middleware(['migration:complete', 'resolve_album_slug']);
Route::get('/search/{albumId?}/{photoId?}', [VueController::class, 'gallery'])->name('search')->middleware(['migration:complete', 'resolve_album_slug']);
Expand Down
Loading