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 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 getUrl(string $conversionName = ''): string |
50
|
|
|
{ |
51
|
|
|
$urlGenerator = UrlGeneratorFactory::createForMedia($this); |
52
|
|
|
|
53
|
|
|
if ($conversionName !== '') { |
54
|
|
|
$conversion = ConversionCollection::createForMedia($this)->getByName($conversionName); |
55
|
|
|
|
56
|
|
|
$urlGenerator->setConversion($conversion); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $urlGenerator->getUrl(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the path to the original media file. |
64
|
|
|
* |
65
|
|
|
* @param string $conversionName |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
* |
69
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion |
70
|
|
|
*/ |
71
|
|
|
public function getPath(string $conversionName = ''): string |
72
|
|
|
{ |
73
|
|
|
$urlGenerator = UrlGeneratorFactory::createForMedia($this); |
74
|
|
|
|
75
|
|
|
if ($conversionName != '') { |
76
|
|
|
$conversion = ConversionCollection::createForMedia($this)->getByName($conversionName); |
77
|
|
|
|
78
|
|
|
$urlGenerator->setConversion($conversion); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $urlGenerator->getPath(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Collection of all ImageGenerator drivers. |
86
|
|
|
*/ |
87
|
|
|
public function getImageGenerators() : Collection |
88
|
|
|
{ |
89
|
|
|
return collect(config('medialibrary.image_generators')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Determine the type of a file. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getTypeAttribute() |
98
|
|
|
{ |
99
|
|
|
$type = $this->getTypeFromExtension(); |
100
|
|
|
|
101
|
|
|
if ($type !== self::TYPE_OTHER) { |
102
|
|
|
return $type; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->getTypeFromMime(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getTypeFromExtension(): string |
109
|
|
|
{ |
110
|
|
|
$imageGenerator = $this->getImageGenerators() |
111
|
|
|
->map(function (string $className) { |
112
|
|
|
return app($className); |
113
|
|
|
}) |
114
|
|
|
->first->canHandleExtension(strtolower($this->extension)); |
115
|
|
|
|
116
|
|
|
return $imageGenerator |
117
|
|
|
? $imageGenerator->getType() |
118
|
|
|
: static::TYPE_OTHER; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/* |
122
|
|
|
* Determine the type of a file from its mime type |
123
|
|
|
*/ |
124
|
|
|
public function getTypeFromMime(): string |
125
|
|
|
{ |
126
|
|
|
$imageGenerator = $this->getImageGenerators() |
127
|
|
|
->map(function (string $className) { |
128
|
|
|
return app($className); |
129
|
|
|
}) |
130
|
|
|
->first->canHandleMime($this->mime_type); |
131
|
|
|
|
132
|
|
|
return $imageGenerator |
133
|
|
|
? $imageGenerator->getType() |
134
|
|
|
: static::TYPE_OTHER; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getExtensionAttribute(): string |
138
|
|
|
{ |
139
|
|
|
return pathinfo($this->file_name, PATHINFO_EXTENSION); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getHumanReadableSizeAttribute(): string |
143
|
|
|
{ |
144
|
|
|
return File::getHumanReadableSize($this->size); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function getDiskDriverName(): string |
148
|
|
|
{ |
149
|
|
|
return strtolower(config("filesystems.disks.{$this->disk}.driver")); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/* |
153
|
|
|
* Determine if the media item has a custom property with the given name. |
154
|
|
|
*/ |
155
|
|
|
public function hasCustomProperty(string $propertyName): bool |
156
|
|
|
{ |
157
|
|
|
return array_has($this->custom_properties, $propertyName); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get if the value of custom property with the given name. |
162
|
|
|
* |
163
|
|
|
* @param string $propertyName |
164
|
|
|
* @param mixed $default |
165
|
|
|
* |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
|
|
public function getCustomProperty(string $propertyName, $default = null) |
169
|
|
|
{ |
170
|
|
|
return array_get($this->custom_properties, $propertyName, $default); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param string $name |
175
|
|
|
* @param mixed $value |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function setCustomProperty(string $name, $value) |
180
|
|
|
{ |
181
|
|
|
$customProperties = $this->custom_properties; |
182
|
|
|
|
183
|
|
|
array_set($customProperties, $name, $value); |
184
|
|
|
|
185
|
|
|
$this->custom_properties = $customProperties; |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $name |
192
|
|
|
* |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
|
|
public function forgetCustomProperty(string $name) |
196
|
|
|
{ |
197
|
|
|
$customProperties = $this->custom_properties; |
198
|
|
|
|
199
|
|
|
array_forget($customProperties, $name); |
200
|
|
|
|
201
|
|
|
$this->custom_properties = $customProperties; |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/* |
207
|
|
|
* Get all the names of the registered media conversions. |
208
|
|
|
*/ |
209
|
|
|
public function getMediaConversionNames(): array |
210
|
|
|
{ |
211
|
|
|
$conversions = ConversionCollection::createForMedia($this); |
212
|
|
|
|
213
|
|
|
return $conversions->map(function (Conversion $conversion) { |
214
|
|
|
return $conversion->getName(); |
215
|
|
|
})->toArray(); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|