Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redirection for legacy links #2227

Merged
merged 3 commits into from Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env.example
Expand Up @@ -23,6 +23,11 @@ LIVEWIRE_ENABLED=true
# enable or disable log viewer. By default it is enabled.
LOG_VIEWER_ENABLED=true

# If you spread old links of to your albums in your Lychee instance starting with
# https://lychee.text/#albumID/PhotoId
# Set this value to true to enable redirection.
LEGACY_V4_REDIRECT=false

##############################################################################
# IMPORTANT: To migrate from Lychee v3 you *MUST* use the same MySQL/MariaDB #
# server as v3. #
Expand Down
20 changes: 20 additions & 0 deletions app/Http/Controllers/RedirectController.php
Expand Up @@ -13,6 +13,7 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\View\View;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

class RedirectController extends Controller
Expand Down Expand Up @@ -87,4 +88,23 @@ public function photo(Request $request, string $albumID, ?string $photoID): Symf
throw new FrameworkException('Lychee redirection component', $e);
}
}

/**
* Redirection to landing or gallery depending on the settings.
* Otherwise attach a JS hook if legacy is enabled.
*
* @return View|SymfonyResponse
*/
public function view(): View|SymfonyResponse
{
$base_route = Configs::getValueAsBool('landing_page_enable') ? route('landing') : route('livewire-gallery');
if (config('app.legacy_v4_redirect') === false) {
return redirect($base_route);
}

return view('hook-redirection', [
'gallery' => route('livewire-gallery'),
'base' => $base_route,
]);
}
}
2 changes: 2 additions & 0 deletions config/app.php
Expand Up @@ -92,6 +92,8 @@ function renv(string $cst, ?string $default = null): string

'asset_url' => null,

'legacy_v4_redirect' => env('LEGACY_V4_REDIRECT', false),

/*
|--------------------------------------------------------------------------
| Application URL
Expand Down
13 changes: 13 additions & 0 deletions resources/views/hook-redirection.blade.php
@@ -0,0 +1,13 @@
<script>
// Dirty work around.
const hashMatch = document.location.hash.replace("#", "").split("/");
const albumID = hashMatch[0] ?? '';
const photoID = hashMatch[1] ?? '';
if (photoID !== '') {
window.location = '{{ $gallery }}/' + albumID + '/' + photoID;
} else if (albumID !== '') {
window.location = '{{ $gallery }}/' + albumID;
} else {
window.location = '{{ $base }}';
}
</script>
6 changes: 2 additions & 4 deletions routes/web-livewire.php
Expand Up @@ -4,10 +4,10 @@

use App\Enum\OauthProvidersType;
use App\Http\Controllers\Oauth;
use App\Http\Controllers\RedirectController;
use App\Livewire\Components\Pages\Gallery\Album;
use App\Livewire\Components\Pages\Gallery\Albums;
use App\Livewire\Components\Pages\Gallery\Search;
use App\Models\Configs;
use Illuminate\Support\Facades\Route;

/*
Expand Down Expand Up @@ -47,9 +47,7 @@
Route::get('/search/{albumId?}', Search::class)->name('livewire-search');
Route::get('/gallery/{albumId}/', Album::class)->name('livewire-gallery-album');
Route::get('/gallery/{albumId}/{photoId}', Album::class)->name('livewire-gallery-photo');
Route::get('/', function () {
return redirect(Configs::getValueAsBool('landing_page_enable') ? route('landing') : route('livewire-gallery'));
})->name('livewire-index');
Route::get('/', [RedirectController::class, 'view'])->name('livewire-index');
});
});