|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Javaabu\Cms\Media\Attachment; |
| 4 | + |
| 5 | +use DateTimeInterface; |
| 6 | +use Illuminate\Database\Eloquent\Model; |
| 7 | +use Illuminate\Database\Eloquent\Relations\BelongsTo; |
| 8 | +use Illuminate\Database\Eloquent\Relations\MorphTo; |
| 9 | +use Javaabu\Cms\Media\Attachment\Conversion\AttachmentConversionCollection; |
| 10 | +use Javaabu\Cms\Media\Attachment\UrlGenerator\AttachmentUrlGeneratorFactory; |
| 11 | +use Javaabu\Cms\Media\Media; |
| 12 | +use Spatie\MediaLibrary\Conversions\Conversion; |
| 13 | +use Spatie\MediaLibrary\MediaCollections\Models\Concerns\IsSorted; |
| 14 | + |
| 15 | +class Attachment extends Model |
| 16 | +{ |
| 17 | + use IsSorted; |
| 18 | + |
| 19 | + protected $morphClass = 'attachment'; |
| 20 | + |
| 21 | + /** |
| 22 | + * An attachment belongs to a media item |
| 23 | + * |
| 24 | + * @return BelongsTo |
| 25 | + */ |
| 26 | + public function media() |
| 27 | + { |
| 28 | + return $this->belongsTo(Media::class); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * An attachment belongs to a model |
| 33 | + * |
| 34 | + * @return MorphTo |
| 35 | + */ |
| 36 | + public function model() |
| 37 | + { |
| 38 | + return $this->morphTo('model'); |
| 39 | + } |
| 40 | + |
| 41 | + /* |
| 42 | + * Get all the names of the registered media conversions. |
| 43 | + */ |
| 44 | + public function getMediaConversionNames(): array |
| 45 | + { |
| 46 | + $conversions = AttachmentConversionCollection::createForAttachment($this); |
| 47 | + |
| 48 | + return $conversions->map(function (Conversion $conversion) { |
| 49 | + return $conversion->getName(); |
| 50 | + })->toArray(); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Check if has a generated conversion |
| 55 | + * |
| 56 | + * @param string $conversionName |
| 57 | + * @return bool |
| 58 | + */ |
| 59 | + public function hasGeneratedConversion(string $conversionName): bool |
| 60 | + { |
| 61 | + $media = $this->media; |
| 62 | + |
| 63 | + return $media ? $media->hasGeneratedConversion($conversionName) : false; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Mark conversion as generated |
| 68 | + * |
| 69 | + * @param string $conversionName |
| 70 | + * @param bool $generated |
| 71 | + * @return Attachment |
| 72 | + */ |
| 73 | + public function markAsConversionGenerated(string $conversionName, bool $generated): self |
| 74 | + { |
| 75 | + $media = $this->media; |
| 76 | + |
| 77 | + if ($media) { |
| 78 | + $media->markAsConversionGenerated($conversionName, $generated); |
| 79 | + } |
| 80 | + |
| 81 | + return $this; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Get the path to the original media file. |
| 86 | + * |
| 87 | + * @param string $conversionName |
| 88 | + * @return string |
| 89 | + */ |
| 90 | + public function getPath(string $conversionName = ''): string |
| 91 | + { |
| 92 | + $urlGenerator = AttachmentUrlGeneratorFactory::createForAttachment($this, $conversionName); |
| 93 | + |
| 94 | + return $urlGenerator->getPath(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get the url to the original media file. |
| 99 | + * |
| 100 | + * @param string $conversionName |
| 101 | + * @return string |
| 102 | + */ |
| 103 | + public function getUrl(string $conversionName = ''): string |
| 104 | + { |
| 105 | + $urlGenerator = AttachmentUrlGeneratorFactory::createForAttachment($this, $conversionName); |
| 106 | + |
| 107 | + return $urlGenerator->getUrl(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Get temporary url |
| 112 | + * |
| 113 | + * @param DateTimeInterface $expiration |
| 114 | + * @param string $conversionName |
| 115 | + * @param array $options |
| 116 | + * @return string |
| 117 | + */ |
| 118 | + public function getTemporaryUrl(DateTimeInterface $expiration, string $conversionName = '', array $options = []): string |
| 119 | + { |
| 120 | + $urlGenerator = AttachmentUrlGeneratorFactory::createForAttachment($this, $conversionName); |
| 121 | + |
| 122 | + return $urlGenerator->getTemporaryUrl($expiration, $options); |
| 123 | + } |
| 124 | +} |
0 commit comments