Completed
Push — master ( 8f4eb1...a42ea3 )
by Freek
03:47
created

Media::setCustomProperty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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 = File::getMimetype($this->getPath());
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 getExtensionAttribute() : string
148
    {
149
        return pathinfo($this->file_name, PATHINFO_EXTENSION);
150
    }
151
152
    public function getHumanReadableSizeAttribute() : string
153
    {
154
        return File::getHumanReadableSize($this->size);
155
    }
156
157
    public function getDiskDriverName() : string
158
    {
159
        return config("filesystems.disks.{$this->disk}.driver");
160
    }
161
162
    /*
163
     * Determine if the media item has a custom property with the given name.
164
     */
165
    public function hasCustomProperty(string $propertyName) : bool
166
    {
167
        return array_key_exists($propertyName, $this->custom_properties);
168
    }
169
170
    /**
171
     * Get if the value of custom property with the given name.
172
     *
173
     * @param string $propertyName
174
     * @param mixed  $default
175
     *
176
     * @return mixed
177
     */
178
    public function getCustomProperty(string $propertyName, $default = null)
179
    {
180
        return $this->custom_properties[$propertyName] ?? $default;
181
    }
182
183
    /**
184
     * @param string $name
185
     * @param mixed  $value
186
     */
187
    public function setCustomProperty(string $name, $value)
188
    {
189
        $this->custom_properties = array_merge($this->custom_properties, [$name => $value]);
190
    }
191
192
    /*
193
     * Get all the names of the registered media conversions.
194
     */
195
    public function getMediaConversionNames() : array
196
    {
197
        $conversions = ConversionCollection::createForMedia($this);
198
199
        return $conversions->map(function (Conversion $conversion) {
200
            return $conversion->getName();
201
        })->toArray();
202
    }
203
}
204