diff --git a/src/Commands/GenerateMediaConversionsCommand.php b/src/Commands/GenerateMediaConversionsCommand.php index 581b144..61a388f 100644 --- a/src/Commands/GenerateMediaConversionsCommand.php +++ b/src/Commands/GenerateMediaConversionsCommand.php @@ -48,13 +48,15 @@ public function handle(): int $this->withProgressBar($media, function (Media $media) use ($conversions, $force) { $model = $media->model; - $modelConversions = $model->getMediaConversions($media); // @phpstan-ignore-line + $modelConversions = $model->getMediaConversions($media); - $conversions = empty($conversions) ? $modelConversions : array_intersect($modelConversions, $conversions); + $conversions = empty($conversions) ? + $modelConversions : + array_intersect($modelConversions->toArray(), $conversions); foreach ($conversions as $conversion) { if ($force || ! $media->hasGeneratedConversion($conversion)) { - $model->dispatchConversion($media, $conversion); // @phpstan-ignore-line + $model->dispatchConversion($media, $conversion); } } }); diff --git a/src/Contracts/InteractWithMedia.php b/src/Contracts/InteractWithMedia.php new file mode 100644 index 0000000..4442723 --- /dev/null +++ b/src/Contracts/InteractWithMedia.php @@ -0,0 +1,107 @@ + + */ + public function getMedia(?string $collection_name = null, ?string $collection_group = null): EloquentCollection; + + public function hasMedia(?string $collection_name = null, ?string $collection_group = null): bool; + + /**c + * @return TMedia + */ + public function getFirstMedia(?string $collection_name = null, ?string $collection_group = null): ?Media; + + public function getFirstMediaUrl( + ?string $collection_name = null, + ?string $collection_group = null, + ?string $conversion = null, + ): ?string; + + /** + * @return Arrayable|iterable|null + */ + public function registerMediaCollections(): Arrayable|iterable|null; + + /** + * @param TMedia $media + * @return Arrayable|iterable|null + */ + public function registerMediaConversions(Media $media): Arrayable|iterable|null; + + /** + * @return Collection + */ + public function getMediaCollections(): Collection; + + public function hasMediaCollection(string $collection_name): bool; + + public function getMediaCollection(string $collection_name): ?MediaCollection; + + /** + * @param TMedia $media + * @return Collection + */ + public function getMediaConversions(Media $media): Collection; + + public function getMediaConversionKey(string $conversion): string; + + /** + * @param TMedia $media + */ + public function getMediaConversion(Media $media, string $conversion): ?MediaConversion; + + /** + * @param int[] $except Array of Media Ids + * @return Collection The deleted media list + */ + public function clearMediaCollection( + string $collection_name, + ?string $collection_group = null, + array $except = [] + ): Collection; + + /** + * @param string|UploadedFile|resource $file + * @return TMedia + */ + public function addMedia( + mixed $file, + ?string $collection_name = null, + ?string $collection_group = null, + ?string $disk = null, + ?string $name = null, + ?string $order = null, + ?array $metadata = null, + ): Media; + + /** + * @param TMedia $media + */ + public function dispatchConversion(Media $media, string $conversionName): static; + + /** + * @param TMedia $media + */ + public function dispatchConversions( + Media $media, + ?bool $force = false, + ?array $only = null, + ?array $except = null, + ): static; +} diff --git a/src/Jobs/ConversionJob.php b/src/Jobs/ConversionJob.php index f7bf464..c24f1a9 100644 --- a/src/Jobs/ConversionJob.php +++ b/src/Jobs/ConversionJob.php @@ -64,7 +64,6 @@ public function middleware(): array public function getConversion(): ?MediaConversion { - // @phpstan-ignore-next-line return $this->media->model?->getMediaConversion($this->media, $this->conversion); } diff --git a/src/Models/Media.php b/src/Models/Media.php index f4e44c6..3cea38e 100644 --- a/src/Models/Media.php +++ b/src/Models/Media.php @@ -5,6 +5,7 @@ use Closure; use Finller\Media\Casts\GeneratedConversion; use Finller\Media\Casts\GeneratedConversions; +use Finller\Media\Contracts\InteractWithMedia; use Finller\Media\Enums\MediaType; use Finller\Media\Events\MediaFileStoredEvent; use Finller\Media\FileDownloaders\FileDownloader; @@ -43,7 +44,7 @@ * @property ?int $order_column * @property ?Collection $generated_conversions * @property ?ArrayObject $metadata - * @property ?Model $model + * @property ?InteractWithMedia $model * @property ?string $model_type * @property ?int $model_id */ @@ -153,7 +154,8 @@ public function generateBasePath(?string $conversion = null): string $root = Str::of($prefix) ->when($prefix, fn ($string) => $string->finish('/')) - ->append($this->uuid)->finish('/'); + ->append($this->uuid) + ->finish('/'); if ($conversion) { return $root @@ -324,16 +326,9 @@ public function forgetGeneratedConversion(string $conversion): static return $this; } - public function storeFileFromHttpFile( - UploadedFile|HttpFile $file, - ?string $collection_name = null, - ?string $basePath = null, - ?string $name = null, - ?string $disk = null, - ) { - $this->collection_name = $collection_name ?? $this->collection_name ?? config('media.default_collection_name'); - $this->disk = $disk ?? $this->disk ?? config('media.disk'); - + public function extractFileInformation( + UploadedFile|HttpFile $file + ): static { $this->mime_type = File::mimeType($file); $this->extension = File::extension($file); $this->size = $file->getSize(); @@ -346,6 +341,21 @@ public function storeFileFromHttpFile( $this->aspect_ratio = $dimension?->getRatio(forceStandards: false)->getValue(); $this->duration = File::duration($file->getPathname()); + return $this; + } + + public function storeFileFromHttpFile( + UploadedFile|HttpFile $file, + ?string $collection_name = null, + ?string $basePath = null, + ?string $name = null, + ?string $disk = null, + ): static { + $this->collection_name = $collection_name ?? $this->collection_name ?? config('media.default_collection_name'); + $this->disk = $disk ?? $this->disk ?? config('media.disk'); + + $this->extractFileInformation($file); + $basePath = Str::finish($basePath ?? $this->generateBasePath(), '/'); $this->name = Str::limit( diff --git a/src/Traits/HasMedia.php b/src/Traits/HasMedia.php index 9cc1413..58ace17 100644 --- a/src/Traits/HasMedia.php +++ b/src/Traits/HasMedia.php @@ -95,7 +95,7 @@ public function getFirstMediaUrl( /** * @return Arrayable|iterable|null */ - protected function registerMediaCollections(): Arrayable|iterable|null + public function registerMediaCollections(): Arrayable|iterable|null { return collect(); } @@ -104,7 +104,7 @@ protected function registerMediaCollections(): Arrayable|iterable|null * @param TMedia $media * @return Arrayable|iterable|null */ - protected function registerMediaConversions(Media $media): Arrayable|iterable|null + public function registerMediaConversions(Media $media): Arrayable|iterable|null { return collect(); } diff --git a/src/Traits/InteractsWithMediaFiles.php b/src/Traits/InteractsWithMediaFiles.php index f168cd1..4f7ce0a 100644 --- a/src/Traits/InteractsWithMediaFiles.php +++ b/src/Traits/InteractsWithMediaFiles.php @@ -124,6 +124,42 @@ public function putFile( return $path; } + public function moveFiles( + ?string $disk = null, + ?string $path = null + ): static { + if (! $disk && ! $path) { + return $this; + } + + $newDisk = $disk ?? $this->disk; + $newPath = $path ?? $this->path; + + $temporaryDirectory = (new TemporaryDirectory()) + ->location(storage_path('media-tmp')) + ->deleteWhenDestroyed() + ->create(); + + $temporaryFilePath = $this->makeTemporaryFileCopy($temporaryDirectory); + + $filesystem = Storage::disk($newDisk); + + $filesystem->putFileAs( + path: SupportFile::dirname($newPath), + file: new HttpFile($temporaryFilePath), + name: File::extractFilename($newPath) + ); + + $temporaryDirectory->delete(); + + $this->deleteFile(); + + $this->disk = $newDisk; + $this->path = $newPath; + + return $this; + } + public function deleteDirectory(): bool { if (! $this->path) { diff --git a/tests/Models/Test.php b/tests/Models/Test.php index ac11b70..d75153e 100644 --- a/tests/Models/Test.php +++ b/tests/Models/Test.php @@ -2,6 +2,7 @@ namespace Finller\Media\Tests\Models; +use Finller\Media\Contracts\InteractWithMedia; use Finller\Media\Enums\MediaType; use Finller\Media\Jobs\OptimizedImageConversionJob; use Finller\Media\MediaCollection; @@ -12,7 +13,7 @@ use Illuminate\Support\Collection; use Spatie\Image\Enums\Fit; -class Test extends Model +class Test extends Model implements InteractWithMedia { use HasMedia; @@ -20,10 +21,7 @@ class Test extends Model protected $guarded = []; - /** - * @return Collection - */ - protected function registerMediaCollections(): Collection + public function registerMediaCollections(): Collection { return collect([ new MediaCollection( @@ -45,10 +43,7 @@ protected function registerMediaCollections(): Collection ]); } - /** - * @return Collection - */ - protected function registerMediaConversions(Media $media): Collection + public function registerMediaConversions(Media $media): Collection { $conversions = collect(); diff --git a/tests/Models/TestSoftDelete.php b/tests/Models/TestSoftDelete.php index 2ac5ce9..35c4083 100644 --- a/tests/Models/TestSoftDelete.php +++ b/tests/Models/TestSoftDelete.php @@ -2,9 +2,10 @@ namespace Finller\Media\Tests\Models; +use Finller\Media\Contracts\InteractWithMedia; use Illuminate\Database\Eloquent\SoftDeletes; -class TestSoftDelete extends Test +class TestSoftDelete extends Test implements InteractWithMedia { use SoftDeletes; } diff --git a/tests/Models/TestWithMultipleConversions.php b/tests/Models/TestWithMultipleConversions.php index 8d2166c..3f5a700 100644 --- a/tests/Models/TestWithMultipleConversions.php +++ b/tests/Models/TestWithMultipleConversions.php @@ -2,6 +2,7 @@ namespace Finller\Media\Tests\Models; +use Finller\Media\Contracts\InteractWithMedia; use Finller\Media\Enums\MediaType; use Finller\Media\Jobs\OptimizedImageConversionJob; use Finller\Media\MediaCollection; @@ -11,7 +12,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; -class TestWithMultipleConversions extends Model +class TestWithMultipleConversions extends Model implements InteractWithMedia { use HasMedia; @@ -19,10 +20,7 @@ class TestWithMultipleConversions extends Model protected $guarded = []; - /** - * @return Collection - */ - protected function registerMediaCollections(): Collection + public function registerMediaCollections(): Collection { return collect([ new MediaCollection( @@ -33,10 +31,7 @@ protected function registerMediaCollections(): Collection ]); } - /** - * @return Collection - */ - protected function registerMediaConversions(Media $media): Collection + public function registerMediaConversions(Media $media): Collection { if ($media->type === MediaType::Image) { diff --git a/tests/Models/TestWithNestedConversions.php b/tests/Models/TestWithNestedConversions.php index 7905e90..d121a04 100644 --- a/tests/Models/TestWithNestedConversions.php +++ b/tests/Models/TestWithNestedConversions.php @@ -2,6 +2,7 @@ namespace Finller\Media\Tests\Models; +use Finller\Media\Contracts\InteractWithMedia; use Finller\Media\Enums\MediaType; use Finller\Media\Jobs\OptimizedImageConversionJob; use Finller\Media\MediaConversion; @@ -10,7 +11,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; -class TestWithNestedConversions extends Model +class TestWithNestedConversions extends Model implements InteractWithMedia { use HasMedia; @@ -18,10 +19,7 @@ class TestWithNestedConversions extends Model protected $guarded = []; - /** - * @return Collection - */ - protected function registerMediaConversions(Media $media): Collection + public function registerMediaConversions(Media $media): Collection { if ($media->type === MediaType::Image) { diff --git a/tests/Models/TestWithResponsiveImages.php b/tests/Models/TestWithResponsiveImages.php index 3e59136..397774a 100644 --- a/tests/Models/TestWithResponsiveImages.php +++ b/tests/Models/TestWithResponsiveImages.php @@ -2,16 +2,16 @@ namespace Finller\Media\Tests\Models; +use Finller\Media\Contracts\InteractWithMedia; use Finller\Media\Enums\MediaType; use Finller\Media\MediaCollection; -use Finller\Media\MediaConversion; use Finller\Media\Models\Media; use Finller\Media\Support\ResponsiveImagesConversionsPreset; use Finller\Media\Traits\HasMedia; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; -class TestWithResponsiveImages extends Model +class TestWithResponsiveImages extends Model implements InteractWithMedia { use HasMedia; @@ -19,10 +19,7 @@ class TestWithResponsiveImages extends Model protected $guarded = []; - /** - * @return Collection - */ - protected function registerMediaCollections(): Collection + public function registerMediaCollections(): Collection { return collect([ new MediaCollection( @@ -34,10 +31,7 @@ protected function registerMediaCollections(): Collection ]); } - /** - * @return Collection - */ - protected function registerMediaConversions(Media $media): Collection + public function registerMediaConversions(Media $media): Collection { if ($media->type === MediaType::Image) { return ResponsiveImagesConversionsPreset::get($media); diff --git a/tests/Models/TestWithVideoConversions.php b/tests/Models/TestWithVideoConversions.php index 3b9eb2a..b4450ae 100644 --- a/tests/Models/TestWithVideoConversions.php +++ b/tests/Models/TestWithVideoConversions.php @@ -2,6 +2,7 @@ namespace Finller\Media\Tests\Models; +use Finller\Media\Contracts\InteractWithMedia; use Finller\Media\Enums\MediaType; use Finller\Media\Jobs\VideoPosterConversionJob; use Finller\Media\MediaCollection; @@ -11,7 +12,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; -class TestWithVideoConversions extends Model +class TestWithVideoConversions extends Model implements InteractWithMedia { use HasMedia; @@ -19,10 +20,7 @@ class TestWithVideoConversions extends Model protected $guarded = []; - /** - * @return Collection - */ - protected function registerMediaCollections(): Collection + public function registerMediaCollections(): Collection { return collect([ new MediaCollection( @@ -33,10 +31,7 @@ protected function registerMediaCollections(): Collection ]); } - /** - * @return Collection - */ - protected function registerMediaConversions(Media $media): Collection + public function registerMediaConversions(Media $media): Collection { if ($media->type === MediaType::Video) { return collect() diff --git a/tests/Models/TestWithVideoPosterPreset.php b/tests/Models/TestWithVideoPosterPreset.php index 3c9b798..e8d9617 100644 --- a/tests/Models/TestWithVideoPosterPreset.php +++ b/tests/Models/TestWithVideoPosterPreset.php @@ -2,16 +2,16 @@ namespace Finller\Media\Tests\Models; +use Finller\Media\Contracts\InteractWithMedia; use Finller\Media\Enums\MediaType; use Finller\Media\MediaCollection; -use Finller\Media\MediaConversion; use Finller\Media\Models\Media; use Finller\Media\Support\VideoPosterConversionPreset; use Finller\Media\Traits\HasMedia; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Collection; -class TestWithVideoPosterPreset extends Model +class TestWithVideoPosterPreset extends Model implements InteractWithMedia { use HasMedia; @@ -19,10 +19,7 @@ class TestWithVideoPosterPreset extends Model protected $guarded = []; - /** - * @return Collection - */ - protected function registerMediaCollections(): Collection + public function registerMediaCollections(): Collection { return collect([ new MediaCollection( @@ -33,10 +30,7 @@ protected function registerMediaCollections(): Collection ]); } - /** - * @return Collection - */ - protected function registerMediaConversions(Media $media): Collection + public function registerMediaConversions(Media $media): Collection { if ($media->type === MediaType::Video) { return VideoPosterConversionPreset::get($media, withResponsiveImages: true);