groovili / rest-uploader-bundle
A Symfony bundle to handle file upload and management for REST API
Installs: 203
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 2
Forks: 2
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=7.1
- doctrine/doctrine-bundle: ^1.6
- doctrine/orm: ^2.5
- symfony/console: ^2.8|^3.0
- symfony/framework-bundle: ^3.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.1
- phpunit/phpunit: ^4.8|^5.0
- symfony/phpunit-bridge: ~3.2
- symfony/yaml: ^2.8|^3.0
Suggests
- friendsofsymfony/rest-bundle: API bundle to handle REST easier
- jms/serializer-bundle: Serialaizer
- lexik/jwt-authentication-bundle: Json web token for authentication
- nelmio/api-doc-bundle: Automatic documentation generator
This package is not auto-updated.
Last update: 2025-02-02 06:57:05 UTC
README
A Symfony bundle for file upload and management for REST API.
Provides File
entity, rest_uploader.manager
,rest_uploader.validator
services, RestFileType
and list of events to subscribe:
rest_uploader.file.preUpload
rest_uploader.file.postUpload
rest_uploader.file.preDownload
rest_uploader.file.preDelete
rest_uploader.file.preGetPath
Examples can be found in examples section below.
Installation
Require the groovili/rest-uploader-bundle
package in your composer.json and update your dependencies.
composer require groovili/rest-uploader-bundle
Add the RestUploaderBundle to your application's kernel:
public function registerBundles() { $bundles = [ // ... new Groovili\RestUploaderBundle\RestUploaderBundle(), // ... ]; // ... }
Please notice that csrf_protection
should be false
to use RestFileType.
Configuration
The public_dir
and private_dir
are path strings from app folder.
If not exist, would be added automatically. This parameters should be only strings.
allowed_extensions
is array of strings with allowed file extensions.
file_max_size
is integer number in MB, which would be maximum limit.
Configuration which provided below is default for this bundle:
rest_uploader: public_dir: '../web/files' private_dir: '../private' allowed_extensions: [] file_max_size: 25
Examples
RestFileType for file upload
<?php /** @var @UploadedFile $upload */ $upload = $request->files->get('file'); if (isset($upload)) { $form = $this->createFormBuilder() ->add('file', RestFileType::class, [ 'allow_delete' => true, 'validate_extensions' => true, 'validate_size' => true, 'private' => false, ]) ->getForm(); $form->handleRequest($request); $clearMissing = $request->getMethod() != 'PATCH'; $form->submit(['file' => $upload], $clearMissing); $data = $form->getData(); if (isset($data['file'])) { /** @var File $file */ $file = $data['file']; $em = $this->getDoctrine()->getManager(); $em->persist($file); $em->flush(); } }
RestFileType submit of existing entity
<?php /** * $file = ['file' => ['id' => 8]] */ $file = json_decode($request->getContent(), true); $form = $this->createFormBuilder() ->add('file', RestFileType::class, [ 'allow_delete' => true, 'validate_extensions' => true, 'validate_size' => true, 'private' => false, ]) ->getForm(); $form->handleRequest($request); $clearMissing = $request->getMethod() != 'PATCH'; $form->submit($file , $clearMissing);
RestFileType delete of existing entity
<?php /** * $file = ['file' => ['id' => 8, 'delete' => true]] */ $file = json_decode($request->getContent(), true); $form = $this->createFormBuilder() ->add('file', RestFileType::class, [ 'allow_delete' => true, 'validate_extensions' => true, 'validate_size' => true, 'private' => false, ]) ->getForm(); $form->handleRequest($request); $clearMissing = $request->getMethod() != 'PATCH'; $form->submit($file , $clearMissing); $em = $this->getDoctrine()->getManager(); $em->flush();
Upload and validate file via service
<?php /** @var @UploadedFile $upload */ $upload = $request->files->get('file'); if (isset($upload)) { $validator = $this->container->get('rest_uploader.validator'); $uploader = $this->container->get('rest_uploader.manager'); if ($validator->isExtensionAllowed($upload) && $validator->isSizeValid($upload)){ /** @var File $file */ $file = $uploader->upload($upload, false); } }
Add bundle routing to your routing.yml
file: resource: '@RestUploaderBundle/Resources/config/routing.yml' type: yaml