Skip to content

Commit

Permalink
add interface
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed May 18, 2024
1 parent 7dd154b commit bdbfb35
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 71 deletions.
8 changes: 5 additions & 3 deletions src/Commands/GenerateMediaConversionsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
});
Expand Down
107 changes: 107 additions & 0 deletions src/Contracts/InteractWithMedia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Finller\Media\Contracts;

use Finller\Media\MediaCollection;
use Finller\Media\MediaConversion;
use Finller\Media\Models\Media;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;

/**
* @template TMedia of Media
*/
interface InteractWithMedia
{
/**
* @return EloquentCollection<int, TMedia>
*/
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<int,MediaCollection>|iterable<MediaCollection>|null
*/
public function registerMediaCollections(): Arrayable|iterable|null;

/**
* @param TMedia $media
* @return Arrayable<int,MediaConversion>|iterable<MediaConversion>|null
*/
public function registerMediaConversions(Media $media): Arrayable|iterable|null;

/**
* @return Collection<string, MediaCollection>
*/
public function getMediaCollections(): Collection;

public function hasMediaCollection(string $collection_name): bool;

public function getMediaCollection(string $collection_name): ?MediaCollection;

/**
* @param TMedia $media
* @return Collection<string, MediaConversion>
*/
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<int, TMedia> 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;
}
1 change: 0 additions & 1 deletion src/Jobs/ConversionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
34 changes: 22 additions & 12 deletions src/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,7 +44,7 @@
* @property ?int $order_column
* @property ?Collection<string, GeneratedConversion> $generated_conversions
* @property ?ArrayObject $metadata
* @property ?Model $model
* @property ?InteractWithMedia $model
* @property ?string $model_type
* @property ?int $model_id
*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/HasMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getFirstMediaUrl(
/**
* @return Arrayable<MediaCollection>|iterable<MediaCollection>|null
*/
protected function registerMediaCollections(): Arrayable|iterable|null
public function registerMediaCollections(): Arrayable|iterable|null
{
return collect();
}
Expand All @@ -104,7 +104,7 @@ protected function registerMediaCollections(): Arrayable|iterable|null
* @param TMedia $media
* @return Arrayable<MediaConversion>|iterable<MediaConversion>|null
*/
protected function registerMediaConversions(Media $media): Arrayable|iterable|null
public function registerMediaConversions(Media $media): Arrayable|iterable|null
{
return collect();
}
Expand Down
36 changes: 36 additions & 0 deletions src/Traits/InteractsWithMediaFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 4 additions & 9 deletions tests/Models/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,18 +13,15 @@
use Illuminate\Support\Collection;
use Spatie\Image\Enums\Fit;

class Test extends Model
class Test extends Model implements InteractWithMedia
{
use HasMedia;

protected $table = 'tests';

protected $guarded = [];

/**
* @return Collection<MediaCollection>
*/
protected function registerMediaCollections(): Collection
public function registerMediaCollections(): Collection
{
return collect([
new MediaCollection(
Expand All @@ -45,10 +43,7 @@ protected function registerMediaCollections(): Collection
]);
}

/**
* @return Collection<MediaConversion>
*/
protected function registerMediaConversions(Media $media): Collection
public function registerMediaConversions(Media $media): Collection
{
$conversions = collect();

Expand Down
3 changes: 2 additions & 1 deletion tests/Models/TestSoftDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
13 changes: 4 additions & 9 deletions tests/Models/TestWithMultipleConversions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,18 +12,15 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class TestWithMultipleConversions extends Model
class TestWithMultipleConversions extends Model implements InteractWithMedia
{
use HasMedia;

protected $table = 'tests';

protected $guarded = [];

/**
* @return Collection<MediaCollection>
*/
protected function registerMediaCollections(): Collection
public function registerMediaCollections(): Collection
{
return collect([
new MediaCollection(
Expand All @@ -33,10 +31,7 @@ protected function registerMediaCollections(): Collection
]);
}

/**
* @return Collection<MediaConversion>
*/
protected function registerMediaConversions(Media $media): Collection
public function registerMediaConversions(Media $media): Collection
{

if ($media->type === MediaType::Image) {
Expand Down
8 changes: 3 additions & 5 deletions tests/Models/TestWithNestedConversions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -10,18 +11,15 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;

class TestWithNestedConversions extends Model
class TestWithNestedConversions extends Model implements InteractWithMedia
{
use HasMedia;

protected $table = 'tests';

protected $guarded = [];

/**
* @return Collection<MediaConversion>
*/
protected function registerMediaConversions(Media $media): Collection
public function registerMediaConversions(Media $media): Collection
{

if ($media->type === MediaType::Image) {
Expand Down
Loading

0 comments on commit bdbfb35

Please sign in to comment.