2026-11-15 17:13:39 +01:00
|
|
|
<?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;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-15 20:31:37 +01:00
|
|
|
$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);
|
2026-11-15 17:13:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|