This commit is contained in:
2024-06-01 03:10:49 +02:00
parent 26551964b1
commit dd341ed642
21 changed files with 896 additions and 0 deletions

34
app/Importers/Image.php Normal file
View File

@@ -0,0 +1,34 @@
<?php
namespace App\Importers;
use App\ImportsMedia;
use Illuminate\Http\UploadedFile;
use App\Importers\Image\Jobs\{ImportImage, GenerateFullscreen, GenerateThumbnail};
use App\Models\Image as ImageModel;
use App\Models\Album;
class Image implements ImportsMedia {
public static function supports(UploadedFile $file): bool {
$supportedMimes = config('gallery.image.suffixes', ['png', 'gif', 'bmp', 'svg', 'avi', 'jpg', 'jpeg']);
return in_array($file->guessExtension(), $supportedMimes);
}
public function __construct(private UploadedFile $file, private Album $location)
{
}
public function import(): array {
$image = ImageModel::create([
'album_id' => $this->location->id
]);
$imageId = $image->id;
return [
new ImportImage($this->file->getPathname(), $image),
new GenerateFullscreen($image),
new GenerateThumbnail($image),
];
}
}

View File

@@ -0,0 +1,58 @@
<?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 GenerateFullscreen implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;
public string $source;
public string $destination;
public function __construct(public Image $image)
{
$this->source = Storage::disk('images')->path($this->image->album_id . '/original/' . $this->image->id . '.avif');
$this->destination = $this->image->album_id . '/lightbox/' . $this->image->id . '.avif';
}
public function handle(): void
{
if ($this->batch()->cancelled()) {
return;
}
$image = InterventionImage::read($this->source);
if($image->width() >= $image->height()) {
// landscape
$image->scaleDown(width: config('gallery.image.fullscreen.maxWidth', 2000));
} else {
// portrait
$image->scaleDown(height: config('gallery.image.fullscreen.maxHeight', 2000));
}
Storage::disk('images')->put($this->destination, $image->toAvif(config('gallery.image.quality', 80)));
}
public function failed(?Throwable $exception): void
{
$this->image->delete();
Log::error($exception, [
'image' => $this->image,
'source' => $this->source,
'destination' => $this->destination
]);
}
}

View File

@@ -0,0 +1,58 @@
<?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 GenerateThumbnail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;
public string $source;
public string $destination;
public function __construct(public Image $image)
{
$this->source = Storage::disk('images')->path($this->image->album_id . '/original/' . $this->image->id . '.avif');
$this->destination = $this->image->album_id . '/thumbnail/' . $this->image->id . '.avif';
}
public function handle(): void
{
if ($this->batch()->cancelled()) {
return;
}
$image = InterventionImage::read($this->source);
if($image->width() >= $image->height()) {
// landscape
$image->scaleDown(width: config('gallery.image.thumbnail.maxWidth', 150));
} else {
// portrait
$image->scaleDown(height: config('gallery.image.thumbnail.maxHeight', 150));
}
Storage::disk('images')->put($this->destination, $image->toAvif(config('gallery.image.quality', 80)));
}
public function failed(?Throwable $exception): void
{
$this->image->delete();
Log::error($exception, [
'image' => $this->image,
'source' => $this->source,
'destination' => $this->destination
]);
}
}

View File

@@ -0,0 +1,40 @@
<?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 Illuminate\Support\Facades\Log;
use Intervention\Image\Laravel\Facades\Image as InterventionImage;
use \Throwable;
class ImportImage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Batchable;
public function __construct(public string $tmpFile, public Image $image)
{
}
public function handle(): void
{
if ($this->batch()->cancelled()) {
return;
}
$encoded = InterventionImage::read($this->tmpFile)->toAvif(config('gallery.image.quality', 80));
Storage::disk('images')->put("{$this->image->album->id}/original/{$this->image->id}.avif", $encoded);
}
public function failed(?Throwable $exception): void
{
Log::error($exception, ['image' => $this->image, 'tmpFile' => $this->tmpFile]);
$this->image->delete();
}
}

