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