This commit is contained in:
2024-06-12 19:51:41 +02:00
parent 154e79aacd
commit 0ce904a7d8
18 changed files with 104 additions and 146 deletions

View File

@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Http\Requests\StoreImageRequest;
use App\Http\Requests\UpdateImageRequest;
use App\Models\Image;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
@@ -37,15 +38,41 @@ class ImageController extends Controller
/**
* Display the specified resource.
*/
public function show(Image $image, string $size = 'original') : BinaryFileResponse
public function show(Image $image, string $size = 'original') : BinaryFileResponse|Response
{
return response()->file(Storage::disk('images')->path($image->album->id . '/' . $size . '/' . $image->id . '.avif'));
$server = request()->server;
$headerEtag = md5($image->updated_at->format('U') . $image->id . '_' . $size);
$headerExpires = $image->updated_at->addYear()->toRfc2822String();
$headerLastModified = $image->updated_at->toRfc2822String();
$requestModifiedSince =
$server->has('HTTP_IF_MODIFIED_SINCE') &&
$server->get('HTTP_IF_MODIFIED_SINCE') === $headerLastModified;
$requestNoneMatch =
$server->has('HTTP_IF_NONE_MATCH') &&
$server->get('HTTP_IF_NONE_MATCH') === $headerEtag;
$headers = [
'Cache-Control' => 'max-age=0, must-revalidate, private',
'Content-Disposition' => sprintf('inline; filename="%s"', $image->id . '_' . $size),
'Etag' => $headerEtag,
'Expires' => $headerExpires,
'Last-Modified' => $headerLastModified,
'Pragma' => 'no-cache',
];
if ($requestModifiedSince || $requestNoneMatch) {
return response('', 304)->withHeaders($headers);
}
return response()->file(Storage::disk('images')->path($image->album->id . '/' . $size . '/' . $image->id . '.avif'), $headers);
}
/**
* Display the thumbnail of the specified resource.
*/
public function thumbnail(Image $image) : BinaryFileResponse
public function thumbnail(Image $image) : BinaryFileResponse|Response
{
return $this->show($image, 'thumbnail');
}
@@ -53,7 +80,7 @@ class ImageController extends Controller
/**
* Display the lightbox of the specified resource.
*/
public function lightbox(Image $image) : BinaryFileResponse
public function lightbox(Image $image) : BinaryFileResponse|Response
{
return $this->show($image, 'lightbox');
}

View File

@@ -2,11 +2,13 @@
namespace App\Importers;
use App\Importers\Image\Jobs\FinishImageModification;
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;
use Illuminate\Support\Facades\Bus;
class Image implements ImportsMedia {
@@ -19,16 +21,19 @@ class Image implements ImportsMedia {
{
}
public function import(): array {
public function import(): void {
$image = ImageModel::create([
'album_id' => $this->location->id
]);
$imageId = $image->id;
return [
Bus::chain([
new ImportImage($this->file->getPathname(), $image),
new GenerateFullscreen($image),
new GenerateThumbnail($image),
];
Bus::batch([
new GenerateFullscreen($image),
new GenerateThumbnail($image),
]),
new FinishImageModification($image),
])->dispatch();
}
}

View File

@@ -29,7 +29,7 @@ class GenerateFullscreen implements ShouldQueue
public function handle(): void
{
if ($this->batch()->cancelled()) {
if (method_exists($this, 'batch') && $this->batch()?->cancelled()) {
return;
}

View File

@@ -29,7 +29,7 @@ class GenerateThumbnail implements ShouldQueue
public function handle(): void
{
if ($this->batch()->cancelled()) {
if (method_exists($this, 'batch') && $this->batch()?->cancelled()) {
return;
}

View File

@@ -24,7 +24,7 @@ class ImportImage implements ShouldQueue
public function handle(): void
{
if ($this->batch()->cancelled()) {
if (method_exists($this, 'batch') && $this->batch()?->cancelled()) {
return;
}

View File

@@ -9,5 +9,5 @@ interface ImportsMedia
{
public static function supports(UploadedFile $file): bool;
public function __construct(UploadedFile $file, Album $location);
public function import(): array;
public function import(): void;
}

View File

@@ -9,7 +9,6 @@ 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\Validate;
use Livewire\Component;
use Livewire\Features\SupportFileUploads\WithFileUploads;
@@ -30,17 +29,9 @@ class AddImage extends Component
public function save(MediaImporter $importer) : void {
$this->validate();
$jobs = array_map(fn($file) => $importer->import($file, $this->album), $this->media);
$batch = Bus::batch($jobs)
->name('Media import in ' . $this->album->name)
->allowFailures()
->dispatch();
BatchMutation::create([
'album_id' => $this->album->id,
'batch_id' => $batch->id,
]);
foreach ($this->media as $file) {
$importer->import($file, $this->album);
}
$this->redirect(route('album.show', $this->album), navigate: true);
}

View File

@@ -28,6 +28,14 @@ class Album extends Model
return $this->images;
}
public function getHasCoverAttribute() : bool {
return $this->images()->where('isCover', 1)->count() > 0;
}
public function getHasProcessingMediaAttribute() : bool {
return $this->images()->where('isProcessing', 1)->count() > 0;
}
public function getThumbnailAttribute() : ?string {
return $this->images()->where('isCover', 1)->first()?->getThumbnail();
}

View File

@@ -18,6 +18,7 @@ class Image extends Model implements HasThumbnail
*/
protected $attributes = [
'isCover' => false,
'isProcessing' => true,
'lightboxWidth' => 0,
'lightboxHeight' => 0,
];
@@ -27,7 +28,7 @@ class Image extends Model implements HasThumbnail
*
* @var array
*/
protected $fillable = ['album_id', 'lightboxWidth', 'lightboxHeight'];
protected $fillable = ['album_id', 'lightboxWidth', 'lightboxHeight', 'isCover', 'isProcessing'];
public function album(): BelongsTo
{
@@ -35,11 +36,11 @@ class Image extends Model implements HasThumbnail
}
public function getThumbnail() : string {
return route('image.thumbnail', $this);
return route('image.thumbnail', $this) . '?cacheBuster3000=' . $this->updated_at->timestamp;
}
public function getDownload() : string {
return route('image.download', $this);
return route('image.download', $this) . '?cacheBuster3000=' . $this->updated_at->timestamp;
}
public function setLightboxSize(int $width, int $height) : void {
@@ -50,7 +51,7 @@ class Image extends Model implements HasThumbnail
public function getLightboxAttribute() : array {
return [
'location' => route('image.lightbox', $this),
'location' => route('image.lightbox', $this) . '?cacheBuster3000=' . $this->updated_at->timestamp,
'width' => $this->lightboxWidth,
'height' => $this->lightboxHeight,
];

View File

@@ -4,7 +4,6 @@ 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;
@@ -17,10 +16,11 @@ class MediaImporter {
}
}
public function import(UploadedFile $file, Album $location): array {
public function import(UploadedFile $file, Album $location): void {
foreach ($this->importers as $importer) {
if($importer::supports($file)) {
return (new $importer($file, $location))->import();
(new $importer($file, $location))->import();
return;
}
}