Completed
Push — master ( 0491a1...311634 )
by Freek
01:37
created

Media::getTemporaryUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
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 Spatie\MediaLibrary\Conversion\Conversion;
10
use Spatie\MediaLibrary\Conversion\ConversionCollection;
11
use Spatie\MediaLibrary\UrlGenerator\UrlGeneratorFactory;
12
13
class Media extends Model
14
{
15
    use SortableTrait;
16
17
    const TYPE_OTHER = 'other';
18
19
    protected $guarded = [];
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 full url to a original media file.
43
     *
44
     * @param string $conversionName
45
     *
46
     * @return string
47
     *
48
     * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion
49
     */
50
    public function getFullUrl(string $conversionName = ''): string
51
    {
52
        return url($this->getUrl($conversionName));
53
    }
54
55
    /**
56
     * Get the url to a original media file.
57
     *
58
     * @param string $conversionName
59
     *
60
     * @return string
61
     *
62
     * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion
63
     */
64
    public function getUrl(string $conversionName = ''): string
65
    {
66
        $urlGenerator = UrlGeneratorFactory::createForMedia($this, $conversionName);
67
68
        return $urlGenerator->getUrl();
69
    }
70
71
    /**
72
     * Get a temporary url to a original media file.
73
     *
74
     * @param string             $conversionName
75
     * @param \DateTimeInterface $expiration
76
     * @param array              $options
77
     *
78
     * @return string
79
     */
80
    public function getTemporaryUrl(DateTimeInterface $expiration, string $conversionName = '', array $options = []): string
81
    {
82
        $urlGenerator = UrlGeneratorFactory::createForMedia($this, $conversionName);
83
84
        return $urlGenerator->getTemporaryUrl($expiration, $options);
85
    }
86
87
    /**
88
     * Get the path to the original media file.
89
     *
90
     * @param string $conversionName
91
     *
92
     * @return string
93
     *
94
     * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion
95
     */
96
    public function getPath(string $conversionName = ''): string
97
    {
98
        $urlGenerator = UrlGeneratorFactory::createForMedia($this, $conversionName);
99
100
        return $urlGenerator->getPath();
101
    }
102
103
    /**
104
     * Collection of all ImageGenerator drivers.
105
     */
106
    public function getImageGenerators() : Collection
107
    {
108
        return collect(config('medialibrary.image_generators'));
109
    }
110
111
    /**
112
     * Determine the type of a file.
113
     *
114
     * @return string
115
     */
116
    public function getTypeAttribute()
117
    {
118
        $type = $this->getTypeFromExtension();
119
120
        if ($type !== self::TYPE_OTHER) {
121
            return $type;
122
        }
123
124
        return $this->getTypeFromMime();
125
    }
126
127
    public function getTypeFromExtension(): string
128
    {
129
        $imageGenerator = $this->getImageGenerators()
130
            ->map(function (string $className) {
131
                return app($className);
132
            })
133
            ->first->canHandleExtension(strtolower($this->extension));
134
135
        return $imageGenerator
136
            ? $imageGenerator->getType()
137
            : static::TYPE_OTHER;
138
    }
139
140
    /*
141
     * Determine the type of a file from its mime type
142
     */
143
    public function getTypeFromMime(): string
144
    {
145
        $imageGenerator = $this->getImageGenerators()
146
            ->map(function (string $className) {
147
                return app($className);
148
            })
149
            ->first->canHandleMime($this->mime_type);
150
151
        return $imageGenerator
152
            ? $imageGenerator->getType()
153
            : static::TYPE_OTHER;
154
    }
155
156
    public function getExtensionAttribute(): string
157
    {
158
        return pathinfo($this->file_name, PATHINFO_EXTENSION);
159
    }
160
161
    public function getHumanReadableSizeAttribute(): string
162
    {
163
        return File::getHumanReadableSize($this->size);
164
    }
165
166
    public function getDiskDriverName(): string
167
    {
168
        return strtolower(config("filesystems.disks.{$this->disk}.driver"));
169
    }
170
171
    /*
172
     * Determine if the media item has a custom property with the given name.
173
     */
174
    public function hasCustomProperty(string $propertyName): bool
175
    {
176
        return array_has($this->custom_properties, $propertyName);
177
    }
178
179
    /**
180
     * Get if the value of custom property with the given name.
181
     *
182
     * @param string $propertyName
183
     * @param mixed $default
184
     *
185
     * @return mixed
186
     */
187
    public function getCustomProperty(string $propertyName, $default = null)
188
    {
189
        return array_get($this->custom_properties, $propertyName, $default);
190
    }
191
192
    /**
193
     * @param string $name
194
     * @param mixed $value
195
     *
196
     * @return $this
197
     */
198
    public function setCustomProperty(string $name, $value)
199
    {
200
        $customProperties = $this->custom_properties;
201
202
        array_set($customProperties, $name, $value);
203
204
        $this->custom_properties = $customProperties;
205
206
        return $this;
207
    }
208
209
    /**
210
     * @param string $name
211
     *
212
     * @return $this
213
     */
214
    public function forgetCustomProperty(string $name)
215
    {
216
        $customProperties = $this->custom_properties;
217
218
        array_forget($customProperties, $name);
219
220
        $this->custom_properties = $customProperties;
221
222
        return $this;
223
    }
224
225
    /*
226
     * Get all the names of the registered media conversions.
227
     */
228
    public function getMediaConversionNames(): array
229
    {
230
        $conversions = ConversionCollection::createForMedia($this);
231
232
        return $conversions->map(function (Conversion $conversion) {
233
            return $conversion->getName();
234
        })->toArray();
235
    }
236
}
237