Installation
composer require oneup/uploader-bundle
Add to config/bundles.php:
return [
// ...
Oneup\UploaderBundle\OneupUploaderBundle::class => ['all' => true],
];
Configure Storage
Edit config/packages/oneup_uploader.yaml:
oneup_uploader:
mappings:
my_uploads:
frontend: dropzone # or plupload, jquery_file_upload, etc.
storage: gaufrette
gaufrette:
service: my_gaufrette_adapter
orphanage: true
Register a Gaufrette adapter (e.g., services.yaml):
services:
my_gaufrette_adapter:
class: Gaufrette\Filesystem
factory: ['Gaufrette\FilesystemMap', 'build']
arguments: ['%kernel.project_dir%/public/uploads']
First Upload Route
// src/Controller/UploadController.php
use Oneup\UploaderBundle\Handler\HandlerRegistry;
use Symfony\Component\HttpFoundation\Request;
class UploadController extends AbstractController
{
public function upload(Request $request, HandlerRegistry $handlerRegistry)
{
$handler = $handlerRegistry->getHandler('my_uploads');
$handler->upload($request);
return $this->json(['success' => true]);
}
}
Route it in config/routes.yaml:
upload:
path: /upload
controller: App\Controller\UploadController::upload
methods: POST
Frontend Integration Use the Dropzone example or another supported library. Key config:
Dropzone.options.myDropzone = {
url: '/upload',
paramName: 'file', // Default, but ensure it matches backend
maxFilesize: 5, // MB
autoProcessQueue: true,
};
Handler-Based Processing
HandlerRegistry to dynamically resolve handlers by mapping name (e.g., 'my_uploads').$handler->addMiddleware(new MyCustomMiddleware());
Storage Abstraction
gaufrette:
service: my_s3_adapter
services:
my_s3_adapter:
class: Gaufrette\Filesystem
factory: ['Gaufrette\Adapter\AwsS3', 'fromPath']
arguments: ['my-bucket']
orphanage: true in config.Chunked Uploads
chunking: true) and backend:
oneup_uploader:
mappings:
my_uploads:
chunk_size: 5MB # Adjust based on server limits
HandlerRegistry (automatically managed by bundle).Progress Tracking
session.upload_progress (PHP ≥5.4) or implement custom progress middleware:
$handler->addMiddleware(new ProgressTrackingMiddleware());
Metadata Handling
$file = $handler->getFile();
$metadata = $file->getMetadata();
Validation
Validator or custom middleware:
$handler->addMiddleware(new FileTypeMiddleware(['jpg', 'png']));
oneup_uploader:
mappings:
my_uploads:
allowed_mime_types: ['image/jpeg', 'image/png']
Dynamic Mappings
$handlerRegistry->addMapping('user_'.$userId, [
'frontend' => 'dropzone',
'storage' => 'gaufrette',
// ...
]);
Event Listeners
oneup_uploader.pre_upload):
// src/EventListener/UploadListener.php
public function onPreUpload(PreUploadEvent $event) {
$file = $event->getFile();
// Modify file metadata or path
}
Register in services.yaml:
services:
App\EventListener\UploadListener:
tags:
- { name: kernel.event_listener, event: oneup_uploader.pre_upload }
Testing
HandlerRegistry and File objects:
$handler = $this->createMock(HandlerInterface::class);
$handler->method('upload')->willReturn(true);
$registry = $this->createMock(HandlerRegistry::class);
$registry->method('getHandler')->willReturn($handler);
Frontend-Backend Mismatch
paramName (e.g., file) doesn’t match frontend config.paramName: 'file') and backend (default is 'file').Chunked Uploads and Orphanage
orphanage_lifetime in config (default: 1 day):
oneup_uploader:
orphanage_lifetime: 3600 # 1 hour
Gaufrette/Flysystem Permissions
chown -R www-data:www-data /path/to/uploads).Session Upload Progress
session.upload_progress requires PHP ≥5.4 and session.upload_progress.enabled=1 in php.ini.Large File Handling
upload_max_filesize or post_max_size limits exceeded.php.ini and configure chunking:
oneup_uploader:
mappings:
my_uploads:
chunk_size: 2MB
Log Upload Events
$handler->addMiddleware(new LoggingMiddleware());
class LoggingMiddleware implements MiddlewareInterface {
public function handle(File $file, callable $next) {
\Log::debug('Upload started', ['file' => $file->getClientOriginalName()]);
return $next($file);
}
}
Check Orphanage
$orphanage = $handlerRegistry->getOrphanage('my_uploads');
$orphans = $orphanage->list();
Validate Frontend Requests
file field).error events).Custom Frontend
Oneup\UploaderBundle\Handler\Frontend\FrontendInterface for unsupported libraries.Storage Adapters
class CustomAdapter extends Gaufrette\Adapter\Local {
public function write($path, $content) {
// Custom logic (e.g., symlink handling)
return parent::write($path, $content);
}
}
Middleware Pipeline
$handler->addMiddleware(new class implements MiddlewareInterface {
public function handle(File $file, callable $next) {
// Pre-upload logic
$file = $next($file);
// Post-upload logic
return $file;
}
});
File Metadata
How can I help you explore Laravel packages today?