diff --git a/lib/Artwork.php b/lib/Artwork.php index 3aecb008..164210a6 100644 --- a/lib/Artwork.php +++ b/lib/Artwork.php @@ -508,6 +508,13 @@ final class Artwork{ $error->Add(new Exceptions\InvalidImageUploadException('An image is required.')); } else{ + try{ + $this->MimeType = Enums\ImageMimeType::FromFile($imagePath) ?? throw new Exceptions\InvalidMimeTypeException(); + } + catch(Exceptions\InvalidMimeTypeException $ex){ + $error->Add($ex); + } + if(!is_writable(WEB_ROOT . COVER_ART_UPLOAD_PATH)){ $error->Add(new Exceptions\InvalidImageUploadException('Upload path not writable.')); } @@ -525,10 +532,6 @@ final class Artwork{ } } - if(!isset($this->MimeType)){ - $error->Add(new Exceptions\InvalidMimeTypeException()); - } - if($error->HasExceptions){ throw $error; } @@ -681,8 +684,6 @@ final class Artwork{ * @throws Exceptions\InvalidImageUploadException */ public function Create(?string $imagePath = null): void{ - $this->MimeType = Enums\ImageMimeType::FromFile($imagePath) ?? throw new Exceptions\InvalidImageUploadException(); - $this->Validate($imagePath, true); $this->Created = NOW; @@ -750,8 +751,6 @@ final class Artwork{ unset($this->_UrlName); if($imagePath !== null){ - $this->MimeType = Enums\ImageMimeType::FromFile($imagePath) ?? throw new Exceptions\InvalidImageUploadException(); - // Manually set the updated timestamp, because if we only update the image and nothing else, the row's updated timestamp won't change automatically. $this->Updated = NOW; unset($this->_ImageUrl); diff --git a/lib/Exceptions/ValidationException.php b/lib/Exceptions/ValidationException.php index 3dcb2c6f..2746de32 100644 --- a/lib/Exceptions/ValidationException.php +++ b/lib/Exceptions/ValidationException.php @@ -45,4 +45,16 @@ class ValidationException extends AppException{ return false; } + + public function Remove(string $exception): void{ + $newExceptions = []; + + foreach($this->Exceptions as $childException){ + if(!is_a($childException, $exception)){ + $newExceptions[] = $childException; + } + } + + $this->Exceptions = $newExceptions; + } } diff --git a/www/artworks/post.php b/www/artworks/post.php index 38412cbc..aad357f2 100644 --- a/www/artworks/post.php +++ b/www/artworks/post.php @@ -7,10 +7,6 @@ try{ $exceptionRedirectUrl = '/artworks/new'; $artwork = new Artwork(); - if(HttpInput::IsRequestTooLarge()){ - throw new Exceptions\InvalidRequestException('File upload too large.'); - } - if(Session::$User === null){ throw new Exceptions\LoginRequiredException(); } @@ -164,6 +160,12 @@ catch(Exceptions\InvalidArtworkException | Exceptions\InvalidArtworkTagException $ex = new Exceptions\InvalidImageUploadException(); } + // If the `Artwork` reports that no image is uploaded, check to see if the image upload was too large. If so, show the user a clearer error message. + if($ex instanceof Exceptions\InvalidArtworkException && $ex->Has(Exceptions\InvalidImageUploadException::class) && HttpInput::IsRequestTooLarge()){ + $ex->Remove(Exceptions\InvalidImageUploadException::class); + $ex->Add(new Exceptions\InvalidRequestException('File upload too large.')); + } + $_SESSION['artwork'] = $artwork; $_SESSION['exception'] = $ex;