13
app/ImportsMedia.php Normal file
View File

@@ -0,0 +1,13 @@
<?php
namespace App;
use App\Models\Album;
use Illuminate\Http\UploadedFile;
interface ImportsMedia
{
public static function supports(UploadedFile $file): bool;
public function __construct(UploadedFile $file, Album $location);
public function import(): array;
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Jobs;
use App\Models\Album;
use App\Models\Image;
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 Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
class ImportMediaJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(private TemporaryUploadedFile $file, private Album $album)
{
}
public function handle(): void
{
if($this->isImage()) {
$image = Image::create([
'album' => $this->album,
'isCover' => false
]);
$encoded = InterventionImage::read($this->file)->toAvif(config('gallery.original.quality', 80));
Storage::disk('images')->put("{$$this->album->id}/original/{$image->id}.avif");
}
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Livewire\Drawer\Album;
use App\Models\BatchMutation;
use App\Models\MutationBatch;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Bus;
use Livewire\Attributes\Locked;
use Livewire\Component;
class ProgressMonitor extends Component
{
#[Locked]
public BatchMutation $mutation;
#[Locked]
public float $progress;
#[Locked]
public int $processedJobs;
#[Locked]
public int $totalJobs;
#[Locked]
public int $failedJobs;
#[Locked]
public string $currentUrl;
public function mounted(BatchMutation $mutation) : void {
$this->mutation = $mutation;
$this->currentUrl = url()->current();
}
public function monitorProgress() : void {
$batch = Bus::findBatch($this->mutation->batch_id);
$this->progress = $batch->progress();
$this->totalJobs = $batch->totalJobs;
$this->processedJobs = $batch->processedJobs();
$this->failedJobs = $batch->failedJobs;
if($batch == null || $batch->finished()) {
$this->mutation->delete();
$this->redirect($this->currentUrl, navigate: true);
}
}
public function render(): View|Factory
{
$this->monitorProgress();
return view('livewire.drawer.album.progress-monitor');
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Bus\Batch;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Bus;
class BatchMutation extends Model
{
use HasFactory;
public $fillable = ['batch_id', 'album_id'];
public function album() : BelongsTo {
return $this->belongsTo(Album::class);
}
public function getBatchAttribute() : Batch {
return Bus::findBatch($this->batchId);
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Laravel\Horizon\Horizon;
use Laravel\Horizon\HorizonApplicationServiceProvider;
class HorizonServiceProvider extends HorizonApplicationServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
parent::boot();
// Horizon::routeSmsNotificationsTo('15556667777');
// Horizon::routeMailNotificationsTo('example@example.com');
// Horizon::routeSlackNotificationsTo('slack-webhook-url', '#channel');
}
/**
* Register the Horizon gate.
*
* This gate determines who can access Horizon in non-local environments.
*/
protected function gate(): void
{
Gate::define('viewHorizon', function ($user) {
return in_array($user->email, [
//
]);
});
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Providers;
use App\Services\MediaImporter;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
class MediaImporterServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(MediaImporter::class, function (Application $app) {
return new MediaImporter();
});
}
public function boot(MediaImporter $service): void
{
$importers = config('gallery.importers');
foreach ($importers as $importer) {
$service->register($importer);
}
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Services;
use App\ImportsMedia;
use App\Models\Album;
use Illuminate\Foundation\Bus\PendingChain;
use Illuminate\Http\UploadedFile;
use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException;
class MediaImporter {
private array $importers = [];
public function register(string $importer) : void {
if(in_array(ImportsMedia::class, class_implements($importer))) {
array_push($this->importers, $importer);
}
}
public function import(UploadedFile $file, Album $location): array {
foreach ($this->importers as $importer) {
if($importer::supports($file)) {
return (new $importer($file, $location))->import();
}
}
throw new UnsupportedMediaTypeHttpException("The provided MediaType is not supported");
}
}