add FileUploadException

This commit is contained in:
coderkun 2014-04-04 13:51:24 +02:00
commit 1a11d220a9
3 changed files with 91 additions and 3 deletions

View file

@ -0,0 +1,75 @@
<?php
/**
* The Legend of Z
*
* @author Oliver Hanraths <oliver.hanraths@uni-duesseldorf.de>
* @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 <oliver.hanraths@uni-duesseldorf.de>
*/
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;
}
}
?>

View file

@ -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'])
);
}
}
}

View file

@ -4,6 +4,8 @@
<?=_('File has wrong type')?>
<?php elseif($exception->getNestedException() instanceof \hhu\z\exceptions\WrongFiletypeException) : ?>
<?=_('File exceeds size maximum')?>
<?php elseif($exception->getNestedException() instanceof \hhu\z\exceptions\FileUploadException) : ?>
<?=sprintf(_('Error during file upload: %s'), $exception->getNestedException()->getNestedMessage())?>
<?php else : ?>
<?=$exception->getNestedException()->getMessage()?>
<?php endif ?>