From 6481dd058427c7ca9f84d5e9c4d3ed62f5d3dd27 Mon Sep 17 00:00:00 2001 From: julien Date: Sun, 22 Mar 2026 12:49:53 +0100 Subject: [PATCH] Minor changes --- .../Infrastructure/LocalMediaStorage.php | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Media/Infrastructure/LocalMediaStorage.php b/src/Media/Infrastructure/LocalMediaStorage.php index aff5ead..c890afa 100644 --- a/src/Media/Infrastructure/LocalMediaStorage.php +++ b/src/Media/Infrastructure/LocalMediaStorage.php @@ -97,12 +97,11 @@ class LocalMediaStorage implements MediaStorageInterface public function storePreparedUpload(UploadedMediaInterface $uploadedFile, PreparedMediaUpload $preparedUpload): string { - if (!is_dir($this->uploadDir) && !@mkdir($this->uploadDir, 0755, true)) { - throw new StorageException("Impossible de créer le répertoire d'upload"); - } + $this->ensureUploadDirectoryExists(); $filename = uniqid('', true) . '_' . bin2hex(random_bytes(4)) . '.' . $preparedUpload->getExtension(); $destPath = $this->uploadDir . DIRECTORY_SEPARATOR . $filename; + $this->ensureParentDirectoryExists($destPath); if ($preparedUpload->shouldCopyFromTemporaryPath()) { if (!copy($preparedUpload->getTemporaryPath(), $destPath)) { @@ -136,6 +135,22 @@ class LocalMediaStorage implements MediaStorageInterface } } + private function ensureUploadDirectoryExists(): void + { + if (!is_dir($this->uploadDir) && !@mkdir($this->uploadDir, 0755, true) && !is_dir($this->uploadDir)) { + throw new StorageException("Impossible de créer le répertoire d'upload"); + } + } + + private function ensureParentDirectoryExists(string $path): void + { + $directory = dirname($path); + + if (!is_dir($directory) && !@mkdir($directory, 0755, true) && !is_dir($directory)) { + throw new StorageException("Impossible de préparer le répertoire cible du média"); + } + } + private function assertReasonableDimensions(string $path): void { $size = @getimagesize($path);