Fix crash when artwork image uploaded is too large.

This commit is contained in:
Alex Cabal 2025-06-22 16:11:07 -05:00
parent eaa8d4f13b
commit 0d2dcc3772
3 changed files with 25 additions and 12 deletions

View file

@ -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);

View file

@ -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;
}
}

View file

@ -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;