From e61ea99e41d86c2e23e55884972ca7a8aca6fb46 Mon Sep 17 00:00:00 2001 From: Martin Kluska Date: Mon, 3 Jul 2017 12:56:14 +0200 Subject: [PATCH] Raise exception if file upload has an error #17 --- readme.md | 12 ++++++++---- src/Exceptions/UploadFailedException.php | 14 ++++++++++++++ src/Receiver/FileReceiver.php | 11 +++++++++-- 3 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 src/Exceptions/UploadFailedException.php diff --git a/readme.md b/readme.md index 858ff5c..c94d437 100644 --- a/readme.md +++ b/readme.md @@ -76,7 +76,8 @@ necessary, add to `api` routes and change the config to use IP for chunk name. 4. Implement the FileReceiver (example below) ### FileReceiver -You must create the file receiver with the file index (in the `Request->file`), the current request and the desired handler class (currently the `ContentRangeUploadHandler::class`) +- You must create the file receiver with the file index (in the `Request->file`), the current request and the desired handler class (currently the `ContentRangeUploadHandler::class`) +- If upload has failed (like file size limit, exception is raised) Then you can use methods: @@ -199,10 +200,10 @@ This will force a given handler to be used for processing the upload. */ public function upload(Request $request) { - // create the file receiver + // Create the file receiver, exception is thrown if file upload is invalid (size limit, etc) $receiver = new FileReceiver("file", $request, ContentRangeUploadHandler::class); - // check if the upload is success + // Check if the upload is success if ($receiver->isUploaded()) { // receive the file @@ -245,7 +246,6 @@ public function upload(Request $request) { * @throws UploadMissingFileException */ public function upload(Request $request) { - // Response for the files - completed and uncompleted $files = []; @@ -260,6 +260,7 @@ public function upload(Request $request) { // Loop sent files foreach ($files as $file) { // Instead of passing the index name, pass the UploadFile object from the $files array we are looping + // Exception is thrown if file upload is invalid (size limit, etc) // Create the file receiver via dynamic handler $receiver = new FileReceiver($file, $request, HandlerFactory::classFromRequest($request)); @@ -395,6 +396,7 @@ You can use the automatic detection of the correct handler (provider) by using t a third parameter when constructing the `FileReceiver`. ```php +// Exception is thrown if file upload is invalid (size limit, etc) $receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request)); ``` #### Fallback class @@ -411,6 +413,8 @@ HandlerFactory::classFromRequest($request, CustomHandler::class) ``` ## Changelog +### Since 1.1.0 +* If there is an error while upload, exception will be thrown on init. ### Since 1.0.3 * Enabled to construct the `FileReceiver` with dependency injection - the fasted way. diff --git a/src/Exceptions/UploadFailedException.php b/src/Exceptions/UploadFailedException.php new file mode 100644 index 0000000..aa131af --- /dev/null +++ b/src/Exceptions/UploadFailedException.php @@ -0,0 +1,14 @@ +config = is_null($config) ? AbstractConfig::config() : $config; if ($this->isUploaded()) { + if (!$this->file->isValid()) { + throw new UploadFailedException($this->file->getErrorMessage()); + } + $this->handler = new $handlerClass($this->request, $this->file, $this->config); } } @@ -70,7 +77,7 @@ public function __construct($fileIndexOrFile, Request $request, $handlerClass, $ */ public function isUploaded() { - return is_object($this->file); + return is_object($this->file) && $this->file->getError() !== UPLOAD_ERR_NO_FILE; } /** @@ -84,7 +91,7 @@ public function isUploaded() */ public function receive() { - if (!$this->isUploaded()) { + if (is_object($this->handler) === false) { return false; }