Deploy Ladill Give / deploy (push) Successful in 29s
External /q requests 301/307 to ladl.link; internal X-Ladill-Internal requests still serve content for platform proxying. Co-authored-by: Cursor <cursoragent@cursor.com>
61 lines
1.6 KiB
PHP
61 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* Canonical short-link URLs for the Ladill platform (ladl.link).
|
|
*/
|
|
final class LadillLink
|
|
{
|
|
public const INTERNAL_HEADER = 'X-Ladill-Internal';
|
|
|
|
public static function baseUrl(): string
|
|
{
|
|
$domain = (string) config('link.public_domain', 'ladl.link');
|
|
|
|
return 'https://'.$domain;
|
|
}
|
|
|
|
public static function url(string $slug): string
|
|
{
|
|
return rtrim(self::baseUrl(), '/').'/'.ltrim($slug, '/');
|
|
}
|
|
|
|
public static function path(string $slug, string $suffix): string
|
|
{
|
|
return self::url($slug).'/'.ltrim($suffix, '/');
|
|
}
|
|
|
|
public static function isInternalRequest(Request $request): bool
|
|
{
|
|
return $request->header(self::INTERNAL_HEADER) === '1';
|
|
}
|
|
|
|
public static function legacyRedirect(Request $request): RedirectResponse
|
|
{
|
|
$shortCode = $request->route('shortCode');
|
|
if (! is_string($shortCode) || $shortCode === '') {
|
|
abort(404);
|
|
}
|
|
|
|
$path = ltrim($request->path(), '/');
|
|
$prefix = 'q/'.$shortCode;
|
|
$suffix = '';
|
|
if ($path !== $prefix && str_starts_with($path, $prefix.'/')) {
|
|
$suffix = substr($path, strlen($prefix) + 1);
|
|
}
|
|
|
|
$target = $suffix === '' ? self::url($shortCode) : self::path($shortCode, $suffix);
|
|
if ($qs = $request->getQueryString()) {
|
|
$target .= '?'.$qs;
|
|
}
|
|
|
|
$status = in_array($request->method(), ['GET', 'HEAD'], true) ? 301 : 307;
|
|
|
|
return redirect()->away($target, $status);
|
|
}
|
|
}
|