ldc-cocotte/app/Services/PublicImageStorageService.php
Jérémy Munsch 86e7340c48
feat(products): add success and failure handling for product actions
- Implemented HandlesSuccess and HandlesFailure interfaces in CreateProduct and UpdateProduct actions.
- Added tracking of uploaded paths and cleanup on failure.
- Enhanced image storage handling to ensure proper cleanup of orphaned files.

feat(ranges): add success and failure handling for range actions

- Implemented HandlesSuccess and HandlesFailure interfaces in CreateRange and UpdateRange actions.
- Added tracking of uploaded paths and cleanup on failure.
- Enhanced image storage handling to ensure proper cleanup of orphaned files.

feat(slides): add success and failure handling for slide actions

- Implemented HandlesSuccess and HandlesFailure interfaces in CreateSlide and UpdateSlide actions.
- Added tracking of uploaded paths and cleanup on failure.
- Enhanced image storage handling to ensure proper cleanup of orphaned files.

chore: introduce TracksPendingStoragePaths trait

- Created a new trait to manage tracking of uploaded and obsolete storage paths.
- This will streamline the handling of file cleanup across various actions.

test: add tests for action failure handling and cleanup

- Added tests to verify that uploaded files are cleaned up on action failure.
- Ensured that previous files are preserved during rollback scenarios.
2026-11-15 20:31:37 +01:00

45 lines
1.2 KiB
PHP

<?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);
}
}
}