1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MediaLibrary; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Spatie\MediaLibrary\ImageGenerators\FileTypes\Image; |
8
|
|
|
use Spatie\MediaLibrary\ImageGenerators\FileTypes\Pdf; |
9
|
|
|
use Spatie\MediaLibrary\ImageGenerators\FileTypes\Svg; |
10
|
|
|
use Spatie\MediaLibrary\ImageGenerators\FileTypes\Video; |
11
|
|
|
use Spatie\MediaLibrary\ImageGenerators\ImageGenerator; |
12
|
|
|
use Spatie\MediaLibrary\Conversion\Conversion; |
13
|
|
|
use Spatie\MediaLibrary\Conversion\ConversionCollection; |
14
|
|
|
use Spatie\MediaLibrary\Helpers\File; |
15
|
|
|
use Spatie\MediaLibrary\UrlGenerator\UrlGeneratorFactory; |
16
|
|
|
|
17
|
|
|
class Media extends Model |
18
|
|
|
{ |
19
|
|
|
use SortableTrait; |
20
|
|
|
|
21
|
|
|
const TYPE_OTHER = 'other'; |
22
|
|
|
const TYPE_IMAGE = 'image'; |
23
|
|
|
const TYPE_VIDEO = 'video'; |
24
|
|
|
const TYPE_SVG = 'svg'; |
25
|
|
|
const TYPE_PDF = 'pdf'; |
26
|
|
|
|
27
|
|
|
protected $guarded = ['id', 'disk', 'file_name', 'size', 'model_type', 'model_id']; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The attributes that should be casted to native types. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $casts = [ |
35
|
|
|
'manipulations' => 'array', |
36
|
|
|
'custom_properties' => 'array', |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create the polymorphic relation. |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
43
|
|
|
*/ |
44
|
|
|
public function model() |
45
|
|
|
{ |
46
|
|
|
return $this->morphTo(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the original Url to a media-file. |
51
|
|
|
* |
52
|
|
|
* @param string $conversionName |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
* |
56
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion |
57
|
|
|
*/ |
58
|
|
|
public function getUrl(string $conversionName = '') : string |
59
|
|
|
{ |
60
|
|
|
$urlGenerator = UrlGeneratorFactory::createForMedia($this); |
61
|
|
|
|
62
|
|
|
if ($conversionName !== '') { |
63
|
|
|
$urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $urlGenerator->getUrl(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the original path to a media-file. |
71
|
|
|
* |
72
|
|
|
* @param string $conversionName |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
* |
76
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion |
77
|
|
|
*/ |
78
|
|
|
public function getPath(string $conversionName = '') : string |
79
|
|
|
{ |
80
|
|
|
$urlGenerator = UrlGeneratorFactory::createForMedia($this); |
81
|
|
|
|
82
|
|
|
if ($conversionName != '') { |
83
|
|
|
$urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $urlGenerator->getPath(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Collection of all ImageGenerator drivers. |
91
|
|
|
*/ |
92
|
|
|
public function getImageGenerators() : Collection |
93
|
|
|
{ |
94
|
|
|
return collect([ |
95
|
|
|
Image::class, |
96
|
|
|
Pdf::class, |
97
|
|
|
Svg::class, |
98
|
|
|
Video::class, |
99
|
|
|
]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Determine the type of a file. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getTypeAttribute() |
108
|
|
|
{ |
109
|
|
|
$type = $this->type_from_extension; |
110
|
|
|
if ($type !== self::TYPE_OTHER) { |
111
|
|
|
return $type; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return $this->type_from_mime; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Determine the type of a file from its file extension. |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getTypeFromExtensionAttribute() |
123
|
|
|
{ |
124
|
|
|
$imageGenerators = $this->getImageGenerators() |
125
|
|
|
->map(function (string $className) { |
126
|
|
|
return app($className); |
127
|
|
|
}); |
128
|
|
|
|
129
|
|
|
foreach ($imageGenerators as $imageGenerator) { |
130
|
|
|
if ($imageGenerator->canHandleExtension(strtolower($this->extension))) { |
131
|
|
|
return $imageGenerator->getType(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return static::TYPE_OTHER; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/* |
139
|
|
|
* Determine the type of a file from its mime type |
140
|
|
|
*/ |
141
|
|
|
public function getTypeFromMimeAttribute() : string |
142
|
|
|
{ |
143
|
|
|
if ($this->getDiskDriverName() !== 'local') { |
144
|
|
|
return static::TYPE_OTHER; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$imageGenerators = $this->getImageGenerators() |
148
|
|
|
->map(function (string $className) { |
149
|
|
|
return app($className); |
150
|
|
|
}); |
151
|
|
|
|
152
|
|
|
foreach ($imageGenerators as $imageGenerator) { |
153
|
|
|
if ($imageGenerator->canHandleMime($this->getMimeAttribute())) { |
154
|
|
|
return $imageGenerator->getType(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return static::TYPE_OTHER; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function getMimeAttribute() : string |
162
|
|
|
{ |
163
|
|
|
return File::getMimetype($this->getPath()); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function getExtensionAttribute() : string |
167
|
|
|
{ |
168
|
|
|
return pathinfo($this->file_name, PATHINFO_EXTENSION); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getHumanReadableSizeAttribute() : string |
172
|
|
|
{ |
173
|
|
|
return File::getHumanReadableSize($this->size); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function getDiskDriverName() : string |
177
|
|
|
{ |
178
|
|
|
return config("filesystems.disks.{$this->disk}.driver"); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/* |
182
|
|
|
* Determine if the media item has a custom property with the given name. |
183
|
|
|
*/ |
184
|
|
|
public function hasCustomProperty(string $propertyName) : bool |
185
|
|
|
{ |
186
|
|
|
return array_key_exists($propertyName, $this->custom_properties); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Get if the value of custom property with the given name. |
191
|
|
|
* |
192
|
|
|
* @param string $propertyName |
193
|
|
|
* @param mixed $default |
194
|
|
|
* |
195
|
|
|
* @return mixed |
196
|
|
|
*/ |
197
|
|
|
public function getCustomProperty(string $propertyName, $default = null) |
198
|
|
|
{ |
199
|
|
|
return $this->custom_properties[$propertyName] ?? $default; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $name |
204
|
|
|
* @param mixed $value |
205
|
|
|
*/ |
206
|
|
|
public function setCustomProperty(string $name, $value) |
207
|
|
|
{ |
208
|
|
|
$this->custom_properties = array_merge($this->custom_properties, [$name => $value]); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param string $name |
213
|
|
|
* |
214
|
|
|
* @return $this |
215
|
|
|
*/ |
216
|
|
|
public function removeCustomProperty(string $name) |
217
|
|
|
{ |
218
|
|
|
if ($this->hasCustomProperty($name)) { |
219
|
|
|
$customProperties = $this->custom_properties; |
220
|
|
|
|
221
|
|
|
unset($customProperties[$name]); |
222
|
|
|
|
223
|
|
|
$this->custom_properties = $customProperties; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/* |
230
|
|
|
* Get all the names of the registered media conversions. |
231
|
|
|
*/ |
232
|
|
|
public function getMediaConversionNames() : array |
233
|
|
|
{ |
234
|
|
|
$conversions = ConversionCollection::createForMedia($this); |
235
|
|
|
|
236
|
|
|
return $conversions->map(function (Conversion $conversion) { |
237
|
|
|
return $conversion->getName(); |
238
|
|
|
})->toArray(); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|