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