2017 © Pedro Peláez
 

symfony-bundle rest-uploader-bundle

A Symfony bundle to handle file upload and management for REST API

image

groovili/rest-uploader-bundle

A Symfony bundle to handle file upload and management for REST API

  • Sunday, April 29, 2018
  • by groovili
  • Repository
  • 2 Watchers
  • 8 Stars
  • 166 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 14 % Grown

The README.md

RestUploaderBundle

Latest Stable Version Total Downloads License, (*1)

A Symfony bundle for file upload and management for REST API., (*2)

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, (*3)

Examples can be found in examples section below., (*4)

Installation

Require the groovili/rest-uploader-bundle package in your composer.json and update your dependencies., (*5)

composer require groovili/rest-uploader-bundle

Add the RestUploaderBundle to your application's kernel:, (*6)

    public function registerBundles()
    {
        $bundles = [
            // ...
            new Groovili\RestUploaderBundle\RestUploaderBundle(),
            // ...
        ];
        // ...
    }

Please notice that csrf_protection should be false to use RestFileType., (*7)

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., (*8)

allowed_extensions is array of strings with allowed file extensions., (*9)

file_max_size is integer number in MB, which would be maximum limit., (*10)

Configuration which provided below is default for this bundle:, (*11)

    rest_uploader:
        public_dir: '../web/files'
        private_dir: '../private'
        allowed_extensions: []
        file_max_size: 25

Examples

RestFileType for file upload, (*12)

<?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, (*13)

<?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, (*14)

<?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, (*15)

<?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, (*16)

file:
    resource: '@RestUploaderBundle/Resources/config/routing.yml'
    type: yaml

The Versions