diff --git a/app/exceptions/FileUploadException.inc b/app/exceptions/FileUploadException.inc new file mode 100644 index 00000000..3fb62e6f --- /dev/null +++ b/app/exceptions/FileUploadException.inc @@ -0,0 +1,75 @@ + + * @copyright 2014 Heinrich-Heine-Universität Düsseldorf + * @license http://www.gnu.org/licenses/gpl.html + * @link https://bitbucket.org/coderkun/the-legend-of-z + */ + + namespace hhu\z\exceptions; + + + /** + * Exception: File upload went wrong + * + * @author Oliver Hanraths + */ + class FileUploadException extends \nre\core\Exception + { + /** + * Error code + * + * @var int + */ + const CODE = 203; + /** + * Error message + * + * @var string + */ + const MESSAGE = 'File upload went wrong'; + + /** + * Nested message + * + * @var string + */ + private $nestedMessage; + + + + + /** + * Construct a new exception. + */ + function __construct($nestedMessage=null, $message=self::MESSAGE, $code=self::CODE) + { + parent::__construct( + $message, + $code, + $nestedMessage + ); + + // Store values + $this->nestedMessage = $nestedMessage; + } + + + + + /** + * Get nested message. + * + * @return Nested message + */ + public function getNestedMessage() + { + return $this->nestedMessage; + } + + } + +?> diff --git a/questtypes/submit/SubmitQuesttypeController.inc b/questtypes/submit/SubmitQuesttypeController.inc index 42f98aeb..eecafb3b 100644 --- a/questtypes/submit/SubmitQuesttypeController.inc +++ b/questtypes/submit/SubmitQuesttypeController.inc @@ -49,6 +49,13 @@ { $answer = $_FILES['answers']; + // Check error + if($answer['error'] !== 0) { + throw new \hhu\z\exceptions\SubmissionNotValidException( + new \hhu\z\exceptions\FileUploadException($answer['error']) + ); + } + // Check mimetype $mimetypes = $this->Submit->getAllowedMimetypes($seminary['id']); $answerMimetype = null; @@ -58,13 +65,13 @@ break; } } - - // Check file if(is_null($answerMimetype)) { throw new \hhu\z\exceptions\SubmissionNotValidException( new \hhu\z\exceptions\WrongFiletypeException() ); } + + // Check file size if($answer['size'] > $answerMimetype['size']) { throw new \hhu\z\exceptions\SubmissionNotValidException( new \hhu\z\exceptions\MaxFilesizeException() @@ -72,7 +79,11 @@ } // Save file - $this->Submit->setCharacterSubmission($seminary['id'], $quest['id'], $this->Auth->getUserId(), $character['id'], $answer); + if(!$this->Submit->setCharacterSubmission($seminary['id'], $quest['id'], $this->Auth->getUserId(), $character['id'], $answer)) { + throw new \hhu\z\exceptions\SubmissionNotValidException( + new \hhu\z\exceptions\FileUploadException(error_get_last()['message']) + ); + } } } diff --git a/questtypes/submit/html/quest.tpl b/questtypes/submit/html/quest.tpl index 34e30f7c..47b45bd4 100644 --- a/questtypes/submit/html/quest.tpl +++ b/questtypes/submit/html/quest.tpl @@ -4,6 +4,8 @@ getNestedException() instanceof \hhu\z\exceptions\WrongFiletypeException) : ?> + getNestedException() instanceof \hhu\z\exceptions\FileUploadException) : ?> + getNestedException()->getNestedMessage())?> getNestedException()->getMessage()?>