|
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
|
|
|
/** @codeCoverageIgnore */ |
|
11
|
|
|
class SingleBase64Image extends Uploader |
|
12
|
|
|
{ |
|
13
|
|
|
public function uploadFiles(Model $entry, $value = null) |
|
14
|
|
|
{ |
|
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(array_filter($previousRepeatableValues), $values); |
|
55
|
|
|
|
|
56
|
|
|
foreach ($imagesToDelete as $image) { |
|
57
|
|
|
Storage::disk($this->getDisk())->delete($image); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $values; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function shouldUploadFiles($value): bool |
|
64
|
|
|
{ |
|
65
|
|
|
return $value && is_string($value) && Str::startsWith($value, 'data:image'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function shouldKeepPreviousValueUnchanged(Model $entry, $entryValue): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return $entry->exists && is_string($entryValue) && ! Str::startsWith($entryValue, 'data:image'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getUploadedFilesFromRequest() |
|
74
|
|
|
{ |
|
75
|
|
|
return CRUD::getRequest()->get($this->getNameForRequest()); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.