ldc-cocotte/app/Services/PublicImageStorageService.php

46 lines
1.2 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace App\Services;
use App\Lib\PublicS3Disk;
use Illuminate\Http\UploadedFile;
use Uwil\Storage\Contracts\StorageService;
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
final readonly class PublicImageStorageService
{
public function __construct(
private StorageService $storageService,
private PublicS3Disk $s3Disk,
private ImageOptimizerService $imageOptimizerService,
) {
}
public function store(UploadedFile $file): StorageFilePath
{
$optimized = $this->imageOptimizerService->optimize($file);
return $this->storageService->storeFileOntoDisk($optimized, $this->s3Disk->disk);
}
public function deleteIfReplaced(
?StorageFilePath $oldPath,
?StorageFilePath $newPath,
): void {
if ($oldPath === null || $newPath === null || $oldPath->value === $newPath->value) {
return;
}
$this->deleteIfExists($oldPath);
}
public function deleteIfExists(?StorageFilePath $path): void
{
if ($path !== null && $this->storageService->fileExistsOnDisk($path, $this->s3Disk->disk)) {
$this->storageService->removeFromDisk($path, $this->s3Disk->disk);
}
}
}