This commit is contained in:
2024-06-12 19:51:59 +02:00
parent 0ce904a7d8
commit 813874a847
16 changed files with 2113 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Importers\Image\Jobs;
use App\Models\Image;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use \Throwable;
use Illuminate\Support\Facades\Log;
class FinishImageModification implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;
public function __construct(public Image $image)
{
}
public function handle(): void
{
if (method_exists($this, 'batch') && $this->batch()?->cancelled()) {
return;
}
$this->image->fill([
'isCover' => $this->getCoverFlag(),
'isProcessing' => false,
]);
$this->image->save();
// TODO: Dispatch image cleaned event
}
private function getCoverFlag() : bool {
if(!$this->image->album->hasCover && $this->image->album->images->sortBy('id')->first()->id == $this->image->id) {
return true;
}
return false;
}
public function failed(?Throwable $exception): void
{
Log::error($exception, [
'image' => $this->image,
]);
}
}

View File

@@ -0,0 +1,50 @@
<?php
namespace App\Importers\Image\Jobs;
use App\Models\Image;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Laravel\Facades\Image as InterventionImage;
use \Throwable;
use Illuminate\Support\Facades\Log;
class RotateImage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;
public string $source;
public string $destination;
public function __construct(public Image $image, public int $degrees)
{
$this->source = Storage::disk('images')->path($this->image->album_id . '/original/' . $this->image->id . '.avif');
$this->destination = $this->image->album_id . '/original/' . $this->image->id . '.avif';
}
public function handle(): void
{
if (method_exists($this, 'batch') && $this->batch()?->cancelled()) {
return;
}
$rotated = InterventionImage::read($this->source);
$rotated = $rotated->rotate($this->degrees);
Storage::disk('images')->put($this->destination, $rotated->toAvif(config('gallery.image.quality', 80)));
}
public function failed(?Throwable $exception): void
{
Log::error($exception, [
'image' => $this->image,
'source' => $this->source,
'destination' => $this->destination
]);
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace App\Livewire\Album;
use App\Importers\Image\Jobs\FinishImageModification;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Bus;
use Livewire\Attributes\Locked;
use Livewire\Attributes\On;
use Livewire\Attributes\Title;
use Livewire\Component;
use App\Models\Album;
use App\Models\Image;
use App\Importers\Image\Jobs\RotateImage;
use App\Importers\Image\Jobs\GenerateFullscreen;
use App\Importers\Image\Jobs\GenerateThumbnail;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
class Show extends Component
{
#[Locked]
public Album $album;
#[Locked]
public EloquentCollection $images;
public function mount(Album $album): void {
$this->album = $album;
$this->images = $album->images;
}
#[On('image.rotate')]
public function rotate(int $image_id, string $direction):void {
$degrees = match ($direction) {
'cw' => -90,
'ccw' => 90,
default => 0,
};
$this->dispatchRotateJob(Image::findOrFail($image_id), $degrees);
$this->redirect(route('album.show', $this->album), navigate: true);
}
private function dispatchRotateJob(Image $image, int $degrees) : void {
$image->update([
'isProcessing' => true,
]);
Bus::chain([
new RotateImage($image, $degrees),
Bus::batch([
new GenerateFullscreen($image),
new GenerateThumbnail($image),
]),
new FinishImageModification($image),
])->dispatch();
}
#[Title('Show Album')]
public function render(): View|Factory
{
return view('livewire.album.show');
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Livewire\Image;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Livewire\Component;
use Livewire\Attributes\On;
class Grid extends Component
{
public Collection $images;
#[On('image.rotate')]
public function rotate(int id, string $direction) : void {
$degree = match ($direction) {
'ccw' => -90,
'cw' => 90,
default => 0,
}
}
public function mount(Collection $images): void {
$this->images = $images;
}
public function render(): View|Factory
{
return view('livewire.image.grid');
}
}