heryfitiavana / rcu-laravel
Resumable chunk upload for Laravel
Requires
- illuminate/http: ^11.13
- illuminate/routing: ^11.13
- illuminate/support: ^11.13
Requires (Dev)
- orchestra/testbench: ^9.1
- phpunit/phpunit: ^11.2
README
Resumable chunk upload for Laravel.
Installation
Via composer :
composer require heryfitiavana/rcu-laravel
Usage
CSRF
Add the csrf token to the Uploader in frontend
// get csrf token from meta tag : <meta name="csrf-token" content="{{ csrf_token() }}"> const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute("content"); new Uploader() .setHeaders({ "X-CSRF-TOKEN": csrfToken, }) /// ... others
Defining Routes
Add the following routes to your routes/web.php
file:
use Heryfitiavana\RCU\Controllers\UploadController; Route::get('/uploadStatus', [UploadController::class, 'uploadStatus']); Route::post('/upload', [UploadController::class, 'upload']);
Default RCU configuration : RCUConfig
Custom RCU configuration
Custom RCU configuration
You can customize the package's behavior by defining a custom configuration array. Here's an example:
$customConfig = [ "store" => new JsonStoreProvider('rcu/uploads.json'), "tmpDir" => "rcu/tmp", "outputDir" => "rcu/output", "onCompleted" => function ($data) { }, ];
Use the configuration
Go to app/Providers/AppServiceProvider.php
and add the following code
use Illuminate\Support\ServiceProvider; use Heryfitiavana\RCU\Controllers\RCUControllerFactory; use Heryfitiavana\RCU\Controllers\UploadController; use Heryfitiavana\RCU\StoreProviders\JsonStoreProvider; class AppServiceProvider extends ServiceProvider { public function register() { // other ... $this->app->singleton(UploadController::class, function ($app) { $customConfig = [ "store" => new JsonStoreProvider('rcu/uploads.json'), "tmpDir" => "rcu/tmp", "outputDir" => "rcu/output", "onCompleted" => function ($data) { }, ]; return RCUControllerFactory::createController($customConfig); }); } public function boot() { } }
API
RCUConfig
[ "store" => new JsonStoreProvider('rcu/uploads.json'), "tmpDir" => "rcu/tmp", "outputDir" => "rcu/output", "onCompleted" => function ($data) { }, ]
store
- Type:
StoreProviderInterface
- Default:
JsonStoreProvider
The store
parameter is used to store information about the upload, such as the number of the last uploaded chunk, the total number of chunks, etc. The default store is JSON, but you can implement your own by implementing the StoreProviderInterface.
tmpDir
- Type:
string
- Default:
rcu/tmp
Directory to save all binary chunks.
outputDir
- Type:
string
- Default:
rcu/output
Directory to save the complete file.
onCompleted
- Type:
(data: ['outputFile' => string, 'fileId' => string]) => void
This callback function can be used to perform any additional actions or operations after the upload is completed, such as updating a database record or sending a notification.
outputFile
: Path of the uploaded file.fileId
: The ID of the file used to identify the upload. This is specified from frontend.
StoreProviderInterface
Upload = [ "id" => string, "chunkCount" => int, "lastUploadedChunkNumber": int, "chunkFilenames": string[], ]; interface StoreProviderInterface { public function getItem(String $id) : Upload | undefined; public function createItem(String $id, Int $chunkCount): Upload; public function updateItem(String $id, Upload $update) : Upload; public function removeItem(String $id) : void; }
License
The MIT License (MIT). Please see License File for more information.