1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\AssetLibrary\Application; |
4
|
|
|
|
5
|
|
|
use Spatie\MediaLibrary\MediaCollections\FileAdder; |
6
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
7
|
|
|
use Thinktomorrow\AssetLibrary\Asset; |
8
|
|
|
use Thinktomorrow\AssetLibrary\AssetHelper; |
9
|
|
|
|
10
|
|
|
class CreateAsset |
11
|
|
|
{ |
12
|
|
|
private ?string $filename = null; |
13
|
|
|
private bool $removeOriginal = false; |
14
|
|
|
|
15
|
|
|
// Available input types |
16
|
|
|
private ?UploadedFile $uploadedFile = null; |
17
|
|
|
private ?string $url = null; |
18
|
|
|
private ?string $path = null; |
19
|
|
|
private ?string $base64 = null; |
20
|
|
|
|
21
|
|
|
public function uploadedFile(UploadedFile $file): static |
22
|
|
|
{ |
23
|
|
|
$this->uploadedFile = $file; |
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function path(string $path): static |
29
|
|
|
{ |
30
|
|
|
$this->path = $path; |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function url(string $url): static |
36
|
|
|
{ |
37
|
|
|
$this->url = $url; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function base64(string $base64): static |
43
|
|
|
{ |
44
|
|
|
$this->base64 = $base64; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function filename(string $filename): static |
51
|
|
|
{ |
52
|
|
|
$this->filename = $filename; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function removeOriginal(): static |
58
|
|
|
{ |
59
|
|
|
$this->removeOriginal = true; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function save(string $disk = ''): Asset |
65
|
|
|
{ |
66
|
|
|
$asset = Asset::create(); |
67
|
|
|
|
68
|
|
|
$fileAdder = $this->getFileAdder($asset)->preservingOriginal(!$this->removeOriginal); |
69
|
|
|
|
70
|
|
|
if ($this->filename) { |
71
|
|
|
$extension = AssetHelper::getExtension($this->filename) ?: $this->guessExtension(); |
72
|
|
|
|
73
|
|
|
$fileAdder->usingFileName(AssetHelper::getBaseName($this->filename) . ($extension ? '.' . $extension : '')); |
74
|
|
|
$fileAdder->usingName(AssetHelper::getBaseName($this->filename)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$fileAdder->toMediaCollection(Asset::MEDIA_COLLECTION, $disk); |
78
|
|
|
|
79
|
|
|
return $asset->load('media'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function guessExtension(): ?string |
83
|
|
|
{ |
84
|
|
|
if ($this->uploadedFile) return $this->uploadedFile->getClientOriginalExtension(); |
85
|
|
|
if ($this->path) return AssetHelper::getExtension($this->path); |
86
|
|
|
if ($this->url) return AssetHelper::getExtension($this->url); |
87
|
|
|
|
88
|
|
|
// TODO: extract extension from base64. |
89
|
|
|
|
90
|
|
|
return null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $asset |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
private function getFileAdder($asset): FileAdder |
98
|
|
|
{ |
99
|
|
|
foreach ([ |
100
|
|
|
'uploadedFile' => 'addMedia', |
101
|
|
|
'path' => 'addMedia', |
102
|
|
|
'url' => 'addMediaFromUrl', |
103
|
|
|
'base64' => 'addMediaFromBase64' |
104
|
|
|
] as $inputProperty => $fileAdderMethod) { |
105
|
|
|
if ($this->{$inputProperty}) { |
106
|
|
|
return $asset->{$fileAdderMethod}($this->{$inputProperty}); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
throw new \InvalidArgumentException('File input is missing. You should call one of the methods: path(), url() or base64().'); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|