Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/groovili/restuploaderbundle
A Symfony bundle to handle file upload and management for REST API.
https://github.com/groovili/restuploaderbundle
file-upload php php7 rest-api symfony-bundle symfony3
Last synced: 3 months ago
JSON representation
A Symfony bundle to handle file upload and management for REST API.
- Host: GitHub
- URL: https://github.com/groovili/restuploaderbundle
- Owner: groovili
- License: mit
- Created: 2017-12-19T13:28:04.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-11-17T13:09:17.000Z (about 5 years ago)
- Last Synced: 2024-09-30T16:02:57.556Z (3 months ago)
- Topics: file-upload, php, php7, rest-api, symfony-bundle, symfony3
- Language: PHP
- Homepage:
- Size: 73.2 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RestUploaderBundle
[![Latest Stable Version](https://poser.pugx.org/groovili/rest-uploader-bundle/v/stable)](https://packagist.org/packages/groovili/rest-uploader-bundle)
[![Total Downloads](https://poser.pugx.org/groovili/rest-uploader-bundle/downloads)](https://packagist.org/packages/groovili/rest-uploader-bundle)
[![License](https://poser.pugx.org/groovili/rest-uploader-bundle/license)](https://packagist.org/packages/groovili/rest-uploader-bundle)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**:
1. `rest_uploader.file.preUpload`
2. `rest_uploader.file.postUpload`
3. `rest_uploader.file.preDownload`
4. `rest_uploader.file.preDelete`
5. `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:
```php
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:
```yaml
rest_uploader:
public_dir: '../web/files'
private_dir: '../private'
allowed_extensions: []
file_max_size: 25
```## Examples
RestFileType for file upload
```php
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
['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
['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
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**
```yaml
file:
resource: '@RestUploaderBundle/Resources/config/routing.yml'
type: yaml
```