Skip to content

Commit

Permalink
Raise exception if file upload has an error #17
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Kluska committed Jul 3, 2017
1 parent d96a185 commit e61ea99
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
12 changes: 8 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -245,7 +246,6 @@ public function upload(Request $request) {
* @throws UploadMissingFileException
*/
public function upload(Request $request) {

// Response for the files - completed and uncompleted
$files = [];

Expand All @@ -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));
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down
14 changes: 14 additions & 0 deletions src/Exceptions/UploadFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Pion\Laravel\ChunkUpload\Exceptions;

use Throwable;

class UploadFailedException extends \Exception
{
public function __construct($message, $code = 500, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}

}
11 changes: 9 additions & 2 deletions src/Receiver/FileReceiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use Illuminate\Http\Request;
use Pion\Laravel\ChunkUpload\Config\AbstractConfig;
use Pion\Laravel\ChunkUpload\Exceptions\UploadFailedException;
use Pion\Laravel\ChunkUpload\Handler\AbstractHandler;
use Pion\Laravel\ChunkUpload\Save\AbstractSave;
use Pion\Laravel\ChunkUpload\Save\ChunkSave;
Expand Down Expand Up @@ -50,6 +51,8 @@ class FileReceiver
* @param string $handlerClass the handler class name for detecting the file upload
* @param ChunkStorage|null $chunkStorage the chunk storage, on null will use the instance from app container
* @param AbstractConfig|null $config the config, on null will use the instance from app container
*
* @throws UploadFailedException
*/
public function __construct($fileIndexOrFile, Request $request, $handlerClass, $chunkStorage = null, $config = null)
{
Expand All @@ -59,6 +62,10 @@ public function __construct($fileIndexOrFile, Request $request, $handlerClass, $
$this->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);
}
}
Expand All @@ -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;
}

/**
Expand All @@ -84,7 +91,7 @@ public function isUploaded()
*/
public function receive()
{
if (!$this->isUploaded()) {
if (is_object($this->handler) === false) {
return false;
}

Expand Down

0 comments on commit e61ea99

Please sign in to comment.