summaryrefslogtreecommitdiff
path: root/app/Http/Requests/FileUploadRequest.php
blob: 6b326b3f906e8ee36105f98b1f077a01f06b3998 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php

namespace App\Http\Requests;

use App\Rules\MimeTypeRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Log;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Exceptions\HttpResponseException;

class FileUploadRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        // Public upload system - everyone can upload
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
     */
    public function rules(): array
    {
        $config = config('fileupload');

        return [
            'f' => [
                'required',
                'array',
                'max:' . $config['max_files_per_upload'],
            ],
            'f.*' => [
                'required',
                'file',
                'max:' . $config['max_file_size'], // in KB
                new MimeTypeRule($config['allowed_mimes']),
            ],
        ];
    }

    /**
     * Get custom messages for validator errors.
     */
    public function messages(): array
    {
        $config = config('fileupload');

        return [
            'f.required' => 'Please select at least one file to upload.',
            'f.array' => 'Invalid file upload format.',
            'f.max' => 'You can upload a maximum of ' . $config['max_files_per_upload'] . ' files at once.',
            'f.*.required' => 'One or more files are missing.',
            'f.*.file' => 'One or more uploads are not valid files.',
            'f.*.max' => 'One or more files exceed the maximum size of ' . $config['max_file_size'] . 'KB.',
        ];
    }

    /**
     * Handle a failed validation attempt.
     */
    protected function failedValidation(Validator $validator)
    {
        Log::warning('File upload validation failed', [
            'ip' => $this->ip(),
            'errors' => $validator->errors()->toArray(),
            'has_files' => $this->hasFile('files'),
            'all_input' => array_keys($this->all()),
        ]);

        parent::failedValidation($validator);
    }
}