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 uploadFile(Model $entry, $value = null) |
13
|
|
|
{ |
14
|
|
|
$value = $value ?? CRUD::getRequest()->get($this->getName()); |
|
|
|
|
15
|
|
|
$previousImage = $entry->getOriginal($this->getName()); |
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 uploadRepeatableFile(Model $entry, $value = null) |
40
|
|
|
{ |
41
|
|
|
$previousImages = $this->getPreviousRepeatableValues($entry); |
42
|
|
|
|
43
|
|
|
foreach ($value as $row => $rowValue) { |
44
|
|
|
if ($rowValue) { |
45
|
|
|
if (Str::startsWith($rowValue, 'data:image')) { |
46
|
|
|
$base64Image = Str::after($rowValue, ';base64,'); |
47
|
|
|
$finalPath = $this->getPath().$this->getFileName($rowValue); |
48
|
|
|
Storage::disk($this->getDisk())->put($finalPath, base64_decode($base64Image)); |
49
|
|
|
$value[$row] = $previousImages[] = $finalPath; |
50
|
|
|
|
51
|
|
|
continue; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$imagesToDelete = array_diff($previousImages, $value); |
57
|
|
|
|
58
|
|
|
foreach ($imagesToDelete as $image) { |
59
|
|
|
Storage::disk($this->getDisk())->delete($image); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $value; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|