| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Backpack\CRUD\app\Library\Uploaders; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Facades\Storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class SingleBase64Image extends Uploader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     public function uploadFiles(Model $entry, $value = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $value = $value ?? CRUD::getRequest()->get($this->getName()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $previousImage = $this->getPreviousFiles($entry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         if (! $value && $previousImage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             Storage::disk($this->getDisk())->delete($previousImage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         if (Str::startsWith($value, 'data:image')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             if ($previousImage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                 Storage::disk($this->getDisk())->delete($previousImage); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $base64Image = Str::after($value, ';base64,'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $finalPath = $this->getPath().$this->getFileName($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             Storage::disk($this->getDisk())->put($finalPath, base64_decode($base64Image)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             return $finalPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return $previousImage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function uploadRepeatableFiles($values, $previousRepeatableValues, $entry = null) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         foreach ($values as $row => $rowValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             if ($rowValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 if (Str::startsWith($rowValue, 'data:image')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                     $base64Image = Str::after($rowValue, ';base64,'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                     $finalPath = $this->getPath().$this->getFileName($rowValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                     Storage::disk($this->getDisk())->put($finalPath, base64_decode($base64Image)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     $values[$row] = $previousRepeatableValues[] = $finalPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                     continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $imagesToDelete = array_diff($previousRepeatableValues, $values); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         foreach ($imagesToDelete as $image) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             Storage::disk($this->getDisk())->delete($image); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         return $values; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     protected function shouldUploadFiles($value): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         return $value && is_string($value) && Str::startsWith($value, 'data:image'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     protected function shouldKeepPreviousValueUnchanged(Model $entry, $entryValue): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $entry->exists && is_string($entryValue) && ! Str::startsWith($entryValue, 'data:image'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  |