aboutcoders/file-distribution-bundle
Symfony bundle providing database-backed file management and distribution. Define filesystems in config or persist via Doctrine ORM, then store and transfer files across local, FTP, or CDN targets. Built on the AbcFileDistribution library and unit tested.
Installation
composer require aboutcoders/file-distribution-bundle:~1.1
Register the bundle in config/bundles.php (Symfony Flex) or AppKernel.php (legacy):
return [
// ...
Abc\Bundle\FileDistributionBundle\AbcFileDistributionBundle::class => ['all' => true],
];
Basic Configuration
Define a filesystem in config/packages/abc_file_distribution.yaml:
abc_file_distribution:
db_driver: orm
filesystems:
local:
type: local
path: '%kernel.project_dir%/public/uploads'
s3:
type: s3
bucket: my-bucket
key: '%env(AWS_KEY)%'
secret: '%env(AWS_SECRET)%'
First Use Case: Uploading a File
Inject the FileDistributionManager service and use it to upload:
use Abc\Bundle\FileDistributionBundle\Manager\FileDistributionManager;
class MyController {
public function __construct(private FileDistributionManager $fileManager) {}
public function upload(Request $request) {
$file = $request->file('file');
$path = $this->fileManager->upload($file, 'local', 'user_uploads');
return new Response("File uploaded to: $path");
}
}
Filesystem Abstraction Use the bundle to switch between storage backends (local, S3, FTP) without changing business logic:
// Upload to S3
$this->fileManager->upload($file, 's3', 'profile_pictures');
// Upload to local filesystem (fallback)
$this->fileManager->upload($file, 'local', 'backups');
File Distribution Copy files between filesystems (e.g., local → CDN):
$this->fileManager->distribute('local', 'cdn', 'user_uploads/file.jpg');
Symfony Integration
abc_file_distribution.twig extension to generate URLs:
{{ abc_file_distribution_url('cdn', 'user_uploads/file.jpg') }}
Abc\Bundle\FileDistributionBundle\Form\Type\FileType for custom file handling.Event-Driven Workflows Listen to file events (e.g., post-upload processing):
// config/services.yaml
services:
App\EventListener\FileUploadListener:
tags:
- { name: 'kernel.event_listener', event: 'abc_file_distribution.upload', method: 'onUpload' }
AWS_KEY).local, s3, cdn) across the app.abc_file_distribution:
default_filesystem: local
Doctrine ORM Dependency
db_driver is correctly set and the underlying AbcFileDistribution library supports your ODM.AbcFileDistribution library’s documentation for supported persistence layers.Filesystem Permissions
chmod -R 775 public/uploads).umask in your deployment script to avoid permission issues.S3/CDN Configuration
Abc\Bundle\FileDistributionBundle to log errors:
monolog:
handlers:
abc_file_distribution:
type: stream
path: "%kernel.logs_dir%/file_distribution.log"
level: debug
File Path Handling
/var/www/uploads) will break portability.path key in configuration for local filesystems and let the bundle handle resolution.if (!$this->fileManager->hasFilesystem('nonexistent')) {
throw new \RuntimeException('Filesystem not configured');
}
validate() method to check file constraints before upload:
$this->fileManager->validate($file, [
'maxSize' => '10M',
'mimeTypes' => ['image/jpeg', 'image/png'],
]);
Custom Filesystem Types
Extend the Abc\FileDistribution\Filesystem\FilesystemInterface to support new backends (e.g., Google Cloud Storage):
class GcsFilesystem implements FilesystemInterface {
// Implement required methods
}
Register it as a service and configure it in abc_file_distribution.yaml.
Pre/Post-Processing Use event subscribers to modify files before/after operations:
// Example: Resize images on upload
public function onUpload(FileUploadEvent $event) {
if ($event->getFilesystem()->getName() === 'local') {
$event->getFile()->resize(new \Imagick(), 800, 600);
}
}
Custom Metadata
Extend the File entity to store additional metadata (e.g., alt_text for images):
// src/Entity/File.php
/**
* @ORM\Column(type="string", nullable=true)
*/
private $altText;
Update the bundle’s mapping configuration to include the new field.
How can I help you explore Laravel packages today?