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