1. Go to this page and download the library: Download netipar/laravel-chunky library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
#[On('chunky-upload-completed')]
public function handleUpload(array $data): void
{
// Default payload: $data['uploadId'], $data['fileName'], $data['fileSize']
//
// Set `chunky.broadcasting.expose_internal_paths = true` in
// config/chunky.php to additionally receive $data['finalPath'] and
// $data['disk']. By default they're stripped to avoid leaking
// server-internal paths to the browser.
}
namespace NETipar\Chunky\Authorization;
interface Authorizer
{
public function canAccessUpload(?Authenticatable $user, UploadMetadata $upload): bool;
public function canAccessBatch(?Authenticatable $user, BatchMetadata $batch): bool;
}
use NETipar\Chunky\Authorization\Authorizer;
use NETipar\Chunky\Authorization\DefaultAuthorizer;
$this->app->singleton(Authorizer::class, function ($app) {
return new class extends DefaultAuthorizer
{
public function canAccessUpload(?Authenticatable $user, UploadMetadata $upload): bool
{
// Admins access everything
if ($user?->is_admin) {
return true;
}
// Teammates share access
if ($upload->userId !== null) {
$owner = User::find($upload->userId);
if ($owner && $user?->team_id === $owner->team_id) {
return true;
}
}
return parent::canAccessUpload($user, $upload);
}
};
});
use NETipar\Chunky\Facades\Chunky;
public function boot(): void
{
Chunky::register(ProfileAvatarContext::class);
}
use NETipar\Chunky\Facades\Chunky;
public function boot(): void
{
Chunky::context(
'documents',
rules: fn () => [
'file_size' => ['max:104857600'], // 100MB
'mime_type' => ['in:application/pdf,application/zip'],
],
);
}
use NETipar\Chunky\Events\UploadCompleted;
use NETipar\Chunky\Events\ChunkUploaded;
use NETipar\Chunky\Events\FileAssembled;
protected $listen = [
UploadCompleted::class => [
\App\Listeners\ProcessUploadedFile::class,
\App\Listeners\NotifyUserAboutUpload::class,
],
ChunkUploaded::class => [
\App\Listeners\TrackUploadProgress::class,
],
];
namespace App\Listeners;
use NETipar\Chunky\Events\UploadCompleted;
use Illuminate\Support\Facades\Storage;
class ProcessUploadedFile
{
public function handle(UploadCompleted $event): void
{
// Full UploadMetadata DTO available via $event->upload
$upload = $event->upload;
Storage::disk($upload->disk)->move(
$upload->finalPath,
"documents/{$upload->uploadId}.zip",
);
// Shorthand properties also available for convenience:
// $event->uploadId, $event->finalPath, $event->disk, $event->metadata
}
}
use Illuminate\Support\Facades\Broadcast;
Broadcast::channel('chunky.uploads.{uploadId}', function ($user, $uploadId) {
// Verify the user owns this upload
return true;
});
Broadcast::channel('chunky.batches.{batchId}', function ($user, $batchId) {
return true;
});