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_PDF = 'pdf'; |
18
|
|
|
|
19
|
|
|
protected $guarded = ['id', 'disk', 'file_name', 'size', 'model_type', 'model_id']; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The attributes that should be casted to native types. |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $casts = [ |
27
|
|
|
'manipulations' => 'array', |
28
|
|
|
'custom_properties' => 'array', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create the polymorphic relation. |
33
|
|
|
* |
34
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
35
|
|
|
*/ |
36
|
|
|
public function model() |
37
|
|
|
{ |
38
|
|
|
return $this->morphTo(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the original Url to a media-file. |
43
|
|
|
* |
44
|
|
|
* @param string $conversionName |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
* |
48
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion |
49
|
|
|
*/ |
50
|
|
|
public function getUrl(string $conversionName = '') : string |
51
|
|
|
{ |
52
|
|
|
$urlGenerator = UrlGeneratorFactory::createForMedia($this); |
53
|
|
|
|
54
|
|
|
if ($conversionName !== '') { |
55
|
|
|
$urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $urlGenerator->getUrl(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the original path to a media-file. |
63
|
|
|
* |
64
|
|
|
* @param string $conversionName |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
* |
68
|
|
|
* @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion |
69
|
|
|
*/ |
70
|
|
|
public function getPath(string $conversionName = '') : string |
71
|
|
|
{ |
72
|
|
|
$urlGenerator = UrlGeneratorFactory::createForMedia($this); |
73
|
|
|
|
74
|
|
|
if ($conversionName != '') { |
75
|
|
|
$urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $urlGenerator->getPath(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Determine the type of a file. |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getTypeAttribute() |
87
|
|
|
{ |
88
|
|
|
$type = $this->type_from_extension; |
89
|
|
|
if ($type !== self::TYPE_OTHER) { |
90
|
|
|
return $type; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->type_from_mime; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Determine the type of a file from its file extension. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getTypeFromExtensionAttribute() |
102
|
|
|
{ |
103
|
|
|
$extension = strtolower($this->extension); |
104
|
|
|
|
105
|
|
|
if (in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) { |
106
|
|
|
return static::TYPE_IMAGE; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if ($extension == 'pdf') { |
110
|
|
|
return static::TYPE_PDF; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return static::TYPE_OTHER; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/* |
117
|
|
|
* Determine the type of a file from its mime type |
118
|
|
|
*/ |
119
|
|
|
public function getTypeFromMimeAttribute() : string |
120
|
|
|
{ |
121
|
|
|
if ($this->getDiskDriverName() !== 'local') { |
122
|
|
|
return static::TYPE_OTHER; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$mime = File::getMimetype($this->getPath()); |
126
|
|
|
|
127
|
|
|
if (in_array($mime, ['image/jpeg', 'image/gif', 'image/png'])) { |
128
|
|
|
return static::TYPE_IMAGE; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($mime === 'application/pdf') { |
132
|
|
|
return static::TYPE_PDF; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return static::TYPE_OTHER; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getExtensionAttribute() : string |
139
|
|
|
{ |
140
|
|
|
return pathinfo($this->file_name, PATHINFO_EXTENSION); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getHumanReadableSizeAttribute() : string |
144
|
|
|
{ |
145
|
|
|
return File::getHumanReadableSize($this->size); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getDiskDriverName() : string |
149
|
|
|
{ |
150
|
|
|
return config("filesystems.disks.{$this->disk}.driver"); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/* |
154
|
|
|
* Determine if the media item has a custom property with the given name. |
155
|
|
|
*/ |
156
|
|
|
public function hasCustomProperty(string $propertyName) : bool |
157
|
|
|
{ |
158
|
|
|
return array_key_exists($propertyName, $this->custom_properties); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get if the value of custom property with the given name. |
163
|
|
|
* |
164
|
|
|
* @param string $propertyName |
165
|
|
|
* @param mixed $default |
166
|
|
|
* |
167
|
|
|
* @return mixed |
168
|
|
|
*/ |
169
|
|
|
public function getCustomProperty(string $propertyName, $default = null) |
170
|
|
|
{ |
171
|
|
|
return $this->custom_properties[$propertyName] ?? $default; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param string $name |
176
|
|
|
* @param mixed $value |
177
|
|
|
*/ |
178
|
|
|
public function setCustomProperty(string $name, $value) |
179
|
|
|
{ |
180
|
|
|
$this->custom_properties = array_merge($this->custom_properties, [$name => $value]); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/* |
184
|
|
|
* Get all the names of the registered media conversions. |
185
|
|
|
*/ |
186
|
|
|
public function getMediaConversionNames() : array |
187
|
|
|
{ |
188
|
|
|
$conversions = ConversionCollection::createForMedia($this); |
189
|
|
|
|
190
|
|
|
return $conversions->map(function (Conversion $conversion) { |
191
|
|
|
return $conversion->getName(); |
192
|
|
|
})->toArray(); |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|