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