Completed
Push — master ( c39f36...b0b140 )
by Freek
02:31
created

Media::getMimeAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
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_SVG = 'svg';
18
    const TYPE_PDF = 'pdf';
19
20
    protected $guarded = ['id', 'disk', 'file_name', 'size', 'model_type', 'model_id'];
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 original Url to a media-file.
44
     *
45
     * @param string $conversionName
46
     *
47
     * @return string
48
     *
49
     * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion
50
     */
51
    public function getUrl(string $conversionName = '') : string
52
    {
53
        $urlGenerator = UrlGeneratorFactory::createForMedia($this);
54
55
        if ($conversionName !== '') {
56
            $urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName));
57
        }
58
59
        return $urlGenerator->getUrl();
60
    }
61
62
    /**
63
     * Get the original path to a 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
            $urlGenerator->setConversion(ConversionCollection::createForMedia($this)->getByName($conversionName));
77
        }
78
79
        return $urlGenerator->getPath();
80
    }
81
82
    /**
83
     * Determine the type of a file.
84
     *
85
     * @return string
86
     */
87
    public function getTypeAttribute()
88
    {
89
        $type = $this->type_from_extension;
90
        if ($type !== self::TYPE_OTHER) {
91
            return $type;
92
        }
93
94
        return $this->type_from_mime;
95
    }
96
97
    /**
98
     * Determine the type of a file from its file extension.
99
     *
100
     * @return string
101
     */
102
    public function getTypeFromExtensionAttribute()
103
    {
104
        $extension = strtolower($this->extension);
105
106
        if (in_array($extension, ['png', 'jpg', 'jpeg', 'gif'])) {
107
            return static::TYPE_IMAGE;
108
        }
109
110
        if ($extension == 'pdf') {
111
            return static::TYPE_PDF;
112
        }
113
114
        if ($extension == 'svg') {
115
            return static::TYPE_SVG;
116
        }
117
118
        return static::TYPE_OTHER;
119
    }
120
121
    /*
122
     * Determine the type of a file from its mime type
123
     */
124
    public function getTypeFromMimeAttribute() : string
125
    {
126
        if ($this->getDiskDriverName() !== 'local') {
127
            return static::TYPE_OTHER;
128
        }
129
130
        $mime = $this->getMimeAttribute();
131
132
        if (in_array($mime, ['image/jpeg', 'image/gif', 'image/png'])) {
133
            return static::TYPE_IMAGE;
134
        }
135
136
        if ($mime === 'application/pdf') {
137
            return static::TYPE_PDF;
138
        }
139
140
        if ($mime === 'image/svg+xml') {
141
            return static::TYPE_SVG;
142
        }
143
144
        return static::TYPE_OTHER;
145
    }
146
147
    public function getMimeAttribute() : string
148
    {
149
        return File::getMimetype($this->getPath());
150
    }
151
152
    public function getExtensionAttribute() : string
153
    {
154
        return pathinfo($this->file_name, PATHINFO_EXTENSION);
155
    }
156
157
    public function getHumanReadableSizeAttribute() : string
158
    {
159
        return File::getHumanReadableSize($this->size);
160
    }
161
162
    public function getDiskDriverName() : string
163
    {
164
        return config("filesystems.disks.{$this->disk}.driver");
165
    }
166
167
    /*
168
     * Determine if the media item has a custom property with the given name.
169
     */
170
    public function hasCustomProperty(string $propertyName) : bool
171
    {
172
        return array_key_exists($propertyName, $this->custom_properties);
173
    }
174
175
    /**
176
     * Get if the value of custom property with the given name.
177
     *
178
     * @param string $propertyName
179
     * @param mixed  $default
180
     *
181
     * @return mixed
182
     */
183
    public function getCustomProperty(string $propertyName, $default = null)
184
    {
185
        return $this->custom_properties[$propertyName] ?? $default;
186
    }
187
188
    /**
189
     * @param string $name
190
     * @param mixed  $value
191
     */
192
    public function setCustomProperty(string $name, $value)
193
    {
194
        $this->custom_properties = array_merge($this->custom_properties, [$name => $value]);
195
    }
196
197
    /**
198
     * @param string $name
199
     *
200
     * @return $this
201
     */
202
    public function removeCustomProperty(string $name)
203
    {
204
        if ($this->hasCustomProperty($name)) {
205
206
            $customProperties = $this->custom_properties;
207
208
            unset($customProperties[$name]);
209
210
            $this->custom_properties = $customProperties;
211
        }
212
213
        return $this;
214
    }
215
216
    /*
217
     * Get all the names of the registered media conversions.
218
     */
219
    public function getMediaConversionNames() : array
220
    {
221
        $conversions = ConversionCollection::createForMedia($this);
222
223
        return $conversions->map(function (Conversion $conversion) {
224
            return $conversion->getName();
225
        })->toArray();
226
    }
227
}
228