1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\CrudPanel\Uploads\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 save(Model $entry, $value = null) |
13
|
|
|
{ |
14
|
|
|
return $this->isRepeatable && ! $this->isRelationship ? $this->saveRepeatableSingleBase64($entry, $value) : $this->saveSingleBase64($entry, $value); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
private function saveSingleBase64($entry, $value) |
18
|
|
|
{ |
19
|
|
|
$value = $value ?? CRUD::getRequest()->get($this->name); |
|
|
|
|
20
|
|
|
$previousImage = $entry->getOriginal($this->name); |
21
|
|
|
|
22
|
|
|
if (! $value && $previousImage) { |
23
|
|
|
Storage::disk($this->disk)->delete($previousImage); |
24
|
|
|
|
25
|
|
|
return null; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (Str::startsWith($value, 'data:image')) { |
29
|
|
|
if ($previousImage) { |
30
|
|
|
Storage::disk($this->disk)->delete($previousImage); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$base64Image = Str::after($value, ';base64,'); |
34
|
|
|
|
35
|
|
|
$finalPath = $this->path.$this->getFileNameWithExtension($value); |
36
|
|
|
|
37
|
|
|
Storage::disk($this->disk)->put($finalPath, base64_decode($base64Image)); |
38
|
|
|
|
39
|
|
|
return $finalPath; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $previousImage; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function saveRepeatableSingleBase64($entry, $value) |
46
|
|
|
{ |
47
|
|
|
$previousImages = $this->getPreviousRepeatableValues($entry); |
48
|
|
|
|
49
|
|
|
foreach ($value as $row => $rowValue) { |
50
|
|
|
if ($rowValue) { |
51
|
|
|
if (Str::startsWith($rowValue, 'data:image')) { |
52
|
|
|
$base64Image = Str::after($rowValue, ';base64,'); |
53
|
|
|
$finalPath = $this->path.$this->getFileNameWithExtension($rowValue); |
54
|
|
|
Storage::disk($this->disk)->put($finalPath, base64_decode($base64Image)); |
55
|
|
|
$value[$row] = $previousImages[] = $finalPath; |
56
|
|
|
|
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$imagesToDelete = array_diff($previousImages, $value); |
63
|
|
|
|
64
|
|
|
foreach ($imagesToDelete as $image) { |
65
|
|
|
Storage::disk($this->disk)->delete($image); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $value; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|