Skip to content

Commit

Permalink
Added ability to programatically specify custom queue on FileAdder (#…
Browse files Browse the repository at this point in the history
…3676)

* Fix styling

* Added ability to programmatically specify custom queue on the fileAdder

---------

Co-authored-by: chrispage1 <[email protected]>
  • Loading branch information
chrispage1 and chrispage1 committed Jul 24, 2024
1 parent bba4ef4 commit 594e0a8
Show file tree
Hide file tree
Showing 34 changed files with 95 additions and 86 deletions.
2 changes: 1 addition & 1 deletion src/Conversions/Actions/PerformConversionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function execute(

event(new ConversionWillStartEvent($media, $conversion, $copiedOriginalFile));

$manipulationResult = (new PerformManipulationsAction())->execute($media, $conversion, $copiedOriginalFile);
$manipulationResult = (new PerformManipulationsAction)->execute($media, $conversion, $copiedOriginalFile);

$newFileName = $conversion->getConversionFile($media);

Expand Down
4 changes: 2 additions & 2 deletions src/Conversions/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
) {
$optimizerChain = OptimizerChainFactory::create(config('media-library.image_optimizers'));

$this->manipulations = new Manipulations();
$this->manipulations = new Manipulations;
$this->manipulations->optimize($optimizerChain)->format('jpg');

$this->fileNamer = app(config('media-library.file_namer'));
Expand Down Expand Up @@ -106,7 +106,7 @@ public function removeManipulation(string $manipulationName): self

public function withoutManipulations(): self
{
$this->manipulations = new Manipulations();
$this->manipulations = new Manipulations;

return $this;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Conversions/ConversionCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ConversionCollection extends Collection

public static function createForMedia(Media $media): self
{
return (new static())->setMedia($media);
return (new static)->setMedia($media);
}

public function setMedia(Media $media): self
Expand Down Expand Up @@ -56,7 +56,7 @@ protected function addConversionsFromRelatedModel(Media $media): void
}

/** @var \Spatie\MediaLibrary\HasMedia $model */
$model = new $modelName();
$model = new $modelName;

/*
* In some cases the user might want to get the actual model
Expand Down
2 changes: 1 addition & 1 deletion src/Conversions/FileManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function performConversions(
return $onlyMissing && Storage::disk($media->disk)->exists($relativePath);
})
->each(function (Conversion $conversion) use ($media, $copiedOriginalFile) {
(new PerformConversionAction())->execute($conversion, $media, $copiedOriginalFile);
(new PerformConversionAction)->execute($conversion, $media, $copiedOriginalFile);
});

$temporaryDirectory->delete();
Expand Down
2 changes: 1 addition & 1 deletion src/Conversions/ImageGenerators/Svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function convert(string $file, ?Conversion $conversion = null): string
{
$imageFile = pathinfo($file, PATHINFO_DIRNAME).'/'.pathinfo($file, PATHINFO_FILENAME).'.jpg';

$image = new Imagick();
$image = new Imagick;
$image->readImage($file);
$image->setBackgroundColor(new ImagickPixel('none'));
$image->setImageFormat('jpg');
Expand Down
4 changes: 2 additions & 2 deletions src/InteractsWithMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function addMediaFromUrl(string $url, array|string ...$allowedMimeTypes):
}

$downloader = config('media-library.media_downloader', DefaultDownloader::class);
$temporaryFile = (new $downloader())->getTempFile($url);
$temporaryFile = (new $downloader)->getTempFile($url);
$this->guardAgainstInvalidMimeType($temporaryFile, $allowedMimeTypes);

$filename = basename(parse_url($url, PHP_URL_PATH));
Expand Down Expand Up @@ -383,7 +383,7 @@ public function updateMedia(array $newMediaArray, string $collectionName = 'defa
$this->removeMediaItemsNotPresentInArray($newMediaArray, $collectionName);

$mediaClass = $this->getMediaModel();
$mediaInstance = new $mediaClass();
$mediaInstance = new $mediaClass;
$keyName = $mediaInstance->getKeyName();

return collect($newMediaArray)
Expand Down
17 changes: 13 additions & 4 deletions src/MediaCollections/FileAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class FileAdder

protected string $diskName = '';

protected ?string $onQueue = null;

protected ?int $fileSize = null;

protected string $conversionsDiskName = '';
Expand Down Expand Up @@ -178,6 +180,13 @@ public function storingConversionsOnDisk(string $diskName): self
return $this;
}

public function onQueue(?string $queue = null): self
{
$this->onQueue = $queue;

return $this;
}

public function withManipulations(array $manipulations): self
{
$this->manipulations = $manipulations;
Expand Down Expand Up @@ -241,7 +250,7 @@ public function toMediaCollectionFromRemote(string $collectionName = 'default',

$mediaClass = $this->subject?->getMediaModel() ?? config('media-library.media_model');
/** @var Media $media */
$media = new $mediaClass();
$media = new $mediaClass;

$media->name = $this->mediaName;

Expand Down Expand Up @@ -304,7 +313,7 @@ public function toMediaCollection(string $collectionName = 'default', string $di

$mediaClass = $this->subject?->getMediaModel() ?? config('media-library.media_model');
/** @var Media $media */
$media = new $mediaClass();
$media = new $mediaClass;

$media->name = $this->mediaName;

Expand Down Expand Up @@ -466,7 +475,7 @@ protected function processMediaItem(HasMedia $model, Media $media, self $fileAdd
}
}

if ($this->generateResponsiveImages && (new ImageGenerator())->canConvert($media)) {
if ($this->generateResponsiveImages && (new ImageGenerator)->canConvert($media)) {
$generateResponsiveImagesJobClass = config('media-library.jobs.generate_responsive_images', GenerateResponsiveImagesJob::class);

$job = new $generateResponsiveImagesJobClass($media);
Expand All @@ -475,7 +484,7 @@ protected function processMediaItem(HasMedia $model, Media $media, self $fileAdd
$job->onConnection($customConnection);
}

if ($customQueue = config('media-library.queue_name')) {
if ($customQueue = ($this->onQueue ?? config('media-library.queue_name'))) {
$job->onQueue($customQueue);
}

Expand Down
2 changes: 1 addition & 1 deletion src/MediaCollections/HtmlableMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function lazy(): self

public function toHtml(): string
{
$imageGenerator = ImageGeneratorFactory::forMedia($this->media) ?? new Image();
$imageGenerator = ImageGeneratorFactory::forMedia($this->media) ?? new Image;

if (! $imageGenerator->canHandleMime($this->media->mime_type)) {
return '';
Expand Down
4 changes: 2 additions & 2 deletions src/MediaLibraryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function packageBooted(): void
{
$mediaClass = config('media-library.media_model', Media::class);

$mediaClass::observe(new MediaObserver());
$mediaClass::observe(new MediaObserver);
}

public function packageRegistered(): void
Expand All @@ -44,7 +44,7 @@ public function packageRegistered(): void
$this->app->scoped(MediaRepository::class, function () {
$mediaClass = config('media-library.media_model');

return new MediaRepository(new $mediaClass());
return new MediaRepository(new $mediaClass);
});
}
}
6 changes: 3 additions & 3 deletions tests/Conversions/Commands/CleanCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@

$this->urlGenerator = new DefaultUrlGenerator($this->config);

$this->pathGenerator = new CustomPathGenerator();
$this->pathGenerator = new CustomPathGenerator;

$this->urlGenerator->setPathGenerator($this->pathGenerator);

Expand Down Expand Up @@ -256,7 +256,7 @@

$this->urlGenerator = new DefaultUrlGenerator($this->config);

$this->pathGenerator = new TestPathGeneratorConversionsInOriginalImageDirectory();
$this->pathGenerator = new TestPathGeneratorConversionsInOriginalImageDirectory;

$this->urlGenerator->setPathGenerator($this->pathGenerator);

Expand Down Expand Up @@ -360,7 +360,7 @@
});

it('will not clean media items on soft deleted models', function () {
$testModelClass = new class() extends TestModel
$testModelClass = new class extends TestModel
{
use SoftDeletes;
};
Expand Down
4 changes: 2 additions & 2 deletions tests/Conversions/ConversionCustomWidthCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
use Spatie\MediaLibrary\Tests\TestSupport\WidthCalculators\FixedWidthCalculator;

it('can utilize various width calculators for conversions across different models', function () {
$testModel3Sizes = (new class() extends TestModel
$testModel3Sizes = (new class extends TestModel
{
public function registerMediaConversions(?Media $media = null): void
{
$this->addMediaConversion('fixed_width')->withWidthCalculator(new FixedWidthCalculator([99, 60, 33]))->withResponsiveImages();
}
})::create(['name' => 'test.jpg']);

$testModel5Sizes = (new class() extends TestModel
$testModel5Sizes = (new class extends TestModel
{
public function registerMediaConversions(?Media $media = null): void
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Conversions/FileManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
$imageFile = $this->getTestJpg();
$media = $this->testModelWithoutMediaConversions->addMedia($this->getTestJpg())->toMediaCollection();

$conversionTempFile = (new PerformManipulationsAction())->execute(
$conversionTempFile = (new PerformManipulationsAction)->execute(
$media,
$this->conversion->withoutManipulations(),
$imageFile
Expand Down
2 changes: 1 addition & 1 deletion tests/Conversions/ImageGenerators/AvifTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Spatie\MediaLibrary\Conversions\ImageGenerators\Avif;

it('can convert a avif', function () {
$imageGenerator = new Avif();
$imageGenerator = new Avif;

if (! $imageGenerator->requirementsAreInstalled()) {
$this->markTestSkipped('Skipping avif test because requirements to run it are not met');
Expand Down
16 changes: 8 additions & 8 deletions tests/Conversions/ImageGenerators/DualTypeCheckingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,55 @@
use Spatie\MediaLibrary\Tests\TestSupport\TestImageGenerator;

it('can convert an image with a valid extension and mime type', function () {
$generator = new TestImageGenerator();
$generator = new TestImageGenerator;
$generator->shouldMatchBothExtensionsAndMimeTypes = true;

$generator->supportedMimetypes->push('supported-mime-type');
$generator->supportedExtensions->push('supported-extension');

$media = new Media();
$media = new Media;
$media->mime_type = 'supported-mime-type';
$media->file_name = 'some-file.supported-extension';

expect($generator->canConvert($media))->toBeTrue();
});

it('cannot convert an image with an invalid extension and mime type', function () {
$generator = new TestImageGenerator();
$generator = new TestImageGenerator;
$generator->shouldMatchBothExtensionsAndMimeTypes = true;

$generator->supportedMimetypes->push('supported-mime-type');
$generator->supportedExtensions->push('supported-extension');

$media = new Media();
$media = new Media;
$media->mime_type = 'invalid-mime-type';
$media->file_name = 'some-file.invalid-extension';

expect($generator->canConvert($media))->toBeFalse();
});

it('cannot convert an image with only a valid mime type', function () {
$generator = new TestImageGenerator();
$generator = new TestImageGenerator;
$generator->shouldMatchBothExtensionsAndMimeTypes = true;

$generator->supportedMimetypes->push('supported-mime-type');
$generator->supportedExtensions->push('supported-extension');

$media = new Media();
$media = new Media;
$media->mime_type = 'supported-mime-type';
$media->file_name = 'some-file.invalid-extension';

expect($generator->canConvert($media))->toBeFalse();
});

it('cannot convert an image with only a valid extension', function () {
$generator = new TestImageGenerator();
$generator = new TestImageGenerator;
$generator->shouldMatchBothExtensionsAndMimeTypes = true;

$generator->supportedExtensions->push('supported-extension');
$generator->supportedMimetypes->push('supported-mime-type');

$media = new Media();
$media = new Media;
$media->mime_type = 'invalid-mime-type';
$media->file_name = 'some-file.supported-extension';

Expand Down
6 changes: 3 additions & 3 deletions tests/Conversions/ImageGenerators/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Spatie\MediaLibrary\Conversions\ImageGenerators\Image;

it('can convert an image', function () {
$imageGenerator = new Image();
$imageGenerator = new Image;

$media = $this->testModelWithoutMediaConversions->addMedia($this->getTestJpg())->toMediaCollection();

Expand All @@ -21,7 +21,7 @@ function () {
//TIFF format requires imagick
config(['media-library.image_driver' => 'imagick']);

$imageGenerator = new Image();
$imageGenerator = new Image;

$media = $this->testModelWithoutMediaConversions->addMedia($this->getTestTiff())->toMediaCollection();

Expand All @@ -40,7 +40,7 @@ function () {
//heic format requires imagick
config(['media-library.image_driver' => 'imagick']);

$imageGenerator = new Image();
$imageGenerator = new Image;

$media = $this->testModelWithoutMediaConversions->addMedia($this->getTestHeic())->toMediaCollection();

Expand Down
2 changes: 1 addition & 1 deletion tests/Conversions/ImageGenerators/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Spatie\MediaLibrary\Conversions\ImageGenerators\Pdf;

it('can convert a pdf', function () {
$imageGenerator = new Pdf();
$imageGenerator = new Pdf;

if (! $imageGenerator->requirementsAreInstalled()) {
$this->markTestSkipped('Skipping pdf test because requirements to run it are not met');
Expand Down
2 changes: 1 addition & 1 deletion tests/Conversions/ImageGenerators/SvgTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
it('can convert a svg', function () {
config()->set('media-library.image_driver', 'imagick');

$imageGenerator = new Svg();
$imageGenerator = new Svg;

if (! $imageGenerator->requirementsAreInstalled()) {
$this->markTestSkipped('Skipping svg test because requirements to run it are not met');
Expand Down
2 changes: 1 addition & 1 deletion tests/Conversions/ImageGenerators/VideoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Spatie\MediaLibrary\Conversions\ImageGenerators\Video;

it('can convert a video', function () {
$imageGenerator = new Video();
$imageGenerator = new Video;

if (! $imageGenerator->requirementsAreInstalled()) {
$this->markTestSkipped('Skipping video test because requirements to run it are not met');
Expand Down
2 changes: 1 addition & 1 deletion tests/Conversions/ImageGenerators/WebpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Spatie\MediaLibrary\Conversions\ImageGenerators\Webp;

it('can convert a webp', function () {
$imageGenerator = new Webp();
$imageGenerator = new Webp;

if (! $imageGenerator->requirementsAreInstalled()) {
$this->markTestSkipped('Skipping webp test because requirements to run it are not met');
Expand Down
4 changes: 2 additions & 2 deletions tests/Downloader/HttpFacadeDownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
->with($url)
->once();

$downloader = new \Spatie\MediaLibrary\Downloaders\HttpFacadeDownloader();
$downloader = new \Spatie\MediaLibrary\Downloaders\HttpFacadeDownloader;

$result = $downloader->getTempFile($url);

Expand All @@ -38,7 +38,7 @@
'https://example.com' => Http::response('::file::'),
]);

$downloader = new \Spatie\MediaLibrary\Downloaders\HttpFacadeDownloader();
$downloader = new \Spatie\MediaLibrary\Downloaders\HttpFacadeDownloader;

$result = $downloader->getTempFile($url);

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/FileAdder/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@
});

it('will throw an exception and revert database when file cannot be added and model uses softdeletes', function () {
$testModelClass = new class() extends TestModel
$testModelClass = new class extends TestModel
{
use SoftDeletes;
};
Expand Down
Loading

0 comments on commit 594e0a8

Please sign in to comment.