summaryrefslogtreecommitdiff
path: root/app/Providers/AppServiceProvider.php
blob: 6fd3a15407972e890f2e79ed7135ec63e52d5ce7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        // Configure file upload rate limiting
        RateLimiter::for('fileupload', function (Request $request) {
            $config = config('fileupload.rate_limit');

            return Limit::perMinute($config['max_attempts'])
                ->by($request->ip())
                ->response(function (Request $request, array $headers) {
                    \Log::warning('Rate limit exceeded', [
                        'ip' => $request->ip(),
                        'url' => $request->fullUrl(),
                        'retry_after' => $headers['Retry-After'] ?? 60
                    ]);

                    return response()->json([
                        'error' => 'Too many upload attempts. Please try again later.',
                        'retry_after' => $headers['Retry-After'] ?? 60
                    ], 429);
                });
        });
    }
}