Skip to content

Custom handler

Martin Kluska edited this page Sep 11, 2018 · 1 revision

Handlers

Objects that will handle parsing request data for chunk detection for every provider.

There are multiple ways how to use custom handler:

1. Set custom handlers in config (in chunk-upload.php, handlers.custom)

If you want to support multiple handlers at one time you can use custom key. If you want to detection super fast, use override key.

'handlers' => [
    // A list of handlers/providers that will be appended to existing list of handlers
    'custom' => [
    	App\MyCustomHandler::class
    ],
    // Overrides the list of handlers - use only what you really want
    'override' => [],
],

2. Call HandlerFactory::register(MyClass::class) before using FileReceiver in depedency injection or by using HandlerFactory::classFromRequest

class FileController extends Controller {
	constructor () {
		FileFactory::register(MyCustomHandler::class)
	}
	
	public function uploadFile(FileReceiver $receiver)
	{
		...
	}
	
	public function upload(Request $request) {
    // create the file receiver
    $receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request));
    }
}

3. Initalize FileReceiver with a given handler class (third parameter)

class FileController extends Controller {
	public function upload(Request $request) {
    // create the file receiver
    $receiver = new FileReceiver("file", $request, MyCustomHandler::classFromRequest($request));
    }
}

Using own implementation

See the Contribution section in Readme

AbstractHandler

Use AbstractHandler for type hint or use a specific handler to se additional methods.

ContentRangeUploadHandler

  • supported by blueimp-file-upload
  • uses the Content-range header with the bytes range

Additional methods

  • getBytesStart() - returns the starting bytes for current request
  • getBytesEnd() - returns the ending bytes for current request
  • getBytesTotal() - returns the total bytes for the file

ChunksInRequestUploadHandler

  • Supported by plupload
  • uses the chunks numbers from the request

ResumableJSUploadHandler

  • Supported by resumable.js
  • uses the chunks numbers from the request

DropZoneUploadHandler

  • Supported by DropZone
  • uses the chunks numbers from the request

Automatic handler (HandlerFactory)

You can use the automatic detection of the correct handler (provider) by using the HandlerFactory::classFromRequest as a third parameter when constructing the FileReceiver.

// Exception is thrown if file upload is invalid (size limit, etc)
$receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request));

Fallback class

The default fallback class is stored in the HandlerFactory (default SingleUploadHandler::class).

You can change it globally by calling

HandlerFactory::setFallbackHandler(CustomHandler::class)

or pass as second parameter when using

HandlerFactory::classFromRequest($request, CustomHandler::class)