Upload beautify
This commit is contained in:
@@ -2,7 +2,12 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use App\Services\Upload\Contracts\UploadDraftServiceInterface;
|
||||
use App\Services\Upload\UploadDraftService;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@@ -14,7 +19,10 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
// Bind UploadDraftService interface to implementation
|
||||
$this->app->singleton(UploadDraftServiceInterface::class, function ($app) {
|
||||
return new UploadDraftService($app->make('filesystem'));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,11 +30,14 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
$this->configureUploadRateLimiters();
|
||||
|
||||
// Provide toolbar counts and user info to layout views (port of legacy toolbar logic)
|
||||
View::composer(['layouts.nova', 'layouts.nova.*'], function ($view) {
|
||||
$uploadCount = $favCount = $msgCount = $noticeCount = 0;
|
||||
$avatar = null;
|
||||
$displayName = null;
|
||||
$userId = null;
|
||||
|
||||
if (Auth::check()) {
|
||||
$userId = Auth::id();
|
||||
@@ -72,4 +83,40 @@ class AppServiceProvider extends ServiceProvider
|
||||
$view->with(compact('userId','uploadCount', 'favCount', 'msgCount', 'noticeCount', 'avatar', 'displayName'));
|
||||
});
|
||||
}
|
||||
|
||||
private function configureUploadRateLimiters(): void
|
||||
{
|
||||
RateLimiter::for('uploads-init', function (Request $request): array {
|
||||
return $this->buildUploadLimits($request, 'init');
|
||||
});
|
||||
|
||||
RateLimiter::for('uploads-finish', function (Request $request): array {
|
||||
return $this->buildUploadLimits($request, 'finish');
|
||||
});
|
||||
|
||||
RateLimiter::for('uploads-status', function (Request $request): array {
|
||||
return $this->buildUploadLimits($request, 'status');
|
||||
});
|
||||
}
|
||||
|
||||
private function buildUploadLimits(Request $request, string $key): array
|
||||
{
|
||||
$config = (array) config('uploads.rate_limits.' . $key, []);
|
||||
$decay = (int) config('uploads.rate_limits.decay_minutes', 1);
|
||||
$perUser = (int) ($config['per_user'] ?? 0);
|
||||
$perIp = (int) ($config['per_ip'] ?? 0);
|
||||
|
||||
$limits = [];
|
||||
|
||||
if ($perUser > 0) {
|
||||
$userId = $request->user()?->id ?? 'guest';
|
||||
$limits[] = Limit::perMinutes($decay, $perUser)->by('u:' . $userId);
|
||||
}
|
||||
|
||||
if ($perIp > 0) {
|
||||
$limits[] = Limit::perMinutes($decay, $perIp)->by('ip:' . $request->ip());
|
||||
}
|
||||
|
||||
return $limits;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user