Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve job dispatching for media optimization and preview generation #2

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/FileStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ protected function persistMedia(Media $media, ?UploadedFile $uploadedFile): Medi
throw new UploadException();
}

if ($this->config->generatePreview) {
$this->generatePreview($media);
}
$this->dispatchJobs($media);

AfterMediaUploaded::dispatch($media);
}
Expand All @@ -145,17 +143,22 @@ protected function persistMedia(Media $media, ?UploadedFile $uploadedFile): Medi
}

/**
* Generate preview for the given Media.
* Dispatches jobs for optimizing and generating preview.
*/
protected function generatePreview(Media $media): void
protected function dispatchJobs(Media $media): void
{
$jobs = [];

if ($media->is_image) {
Bus::chain([
new OptimizeImage($media),
new GeneratePreview($media),
])->onQueue($this->config->queueName)->dispatch();
} elseif ($media->is_pdf) {
GeneratePreview::dispatch($media)->onQueue($this->config->queueName);
$jobs[] = new OptimizeImage($media);
}

if ($this->config->generatePreview && ($media->is_image || $media->is_pdf)) {
$jobs[] = new GeneratePreview($media);
}

if ($jobs) {
Bus::chain($jobs)->onQueue($this->config->queueName)->dispatch();
}
}

Expand Down
Loading