%PDF- %PDF-
Direktori : /var/www/html/workeasy-api/vendor/spatie/laravel-medialibrary/src/ |
Current File : //var/www/html/workeasy-api/vendor/spatie/laravel-medialibrary/src/Media.php |
<?php namespace Spatie\MediaLibrary; use Illuminate\Support\Collection; use Spatie\MediaLibrary\Helpers\File; use Illuminate\Database\Eloquent\Model; use Spatie\MediaLibrary\Conversion\Conversion; use Spatie\MediaLibrary\Conversion\ConversionCollection; use Spatie\MediaLibrary\UrlGenerator\UrlGeneratorFactory; class Media extends Model { use SortableTrait; const TYPE_OTHER = 'other'; protected $guarded = []; /** * The attributes that should be casted to native types. * * @var array */ protected $casts = [ 'manipulations' => 'array', 'custom_properties' => 'array', ]; /** * Create the polymorphic relation. * * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function model() { return $this->morphTo(); } /** * Get the full url to a original media file. * * @param string $conversionName * * @return string * * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion */ public function getFullUrl(string $conversionName = ''): string { return url($this->getUrl($conversionName)); } /** * Get the url to a original media file. * * @param string $conversionName * * @return string * * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion */ public function getUrl(string $conversionName = ''): string { $urlGenerator = UrlGeneratorFactory::createForMedia($this); if ($conversionName !== '') { $conversion = ConversionCollection::createForMedia($this)->getByName($conversionName); $urlGenerator->setConversion($conversion); } return $urlGenerator->getUrl(); } /** * Get the path to the original media file. * * @param string $conversionName * * @return string * * @throws \Spatie\MediaLibrary\Exceptions\InvalidConversion */ public function getPath(string $conversionName = ''): string { $urlGenerator = UrlGeneratorFactory::createForMedia($this); if ($conversionName != '') { $conversion = ConversionCollection::createForMedia($this)->getByName($conversionName); $urlGenerator->setConversion($conversion); } return $urlGenerator->getPath(); } /** * Collection of all ImageGenerator drivers. */ public function getImageGenerators() : Collection { return collect(config('medialibrary.image_generators')); } /** * Determine the type of a file. * * @return string */ public function getTypeAttribute() { $type = $this->getTypeFromExtension(); if ($type !== self::TYPE_OTHER) { return $type; } return $this->getTypeFromMime(); } public function getTypeFromExtension(): string { $imageGenerator = $this->getImageGenerators() ->map(function (string $className) { return app($className); }) ->first->canHandleExtension(strtolower($this->extension)); return $imageGenerator ? $imageGenerator->getType() : static::TYPE_OTHER; } /* * Determine the type of a file from its mime type */ public function getTypeFromMime(): string { $imageGenerator = $this->getImageGenerators() ->map(function (string $className) { return app($className); }) ->first->canHandleMime($this->mime_type); return $imageGenerator ? $imageGenerator->getType() : static::TYPE_OTHER; } public function getExtensionAttribute(): string { return pathinfo($this->file_name, PATHINFO_EXTENSION); } public function getHumanReadableSizeAttribute(): string { return File::getHumanReadableSize($this->size); } public function getDiskDriverName(): string { return strtolower(config("filesystems.disks.{$this->disk}.driver")); } /* * Determine if the media item has a custom property with the given name. */ public function hasCustomProperty(string $propertyName): bool { return array_has($this->custom_properties, $propertyName); } /** * Get if the value of custom property with the given name. * * @param string $propertyName * @param mixed $default * * @return mixed */ public function getCustomProperty(string $propertyName, $default = null) { return array_get($this->custom_properties, $propertyName, $default); } /** * @param string $name * @param mixed $value * * @return $this */ public function setCustomProperty(string $name, $value) { $customProperties = $this->custom_properties; array_set($customProperties, $name, $value); $this->custom_properties = $customProperties; return $this; } /** * @param string $name * * @return $this */ public function forgetCustomProperty(string $name) { $customProperties = $this->custom_properties; array_forget($customProperties, $name); $this->custom_properties = $customProperties; return $this; } /* * Get all the names of the registered media conversions. */ public function getMediaConversionNames(): array { $conversions = ConversionCollection::createForMedia($this); return $conversions->map(function (Conversion $conversion) { return $conversion->getName(); })->toArray(); } }