minor fixes
This commit is contained in:
@@ -7,10 +7,24 @@ namespace App\Http\Requests\Uploads;
|
||||
use App\Repositories\Uploads\UploadSessionRepository;
|
||||
use App\Services\Uploads\UploadTokenService;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
final class UploadChunkRequest extends FormRequest
|
||||
{
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$uploadError = $this->detectChunkUploadError();
|
||||
|
||||
if ($uploadError !== null && $uploadError !== UPLOAD_ERR_OK) {
|
||||
$this->logChunkUploadFailure($uploadError);
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'chunk' => [$this->messageForUploadError($uploadError)],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
{
|
||||
$user = $this->user();
|
||||
@@ -79,6 +93,63 @@ final class UploadChunkRequest extends FormRequest
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
private function detectChunkUploadError(): ?int
|
||||
{
|
||||
$uploadedFile = $this->file('chunk');
|
||||
if ($uploadedFile !== null) {
|
||||
return (int) $uploadedFile->getError();
|
||||
}
|
||||
|
||||
$rawError = data_get($_FILES, 'chunk.error');
|
||||
if ($rawError === null || $rawError === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (int) $rawError;
|
||||
}
|
||||
|
||||
private function messageForUploadError(int $error): string
|
||||
{
|
||||
return match ($error) {
|
||||
UPLOAD_ERR_INI_SIZE => 'The upload chunk exceeded PHP upload_max_filesize. Lower UPLOAD_CHUNK_MAX_BYTES or raise upload_max_filesize/post_max_size.',
|
||||
UPLOAD_ERR_FORM_SIZE => 'The upload chunk exceeded the allowed form upload size.',
|
||||
UPLOAD_ERR_PARTIAL => 'The upload chunk was only partially received. Check Nginx/PHP-FPM request handling and network stability.',
|
||||
UPLOAD_ERR_NO_FILE => 'No upload chunk file was received by PHP.',
|
||||
UPLOAD_ERR_NO_TMP_DIR => 'PHP upload_tmp_dir is missing or unavailable. Check the configured temporary upload directory on the server.',
|
||||
UPLOAD_ERR_CANT_WRITE => 'PHP could not write the upload chunk to the temporary directory. Check upload_tmp_dir permissions and free disk space.',
|
||||
UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the upload chunk before Laravel could process it.',
|
||||
default => 'The upload chunk failed before Laravel could read it. Check PHP temporary upload storage and request size limits.',
|
||||
};
|
||||
}
|
||||
|
||||
private function logChunkUploadFailure(int $error): void
|
||||
{
|
||||
$uploadTmpDir = (string) (ini_get('upload_tmp_dir') ?: sys_get_temp_dir() ?: '');
|
||||
$tmpExists = $uploadTmpDir !== '' ? is_dir($uploadTmpDir) : false;
|
||||
$tmpWritable = $tmpExists ? is_writable($uploadTmpDir) : false;
|
||||
|
||||
logger()->warning('Upload chunk failed before validation completed', [
|
||||
'session_id' => (string) $this->input('session_id'),
|
||||
'user_id' => $this->user()?->id,
|
||||
'ip' => $this->ip(),
|
||||
'upload_error' => $error,
|
||||
'upload_error_message' => $this->messageForUploadError($error),
|
||||
'content_length' => $this->server('CONTENT_LENGTH'),
|
||||
'post_max_size' => ini_get('post_max_size'),
|
||||
'upload_max_filesize' => ini_get('upload_max_filesize'),
|
||||
'upload_tmp_dir' => $uploadTmpDir,
|
||||
'tmp_exists' => $tmpExists,
|
||||
'tmp_writable' => $tmpWritable,
|
||||
'raw_files' => isset($_FILES['chunk']) ? [
|
||||
'name' => $_FILES['chunk']['name'] ?? null,
|
||||
'type' => $_FILES['chunk']['type'] ?? null,
|
||||
'size' => $_FILES['chunk']['size'] ?? null,
|
||||
'tmp_name' => $_FILES['chunk']['tmp_name'] ?? null,
|
||||
'error' => $_FILES['chunk']['error'] ?? null,
|
||||
] : null,
|
||||
]);
|
||||
}
|
||||
|
||||
private function logUnauthorized(string $reason): void
|
||||
{
|
||||
logger()->warning('Upload chunk unauthorized access', [
|
||||
|
||||
Reference in New Issue
Block a user