57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class HandleUploadErrors
|
|
{
|
|
public function handle(Request $request, Closure $next): Response
|
|
{
|
|
// Check for PHP upload errors before processing
|
|
if ($request->isMethod('POST') && $request->header('Content-Type', '') === 'application/json') {
|
|
$content = $request->getContent();
|
|
$data = json_decode($content, true);
|
|
|
|
if (json_last_error() === JSON_ERROR_NONE) {
|
|
// Check if any file uploads are present in the request
|
|
$this->checkUploadLimits($data, $request);
|
|
}
|
|
}
|
|
|
|
$response = $next($request);
|
|
|
|
return $response;
|
|
}
|
|
|
|
protected function checkUploadLimits(array $data, Request $request): void
|
|
{
|
|
// Check PHP upload limits
|
|
$uploadMax = $this->parseSize(ini_get('upload_max_filesize'));
|
|
$postMax = $this->parseSize(ini_get('post_max_size'));
|
|
|
|
// Log current limits for debugging
|
|
Log::info('Upload limits', [
|
|
'upload_max_filesize' => ini_get('upload_max_filesize'),
|
|
'post_max_size' => ini_get('post_max_size'),
|
|
'content_length' => $request->header('Content-Length'),
|
|
]);
|
|
}
|
|
|
|
protected function parseSize(string $size): int
|
|
{
|
|
$unit = strtolower(substr($size, -1));
|
|
$value = (int) $size;
|
|
|
|
return match ($unit) {
|
|
'g' => $value * 1024 * 1024 * 1024,
|
|
'm' => $value * 1024 * 1024,
|
|
'k' => $value * 1024,
|
|
default => $value,
|
|
};
|
|
}
|
|
}
|