1 | <?php |
||
16 | class Media extends Model |
||
17 | { |
||
18 | use SortableTrait; |
||
19 | |||
20 | const TYPE_OTHER = 'other'; |
||
21 | const TYPE_IMAGE = 'image'; |
||
22 | const TYPE_VIDEO = 'video'; |
||
23 | const TYPE_SVG = 'svg'; |
||
24 | const TYPE_PDF = 'pdf'; |
||
25 | |||
26 | protected $guarded = ['id', 'disk', 'file_name', 'size', 'model_type', 'model_id']; |
||
27 | |||
28 | /** |
||
29 | * The attributes that should be casted to native types. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $casts = [ |
||
34 | 'manipulations' => 'array', |
||
35 | 'custom_properties' => 'array', |
||
36 | ]; |
||
37 | |||
38 | /** |
||
39 | * Create the polymorphic relation. |
||
40 | * |
||
41 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo |
||
42 | */ |
||
43 | public function model() |
||
44 | { |
||
45 | return $this->morphTo(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Get the original Url to a media-file. |
||
50 | * |
||
51 | * @param string $conversionName |
||
52 | * |
||
53 | * @return string |
||
54 | * |
||
55 | * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion |
||
56 | */ |
||
57 | public function getUrl(string $conversionName = '') : string |
||
58 | { |
||
59 | $urlGenerator = UrlGeneratorFactory::createForMedia($this); |
||
60 | |||
61 | if ($conversionName !== '') { |
||
62 | $urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName)); |
||
63 | } |
||
64 | |||
65 | return $urlGenerator->getUrl(); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Get the original path to a media-file. |
||
70 | * |
||
71 | * @param string $conversionName |
||
72 | * |
||
73 | * @return string |
||
74 | * |
||
75 | * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion |
||
76 | */ |
||
77 | public function getPath(string $conversionName = '') : string |
||
78 | { |
||
79 | $urlGenerator = UrlGeneratorFactory::createForMedia($this); |
||
80 | |||
81 | if ($conversionName != '') { |
||
82 | $urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName)); |
||
83 | } |
||
84 | |||
85 | return $urlGenerator->getPath(); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Collection of all ImageGenerator drivers. |
||
90 | */ |
||
91 | public function getImageGenerators() : Collection |
||
92 | { |
||
93 | return collect([ |
||
94 | Image::class, |
||
95 | Pdf::class, |
||
96 | Svg::class, |
||
97 | Video::class, |
||
98 | ]); |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Determine the type of a file. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getTypeAttribute() |
||
107 | { |
||
108 | $type = $this->type_from_extension; |
||
109 | if ($type !== self::TYPE_OTHER) { |
||
110 | return $type; |
||
111 | } |
||
112 | |||
113 | return $this->type_from_mime; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Determine the type of a file from its file extension. |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | public function getTypeFromExtensionAttribute() |
||
122 | { |
||
123 | $imageGenerators = $this->getImageGenerators() |
||
124 | ->map(function (string $className) { |
||
125 | return app($className); |
||
126 | }); |
||
127 | |||
128 | foreach ($imageGenerators as $imageGenerator) { |
||
129 | if ($imageGenerator->canHandleExtension(strtolower($this->extension))) { |
||
130 | return $imageGenerator->getType(); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | return static::TYPE_OTHER; |
||
135 | } |
||
136 | |||
137 | /* |
||
138 | * Determine the type of a file from its mime type |
||
139 | */ |
||
140 | public function getTypeFromMimeAttribute() : string |
||
141 | { |
||
142 | $imageGenerators = $this->getImageGenerators() |
||
143 | ->map(function (string $className) { |
||
144 | return app($className); |
||
145 | }); |
||
146 | |||
147 | foreach ($imageGenerators as $imageGenerator) { |
||
148 | if ($imageGenerator->canHandleMime($this->getMimeAttribute())) { |
||
149 | return $imageGenerator->getType(); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | return static::TYPE_OTHER; |
||
154 | } |
||
155 | |||
156 | public function getMimeAttribute() : string |
||
160 | |||
161 | public function getExtensionAttribute() : string |
||
165 | |||
166 | public function getHumanReadableSizeAttribute() : string |
||
170 | |||
171 | public function getDiskDriverName() : string |
||
175 | |||
176 | /* |
||
177 | * Determine if the media item has a custom property with the given name. |
||
178 | */ |
||
179 | public function hasCustomProperty(string $propertyName) : bool |
||
183 | |||
184 | /** |
||
185 | * Determine if the media item has a custom property with the given name |
||
186 | * using dot notation. |
||
187 | * |
||
188 | * @param string $propertyName |
||
189 | * |
||
190 | * @return bool |
||
191 | * |
||
192 | * @deprecated Will be removed in the next major version in favor of |
||
193 | * changing `hasCustomProperty` to use dot notation. |
||
194 | */ |
||
195 | public function hasNestedCustomProperty(string $propertyName) : bool |
||
199 | |||
200 | /** |
||
201 | * Get if the value of custom property with the given name. |
||
202 | * |
||
203 | * @param string $propertyName |
||
204 | * @param mixed $default |
||
205 | * |
||
206 | * @return mixed |
||
207 | */ |
||
208 | public function getCustomProperty(string $propertyName, $default = null) |
||
212 | |||
213 | /** |
||
214 | * Get a custom property using dot notation. |
||
215 | * |
||
216 | * @param string $propertyName |
||
217 | * @param mixed $default |
||
218 | * |
||
219 | * @return mixed |
||
220 | * |
||
221 | * @deprecated Will be removed in the next major version in favor of |
||
222 | * changing `getCustomProperty` to use dot notation. |
||
223 | */ |
||
224 | public function getNestedCustomProperty(string $propertyName, $default = null) |
||
228 | |||
229 | /** |
||
230 | * @param string $name |
||
231 | * @param mixed $value |
||
232 | */ |
||
233 | public function setCustomProperty(string $name, $value) |
||
237 | |||
238 | /** |
||
239 | * Set a custom property using dot notation. |
||
240 | * |
||
241 | * @param string $name |
||
242 | * @param mixed $value |
||
243 | * |
||
244 | * @deprecated Will be removed in the next major version in favor of |
||
245 | * changing `setCustomProperty` to use dot notation. |
||
246 | */ |
||
247 | public function setNestedCustomProperty(string $name, $value) |
||
257 | |||
258 | /** |
||
259 | * @param string $name |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | public function forgetCustomProperty(string $name) |
||
275 | |||
276 | /** |
||
277 | * @param string $name |
||
278 | * |
||
279 | * @return $this |
||
280 | * |
||
281 | * @deprecated Will be renamed to `forgetCustomProperty` in the next |
||
282 | * major version. |
||
283 | */ |
||
284 | public function removeCustomProperty(string $name) |
||
288 | |||
289 | /** |
||
290 | * Forget a custom property using dot notation. |
||
291 | * |
||
292 | * @param string $name |
||
293 | * |
||
294 | * @deprecated Will be removed in the next major version in favor of |
||
295 | * changing `forgetCustomProperty` to use dot notation. |
||
296 | */ |
||
297 | public function forgetNestedCustomProperty(string $name) |
||
307 | |||
308 | /* |
||
309 | * Get all the names of the registered media conversions. |
||
310 | */ |
||
311 | public function getMediaConversionNames(): array |
||
319 | } |
||
320 |