2024-09-20 10:37:16 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\Models\ModVersion;
|
2024-09-25 17:02:03 -04:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
2024-09-20 10:37:16 -04:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2024-09-25 17:02:03 -04:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\RateLimiter;
|
2024-09-20 10:37:16 -04:00
|
|
|
|
|
|
|
class ModVersionController extends Controller
|
|
|
|
{
|
2024-09-25 17:02:03 -04:00
|
|
|
use AuthorizesRequests;
|
|
|
|
|
|
|
|
public function show(Request $request, int $modId, string $slug, string $version): RedirectResponse
|
2024-09-20 10:37:16 -04:00
|
|
|
{
|
2024-09-25 17:02:03 -04:00
|
|
|
$modVersion = ModVersion::whereModId($modId)
|
|
|
|
->whereVersion($version)
|
|
|
|
->firstOrFail();
|
2024-09-20 10:37:16 -04:00
|
|
|
|
2024-09-25 17:02:03 -04:00
|
|
|
if ($modVersion->mod->slug !== $slug) {
|
2024-09-20 10:37:16 -04:00
|
|
|
abort(404);
|
|
|
|
}
|
|
|
|
|
2024-09-25 17:02:03 -04:00
|
|
|
$this->authorize('view', $modVersion);
|
|
|
|
|
|
|
|
// Rate limit the downloads.
|
|
|
|
$rateKey = 'mod-download:'.($request->user()?->id ?: $request->ip());
|
|
|
|
if (RateLimiter::tooManyAttempts($rateKey, maxAttempts: 5)) { // Max attempts is per minute.
|
|
|
|
abort(429);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment downloads counts in the background.
|
|
|
|
defer(fn () => $modVersion->incrementDownloads());
|
|
|
|
|
|
|
|
// Increment the rate limiter.
|
|
|
|
RateLimiter::increment($rateKey);
|
2024-09-20 10:37:16 -04:00
|
|
|
|
2024-09-25 17:02:03 -04:00
|
|
|
// Redirect to the download link, using a 307 status code to prevent browsers from caching.
|
2024-09-20 10:46:26 -04:00
|
|
|
return redirect($modVersion->link, 307);
|
2024-09-20 10:37:16 -04:00
|
|
|
}
|
|
|
|
}
|