|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\Chief\Media\Application; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Thinktomorrow\AssetLibrary\Asset; |
|
8
|
|
|
use Thinktomorrow\AssetLibrary\HasAsset; |
|
9
|
|
|
use Thinktomorrow\Chief\Fields\Types\MediaField; |
|
10
|
|
|
use Thinktomorrow\AssetLibrary\Application\AddAsset; |
|
11
|
|
|
use Thinktomorrow\Chief\Media\DuplicateAssetException; |
|
12
|
|
|
use Thinktomorrow\AssetLibrary\Application\DetachAsset; |
|
13
|
|
|
use Thinktomorrow\AssetLibrary\Application\ReplaceAsset; |
|
14
|
|
|
use Thinktomorrow\AssetLibrary\Application\AssetUploader; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractMediaFieldHandler |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var ReplaceAsset */ |
|
19
|
|
|
protected $replaceAsset; |
|
20
|
|
|
|
|
21
|
|
|
/** @var AddAsset */ |
|
22
|
|
|
protected $addAsset; |
|
23
|
|
|
|
|
24
|
|
|
/** @var DetachAsset */ |
|
25
|
|
|
protected $detachAsset; |
|
26
|
|
|
|
|
27
|
|
|
/** @var AssetUploader */ |
|
28
|
|
|
protected $assetUploader; |
|
29
|
|
|
|
|
30
|
|
|
final public function __construct(AddAsset $addAsset, ReplaceAsset $replaceAsset, DetachAsset $detachAsset, AssetUploader $assetUploader) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->replaceAsset = $replaceAsset; |
|
33
|
|
|
$this->addAsset = $addAsset; |
|
34
|
|
|
$this->detachAsset = $detachAsset; |
|
35
|
|
|
$this->assetUploader = $assetUploader; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
protected function mediaRequest(array $requests, MediaField $field, Request $request): MediaRequest |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
$mediaRequest = new MediaRequest(); |
|
41
|
|
|
|
|
42
|
|
|
foreach($requests as $requestData){ |
|
43
|
|
|
foreach($requestData as $locale => $filesPerLocale) { |
|
44
|
|
|
foreach($filesPerLocale as $action => $files) { |
|
45
|
|
|
|
|
46
|
|
|
if(!is_array($files) || !in_array($action, [MediaRequest::NEW, MediaRequest::REPLACE, MediaRequest::DETACH])) { |
|
47
|
|
|
throw new \InvalidArgumentException('Malformed request data. Files are expected to be passed in a localized array.'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
foreach($files as $k => $file) { |
|
51
|
|
|
$mediaRequest->add($action, new MediaRequestInput( |
|
52
|
|
|
$file, $locale, $field->getKey(), [ |
|
53
|
|
|
'index' => $k, // index key is used for replace method to indicate the current asset id |
|
54
|
|
|
'existing_asset' => $this->refersToExistingAsset($file), |
|
55
|
|
|
] |
|
56
|
|
|
)); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $mediaRequest; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function refersToExistingAsset($value): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return is_int($value); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param HasAsset $model |
|
72
|
|
|
* @param MediaRequestInput $mediaRequestInput |
|
73
|
|
|
* @return Asset |
|
74
|
|
|
* @throws DuplicateAssetException |
|
75
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\FileCannotBeAdded |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function newExistingAsset(HasAsset $model, MediaRequestInput $mediaRequestInput): Asset |
|
78
|
|
|
{ |
|
79
|
|
|
$existingAsset = Asset::find($mediaRequestInput->value()); |
|
80
|
|
|
|
|
81
|
|
|
if ($model->assetRelation()->where('asset_pivots.type', $mediaRequestInput->type())->where('asset_pivots.locale', $mediaRequestInput->locale())->get()->contains($existingAsset)) { |
|
82
|
|
|
throw new DuplicateAssetException(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $this->addAsset->add($model, $existingAsset, $mediaRequestInput->type(), $mediaRequestInput->locale()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $filename |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function sluggifyFilename(string $filename): string |
|
93
|
|
|
{ |
|
94
|
|
|
if(false === strpos($filename, '.')) return $filename; |
|
95
|
|
|
|
|
96
|
|
|
$extension = substr($filename, strrpos($filename, '.') + 1); |
|
97
|
|
|
$filename = substr($filename, 0, strrpos($filename, '.')); |
|
98
|
|
|
|
|
99
|
|
|
return Str::slug($filename) . '.' . $extension; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.