|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\AssetLibrary\Application; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Thinktomorrow\AssetLibrary\Asset; |
|
7
|
|
|
use Thinktomorrow\AssetLibrary\HasAsset; |
|
8
|
|
|
|
|
9
|
|
|
class AddAsset |
|
10
|
|
|
{ |
|
11
|
|
|
private $order; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Add a file to this model, accepts a type and locale to be saved with the file. |
|
15
|
|
|
* |
|
16
|
|
|
* @param \Thinktomorrow\AssetLibrary\HasAsset $model |
|
17
|
|
|
* @param $file |
|
18
|
|
|
* @param string $type |
|
19
|
|
|
* @param string $locale |
|
20
|
|
|
* @param string|null $filename |
|
21
|
|
|
* @param string $collection |
|
22
|
|
|
* @param string $disk |
|
23
|
|
|
* @return Asset |
|
24
|
|
|
*/ |
|
25
|
61 |
|
public function add(HasAsset $model, $file, string $type, string $locale, ?string $filename = null, string $collection = 'default', string $disk = ''): Asset |
|
26
|
|
|
{ |
|
27
|
61 |
|
$asset = $this->uploadAssetFromInput($file, $filename, $collection, $disk); |
|
28
|
|
|
|
|
29
|
61 |
|
$this->attachAssetToModel($asset, $model, $type, $locale); |
|
30
|
|
|
|
|
31
|
61 |
|
return $asset; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Adds multiple files to this model, accepts a type and locale to be saved with the file. |
|
36
|
|
|
* |
|
37
|
|
|
* @param $files |
|
38
|
|
|
* @param string $type |
|
39
|
|
|
* @param string $locale |
|
40
|
|
|
* @throws \Spatie\MediaLibrary\MediaCollections\Exceptions\FileCannotBeAdded |
|
41
|
|
|
*/ |
|
42
|
|
|
public function addMultiple(HasAsset $model, Collection $files, string $type, string $locale): Collection |
|
43
|
5 |
|
{ |
|
44
|
|
|
$assets = collect(); |
|
45
|
5 |
|
|
|
46
|
|
|
$files->each(function ($file, $filename) use ($assets, $model, $type, $locale) { |
|
47
|
|
|
$filename = is_string($filename) ? $filename : ''; |
|
48
|
5 |
|
$assets->push($this->add($model, $file, $type, $locale, $filename)); |
|
49
|
5 |
|
}); |
|
50
|
5 |
|
|
|
51
|
|
|
return $assets; |
|
52
|
5 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function setOrder(int $order = null): self |
|
55
|
13 |
|
{ |
|
56
|
|
|
$this->order = $order; |
|
57
|
13 |
|
|
|
58
|
|
|
return $this; |
|
59
|
13 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Attaches this asset instance to the given model and |
|
63
|
|
|
* sets the type and locale to the given values and |
|
64
|
|
|
* returns the model with the asset relationship. |
|
65
|
|
|
* |
|
66
|
|
|
* @param HasAsset $model |
|
67
|
|
|
* @param string $type |
|
68
|
|
|
* @param null|string $locale |
|
69
|
|
|
* @param null|int $order |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
private function attachAssetToModel(Asset $asset, HasAsset $model, string $type, string $locale): void |
|
73
|
|
|
{ |
|
74
|
61 |
|
$model->assetRelation()->attach($asset, ['type' => $type, 'locale' => $locale, 'order' => $this->order]); |
|
75
|
|
|
} |
|
76
|
61 |
|
|
|
77
|
61 |
|
private function uploadAssetFromInput($file, ?string $filename = null, string $collection = 'default', string $disk = ''): Asset |
|
78
|
|
|
{ |
|
79
|
61 |
|
if ($file instanceof Asset) { |
|
80
|
|
|
return $file; |
|
81
|
61 |
|
} |
|
82
|
53 |
|
|
|
83
|
|
|
if (is_string($file)) { |
|
84
|
|
|
if (! $filename) { |
|
85
|
21 |
|
$filename = md5(time()).'.'.substr($file, 11, strpos($file, ';') - 11); |
|
86
|
6 |
|
} |
|
87
|
1 |
|
|
|
88
|
|
|
return AssetUploader::uploadFromBase64($file, $filename, $collection, $disk); |
|
89
|
|
|
} |
|
90
|
6 |
|
|
|
91
|
|
|
return AssetUploader::upload($file, $filename, $collection, $disk); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|