2017 © Pedro Peláez
 

symfony-bundle filechunk-bundle

Javascript file upload widget and form type for Symfony

image

makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  • Tuesday, July 10, 2018
  • by pounard
  • Repository
  • 14 Watchers
  • 1 Stars
  • 107 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 5 % Grown

The README.md

File upload widget and file management for Symfony

This bundle provides a chunked file upload endpoint, that is tailored to be used with the https://github.com/makinacorpus/filechunk-front widget, but that may be used by any other component,, (*1)

The chunked file upload endpoint allows:, (*2)

  • very large file uploads,
  • resuming broken uploads,
  • bypassing most HTTP restrictions on file uploads (size, timeouts);
  • avoiding the PHP file upload mecanisms,
  • easy to use with a custom form type that can input and output either File instances or string URIs.

Known browsers to work with the external JavaScript widget:, (*3)

  • Chrome <= 49
  • Edge <= 13
  • IE <= 11
  • Firefox <= 33
  • And probably others, since it only uses a very small subset of the FileReader API.

Setup

Installation

composer require makinacorpus/filechunk-bundle

Current version does not carry the associated JavaScript widget, you must install it from: https://github.com/makinacorpus/filechunk-front, (*4)

Optionnally, if you are working in a Drupal 7 context, you may just install the following module: https://github.com/makinacorpus/drupal-filechunk instead of manually registering the JavaScript widget., (*5)

Basic configuration

Everything should be auto-configured if you follow the rest of this section., (*6)

Custom schemes configuration

Each custom scheme is tied to a custom folder, allowing you to store protocol relative URI in your database instead of absolute path, making the application portable and migrable easily., (*7)

Per default, the bundle offers three schemes:, (*8)

  • private:// for files that should not be accessible via the HTTPd which will default to %kernel.project_dir/var/private/%,
  • public:// for files that will be freely visible via the HTTPd, which will default to %kernel.project_dir/public/files/,
  • temporary:// for temporary files, which will default to PHP configured temporary folder,
  • upload:// for chunked file upload, which defaults to temporary://filechunk/
  • webroot:// for files that are in the public directory, will default to %kernel.project_dir/public,

Only the temporary one cannot be configured, all others can be set via the following .env file variables:, (*9)

FILE_PRIVATE_DIR="%kernel.project_dir%/var/private"
FILE_PUBLIC_DIR="%kernel.project_dir%/public/files"
FILE_UPLOAD_DIR="%kernel.project_dir%/var/tmp/upload"
FILE_WEBROOT_DIR="%kernel.project_dir%/public"

Chunked file upload widget configuration

Register the routing.yml file in your config/routes.yaml file:, (*10)

filechunk:
    resource: "@FilechunkBundle/Resources/config/routing.yml"
    prefix: /

And the associated form theme in your config/packages/twig.yaml file:, (*11)

twig:
    debug:            "%kernel.debug%"
    strict_variables: false
    form_themes:
        # ...
        - "FilechunkBundle:Form:fields.html.twig"

And it should probably work., (*12)

Usage

File manager API

Documentation will come soon., (*13)

File widget

Basic usage

Just use the MakinaCorpus\FilechunkBundle\Form\Type\FilechunkType form type in your own form builders., (*14)

Default values MUST be Symfony\Component\HttpFoundation\File\File instances, values returned will also be., (*15)

Validation

You may happily use the Symfony\Component\Validator\Constraints\File file constraint to validate you file:, (*16)

    $this
        ->createFormBuilder()
        ->add('photo', FilechunkType::class, [
            'label' => "Photo",
            'multiple' => false,
            'required' => true,
            'constraints' => [
                new Assert\NotBlank(),
                new Assert\File(['mimeTypes' => ['image/jpg', 'image/jpeg', 'image/png', 'application/pdf']]),
            ],
        ])

Caveat with multiple values

When using the multiple property set to true, you cannot just apply the Assert\File validator, if you do, since the widget will return an array of files the validator will fail. To get around this problem, here is a real life working example on how to tranform the previous example:, (*17)

    $this
        ->createFormBuilder()
        ->add('photo', FilechunkType::class, [
            'label' => "Photo",
            'multiple' => false,
            'required' => true,
            'constraints' => [
                new Assert\NotBlank(),
                new All([
                    'constraints' => [
                        new Assert\File(['mimeTypes' => ['image/jpg', 'image/jpeg', 'image/png', 'application/pdf']]),
                    ],
                ]),
            ],
       ])

You may find a better explaination of this there http://blog.arithm.com/2014/11/24/validating-multiple-files-in-symfony-2-5/, (*18)

Using validation group when working with multiple values

Same as upper, but you have validation groups too, you need to cascade the groups in the whole validator chain, this way:, (*19)

    $this
        ->createFormBuilder()
        ->add('photo', FilechunkType::class, [
            'label' => "Photo",
            'multiple' => false,
            'required' => true,
            'constraints' => [
                new Assert\NotBlank([
                    'groups' => ['some', 'group'],
                ]),
                new All([
                    'groups' => ['some', 'group'],
                    'constraints' => [
                        new Assert\File(
                            'groups' => ['some', 'group'],
                            'mimeTypes' => ['image/jpg', 'image/jpeg', 'image/png', 'application/pdf'],
                        ]),
                    ],
                ]),
            ],
       ])

Important notes

  • if you provide default values via the form data, and remove it via the UI on the HTML page, you have no way of fetching the removed file list, you must take care of this manually: this will be one of the first feature to be implemented in the future;, (*20)

  • uploaded files are NOT PHP uploaded files, but regular files in your temporary folder, you need to move them manually (you cannot use the move_uploaded_file() PHP function;, (*21)

  • You need recent browsers., (*22)

That's pretty much it, have fun!, (*23)

The Versions

10/07 2018

0.x-dev

0.9999999.9999999.9999999-dev http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2.0-or-later

The Requires

 

The Development Requires

10/07 2018

1.0.0-alpha9

1.0.0.0-alpha9 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2.0-or-later

The Requires

 

The Development Requires

10/07 2018

dev-master

9999999-dev http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2 GPL-2.0-or-later

The Requires

 

The Development Requires

10/07 2018

1.0.0-beta5

1.0.0.0-beta5 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2.0-or-later

The Requires

 

The Development Requires

24/05 2018

1.0.0-beta2

1.0.0.0-beta2 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2.0-or-later

The Requires

 

The Development Requires

23/05 2018

1.0.0-beta1

1.0.0.0-beta1 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2.0-or-later

The Requires

 

The Development Requires

23/03 2018

1.0.0-alpha8

1.0.0.0-alpha8 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2.0-or-later

The Requires

 

The Development Requires

23/03 2018

1.0.0-alpha7

1.0.0.0-alpha7 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2.0-or-later

The Requires

 

The Development Requires

21/02 2018

1.0.0-alpha6

1.0.0.0-alpha6 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2

The Requires

 

The Development Requires

13/12 2017

1.0.0-alpha5

1.0.0.0-alpha5 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2

The Requires

 

The Development Requires

05/09 2017

1.0.0-alpha4

1.0.0.0-alpha4 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2

The Requires

 

The Development Requires

02/08 2017

1.0.0-alpha3

1.0.0.0-alpha3 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2

The Requires

 

The Development Requires

25/07 2017

1.0.0-alpha2

1.0.0.0-alpha2 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2

The Requires

 

The Development Requires

01/03 2017

1.0.0-alpha1

1.0.0.0-alpha1 http://github.com/makinacorpus/filechunk-bundle

Javascript file upload widget and form type for Symfony

  Sources   Download

GPL-2

The Requires

 

The Development Requires