|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Fields; |
| 4 | + |
| 5 | +use Binaryk\LaravelRestify\Http\Requests\RestifyRequest; |
| 6 | +use Closure; |
| 7 | +use Illuminate\Support\Facades\Storage; |
| 8 | +use Illuminate\Support\Str; |
| 9 | + |
| 10 | +class Base64File extends File |
| 11 | +{ |
| 12 | + public function fillAttribute(RestifyRequest $request, $model, ?int $bulkRow = null) |
| 13 | + { |
| 14 | + if ($this->storeCallback instanceof Closure) { |
| 15 | + return call_user_func($this->storeCallback, $request, $model, $this->attribute); |
| 16 | + } |
| 17 | + |
| 18 | + // Delegate to parent for regular file uploads first |
| 19 | + if ($this->resolveFileFromRequest($request)) { |
| 20 | + return parent::fillAttribute($request, $model, $bulkRow); |
| 21 | + } |
| 22 | + |
| 23 | + $input = $request->input($this->attribute); |
| 24 | + |
| 25 | + if (! $input || ! is_string($input)) { |
| 26 | + return $this; |
| 27 | + } |
| 28 | + |
| 29 | + // Delegate to parent for URL inputs |
| 30 | + if (filter_var($input, FILTER_VALIDATE_URL)) { |
| 31 | + return parent::fillAttribute($request, $model, $bulkRow); |
| 32 | + } |
| 33 | + |
| 34 | + // Handle base64 data |
| 35 | + if (! $this->isBase64($input)) { |
| 36 | + return $this; |
| 37 | + } |
| 38 | + |
| 39 | + if ($this->isPrunable()) { |
| 40 | + call_user_func( |
| 41 | + $this->deleteCallback, |
| 42 | + $request, |
| 43 | + $model, |
| 44 | + $this->getStorageDisk(), |
| 45 | + $this->getStoragePath() |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + $result = $this->mergeExtraStorageColumnsForBase64($request, [ |
| 50 | + $this->attribute => $this->storeBase64File($request), |
| 51 | + ]); |
| 52 | + |
| 53 | + if (! is_array($result)) { |
| 54 | + return $model->{$this->attribute} = $result; |
| 55 | + } |
| 56 | + |
| 57 | + foreach ($result as $key => $value) { |
| 58 | + if ($model->isFillable($key)) { |
| 59 | + $model->{$key} = $value; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return $this; |
| 64 | + } |
| 65 | + |
| 66 | + protected function storeBase64File(RestifyRequest $request): string |
| 67 | + { |
| 68 | + $base64Data = $request->input($this->attribute); |
| 69 | + $imageData = $this->decodeBase64($base64Data); |
| 70 | + $extension = $this->detectExtension($base64Data); |
| 71 | + |
| 72 | + $filename = $this->resolveFilename($request, $extension); |
| 73 | + $directory = trim($this->getStorageDir(), '/'); |
| 74 | + $path = $directory ? "{$directory}/{$filename}" : $filename; |
| 75 | + |
| 76 | + Storage::disk($this->getStorageDisk())->put($path, $imageData); |
| 77 | + |
| 78 | + return $path; |
| 79 | + } |
| 80 | + |
| 81 | + protected function resolveFilename(RestifyRequest $request, string $extension): string |
| 82 | + { |
| 83 | + if (! $this->storeAs) { |
| 84 | + $this->customFilename = null; |
| 85 | + $this->useCustomFilenameForOriginal = false; |
| 86 | + |
| 87 | + return Str::uuid().'.'.$extension; |
| 88 | + } |
| 89 | + |
| 90 | + $isCallable = is_callable($this->storeAs); |
| 91 | + $filename = $isCallable |
| 92 | + ? call_user_func($this->storeAs, $request) |
| 93 | + : $this->storeAs; |
| 94 | + |
| 95 | + if (empty($filename)) { |
| 96 | + $this->customFilename = null; |
| 97 | + $this->useCustomFilenameForOriginal = false; |
| 98 | + |
| 99 | + return Str::uuid().'.'.$extension; |
| 100 | + } |
| 101 | + |
| 102 | + // Smart extension handling - append if missing |
| 103 | + if ($extension && ! str_ends_with(strtolower($filename), '.'.$extension)) { |
| 104 | + $filename = $filename.'.'.$extension; |
| 105 | + } |
| 106 | + |
| 107 | + $this->customFilename = $filename; |
| 108 | + $this->useCustomFilenameForOriginal = $isCallable; |
| 109 | + |
| 110 | + return $filename; |
| 111 | + } |
| 112 | + |
| 113 | + protected function mergeExtraStorageColumnsForBase64(RestifyRequest $request, array $attributes): array |
| 114 | + { |
| 115 | + $base64Data = $request->input($this->attribute); |
| 116 | + |
| 117 | + if ($this->originalNameColumn) { |
| 118 | + $attributes[$this->originalNameColumn] = ($this->useCustomFilenameForOriginal && $this->customFilename) |
| 119 | + ? $this->customFilename |
| 120 | + : $this->detectOriginalName($base64Data); |
| 121 | + } |
| 122 | + |
| 123 | + if ($this->sizeColumn) { |
| 124 | + $attributes[$this->sizeColumn] = $this->calculateDecodedSize($base64Data); |
| 125 | + } |
| 126 | + |
| 127 | + return $attributes; |
| 128 | + } |
| 129 | + |
| 130 | + protected function detectOriginalName(string $base64Data): string |
| 131 | + { |
| 132 | + $extension = $this->detectExtension($base64Data); |
| 133 | + |
| 134 | + return 'base64-upload.'.$extension; |
| 135 | + } |
| 136 | + |
| 137 | + protected function calculateDecodedSize(string $base64Data): int |
| 138 | + { |
| 139 | + $parts = explode(',', $base64Data); |
| 140 | + $encoded = count($parts) > 1 ? $parts[1] : $parts[0]; |
| 141 | + |
| 142 | + return (int) (strlen($encoded) * 3 / 4); |
| 143 | + } |
| 144 | + |
| 145 | + protected function isBase64(string $data): bool |
| 146 | + { |
| 147 | + // Check for data URI format |
| 148 | + if (str_starts_with($data, 'data:')) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + // Check for raw base64 (no data URI prefix) |
| 153 | + return base64_encode(base64_decode($data, true)) === $data; |
| 154 | + } |
| 155 | + |
| 156 | + protected function decodeBase64(string $base64Data): string |
| 157 | + { |
| 158 | + $parts = explode(',', $base64Data); |
| 159 | + |
| 160 | + return base64_decode(count($parts) > 1 ? $parts[1] : $parts[0]); |
| 161 | + } |
| 162 | + |
| 163 | + protected function detectExtension(string $base64Data): string |
| 164 | + { |
| 165 | + return match (true) { |
| 166 | + str_contains($base64Data, 'image/png') => 'png', |
| 167 | + str_contains($base64Data, 'image/jpeg'), str_contains($base64Data, 'image/jpg') => 'jpg', |
| 168 | + str_contains($base64Data, 'image/gif') => 'gif', |
| 169 | + str_contains($base64Data, 'image/webp') => 'webp', |
| 170 | + str_contains($base64Data, 'image/svg+xml'), str_contains($base64Data, 'image/svg') => 'svg', |
| 171 | + str_contains($base64Data, 'application/pdf') => 'pdf', |
| 172 | + str_contains($base64Data, 'text/plain') => 'txt', |
| 173 | + str_contains($base64Data, 'application/json') => 'json', |
| 174 | + default => 'bin', |
| 175 | + }; |
| 176 | + } |
| 177 | +} |
0 commit comments