|
2 | 2 |
|
3 | 3 | namespace App\Http\Traits; |
4 | 4 |
|
| 5 | +use Illuminate\Validation\ValidationException; |
5 | 6 | use Illuminate\Http\UploadedFile; |
6 | 7 | use Illuminate\Support\Arr; |
7 | 8 | use Illuminate\Support\Collection; |
@@ -38,28 +39,22 @@ protected function prepareForValidation() |
38 | 39 | if (!$base64Contents) { |
39 | 40 | return; |
40 | 41 | } |
41 | | - |
42 | | - // autogenerate filenames |
43 | | - if ($filename == 'auto'){ |
44 | | - $header = explode(';', $base64Contents, 2)[0]; |
45 | | - // Grab the image type from the header while we're at it. |
46 | | - $filename = $key . '.' . substr($header, strpos($header, '/')+1); |
47 | | - } |
48 | 42 |
|
49 | 43 | // Generate a temporary path to store the Base64 contents |
50 | 44 | $tempFilePath = tempnam(sys_get_temp_dir(), $filename); |
51 | 45 |
|
52 | | - // Store the contents using a stream, or by decoding manually |
| 46 | + // Store the contents using a stream, or throw an Error (which doesn't do anything?) |
53 | 47 | if (Str::startsWith($base64Contents, 'data:') && count(explode(',', $base64Contents)) > 1) { |
54 | | - $source = fopen($base64Contents, 'r'); |
| 48 | + $source = fopen($base64Contents, 'r'); // PHP has special processing for "data:" URL's |
55 | 49 | $destination = fopen($tempFilePath, 'w'); |
56 | 50 |
|
57 | 51 | stream_copy_to_stream($source, $destination); |
58 | 52 |
|
59 | 53 | fclose($source); |
60 | 54 | fclose($destination); |
61 | 55 | } else { |
62 | | - file_put_contents($tempFilePath, base64_decode($base64Contents, true)); |
| 56 | + // TODO - to get a better error message here, can we maybe do something with modifying the errorBag? |
| 57 | + throw new ValidationException("Need Base64 URL starting with 'data:'"); // This doesn't actually throw? |
63 | 58 | } |
64 | 59 |
|
65 | 60 | $uploadedFile = new UploadedFile($tempFilePath, $filename, null, null, true); |
|
0 commit comments