add FileUploadException
This commit is contained in:
parent
8abf29b2b0
commit
1a11d220a9
3 changed files with 91 additions and 3 deletions
75
app/exceptions/FileUploadException.inc
Normal file
75
app/exceptions/FileUploadException.inc
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
||||||
|
|
@ -49,6 +49,13 @@
|
||||||
{
|
{
|
||||||
$answer = $_FILES['answers'];
|
$answer = $_FILES['answers'];
|
||||||
|
|
||||||
|
// Check error
|
||||||
|
if($answer['error'] !== 0) {
|
||||||
|
throw new \hhu\z\exceptions\SubmissionNotValidException(
|
||||||
|
new \hhu\z\exceptions\FileUploadException($answer['error'])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Check mimetype
|
// Check mimetype
|
||||||
$mimetypes = $this->Submit->getAllowedMimetypes($seminary['id']);
|
$mimetypes = $this->Submit->getAllowedMimetypes($seminary['id']);
|
||||||
$answerMimetype = null;
|
$answerMimetype = null;
|
||||||
|
|
@ -58,13 +65,13 @@
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check file
|
|
||||||
if(is_null($answerMimetype)) {
|
if(is_null($answerMimetype)) {
|
||||||
throw new \hhu\z\exceptions\SubmissionNotValidException(
|
throw new \hhu\z\exceptions\SubmissionNotValidException(
|
||||||
new \hhu\z\exceptions\WrongFiletypeException()
|
new \hhu\z\exceptions\WrongFiletypeException()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check file size
|
||||||
if($answer['size'] > $answerMimetype['size']) {
|
if($answer['size'] > $answerMimetype['size']) {
|
||||||
throw new \hhu\z\exceptions\SubmissionNotValidException(
|
throw new \hhu\z\exceptions\SubmissionNotValidException(
|
||||||
new \hhu\z\exceptions\MaxFilesizeException()
|
new \hhu\z\exceptions\MaxFilesizeException()
|
||||||
|
|
@ -72,7 +79,11 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save file
|
// 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'])
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@
|
||||||
<?=_('File has wrong type')?>
|
<?=_('File has wrong type')?>
|
||||||
<?php elseif($exception->getNestedException() instanceof \hhu\z\exceptions\WrongFiletypeException) : ?>
|
<?php elseif($exception->getNestedException() instanceof \hhu\z\exceptions\WrongFiletypeException) : ?>
|
||||||
<?=_('File exceeds size maximum')?>
|
<?=_('File exceeds size maximum')?>
|
||||||
|
<?php elseif($exception->getNestedException() instanceof \hhu\z\exceptions\FileUploadException) : ?>
|
||||||
|
<?=sprintf(_('Error during file upload: %s'), $exception->getNestedException()->getNestedMessage())?>
|
||||||
<?php else : ?>
|
<?php else : ?>
|
||||||
<?=$exception->getNestedException()->getMessage()?>
|
<?=$exception->getNestedException()->getMessage()?>
|
||||||
<?php endif ?>
|
<?php endif ?>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue