2017 © Pedro Peláez
 

yii2-extension yii2-file-kit

Yii2 file upload and storage kit

image

weison-tech/yii2-file-kit

Yii2 file upload and storage kit

  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 82 Forks
  • 0 Open issues
  • 19 Versions
  • 0 % Grown

The README.md

Packagist Dependency Status, (*1)

This kit is designed to automate routine processes of uploading files, their saving and storage. It includes: - File upload widget (based on Blueimp File Upload) - Component for storing files (built on top of flysystem) - Actions to download, delete, and view (download) files - Behavior for saving files in the model and delete files when you delete a model, (*2)

Here you can see list of available filesystem adapters, (*3)

Demo

Since file kit is a part of yii2-starter-kit it's demo can be found in starter kit demo here., (*4)

Installation

The preferred way to install this extension is through composer., (*5)

Either run, (*6)

php composer.phar require trntv/yii2-file-kit

or add, (*7)

"trntv/yii2-file-kit": "@stable"

to the require section of your composer.json file., (*8)

File Storage

To work with the File Kit you need to configure FileStorage first. This component is a layer of abstraction over the filesystem - Its main task to take on the generation of a unique name for each file and trigger corresponding events., (*9)

'fileStorage'=>[
    'class' => 'trntv\filekit\Storage',
    'baseUrl' => '@web/uploads'
    'filesystem'=> ...
        // OR
    'filesystemComponent' => ...    
],

There are several ways to configure trntv\filekit\Storage to work with flysystem., (*10)

Using Closure

'fileStorage'=>[
    ...
    'filesystem'=> function() {
        $adapter = new \League\Flysystem\Adapter\Local('some/path/to/storage');
        return new League\Flysystem\Filesystem($adapter);
    }
]

Using filesystem builder

  • Create a builder class that implements trntv\filekit\filesystem\FilesystemBuilderInterface and implement method build which returns filesystem object. See examples/
  • Add to your configuration:
'fileStorage'=>[
    ...
    'filesystem'=> [
        'class' => 'app\components\FilesystemBuilder',
        'path' => '@webroot/uploads'
        ...
    ]
]

Read more about flysystem at http://flysystem.thephpleague.com/, (*11)

Using third-party extensions

  • Create filesystem component (example uses creocoder/yii2-flysystem)
'components' => [
    ...
    'fs' => [
        'class' => 'creocoder\flysystem\LocalFilesystem',
        'path' => '@webroot/files'
    ],
    ...
]
  • Set filesystem component name in storage configuration:
'components' => [
    ...
    'fileStorage'=>[
        'filesystemComponent'=> 'fs'
    ],
    ...
]

Actions

File Kit contains several Actions to work with uploads., (*12)

Upload Action

Designed to save the file uploaded by the widget, (*13)

public function actions(){
    return [
           'upload'=>[
               'class'=>'trntv\filekit\actions\UploadAction',
               //'deleteRoute' => 'my-custom-delete', // my custom delete action for deleting just uploaded files(not yet saved)
               //'fileStorage' => 'myfileStorage', // my custom fileStorage from configuration
               'multiple' => true,
               'disableCsrf' => true,
               'responseFormat' => Response::FORMAT_JSON,
               'responsePathParam' => 'path',
               'responseBaseUrlParam' => 'base_url',
               'responseUrlParam' => 'url',
               'responseDeleteUrlParam' => 'delete_url',
               'responseMimeTypeParam' => 'type',
               'responseNameParam' => 'name',
               'responseSizeParam' => 'size',
               'deleteRoute' => 'delete',
               'fileStorage' => 'fileStorage', // Yii::$app->get('fileStorage')
               'fileStorageParam' => 'fileStorage', // ?fileStorage=someStorageComponent
               'sessionKey' => '_uploadedFiles',
               'allowChangeFilestorage' => false,
               'validationRules' => [
                    ...
               ],
               'on afterSave' => function($event) {
                    /* @var $file \League\Flysystem\File */
                    $file = $event->file
                    // do something (resize, add watermark etc)
               }
           ]
       ];
}

See additional settings in the corresponding class, (*14)

Delete Action

public function actions(){
    return [
       'delete'=>[
           'class'=>'trntv\filekit\actions\DeleteAction',
           //'fileStorage' => 'fileStorageMy', // my custom fileStorage from configuration(such as in the upload action)
       ]
    ];
}

See additional settings in the corresponding class, (*15)

View (Download) Action

public function actions(){
    return [
       'view'=>[
           'class'=>'trntv\filekit\actions\ViewAction',
       ]
    ];
}

See additional settings in the corresponding class, (*16)

Upload Widget

Standalone usage, (*17)

echo \trntv\filekit\widget\Upload::widget([
    'model' => $model,
    'attribute' => 'files',
    'url' => ['upload'],
    'sortable' => true,
    'maxFileSize' => 10 * 1024 * 1024, // 10Mb
    'minFileSize' => 1 * 1024 * 1024, // 1Mb
    'maxNumberOfFiles' => 3 // default 1,
    'acceptFileTypes' => new JsExpression('/(\.|\/)(gif|jpe?g|png)$/i'),
    'showPreviewFilename' => false,
    'clientOptions' => [ ...other blueimp options... ]
]);

With ActiveForm, (*18)

echo $form->field($model, 'files')->widget(
    '\trntv\filekit\widget\Upload',
    [
        'url' => ['upload'],
        'sortable' => true,
        'maxFileSize' => 10 * 1024 * 1024, // 10 MiB
        'maxNumberOfFiles' => 3,
        'clientOptions' => [ ...other blueimp options... ]
    ]
);

Upload Widget events

Upload widget trigger some of built-in blueimp events: - start - fail - done - always, (*19)

You can use them directly or add your custom handlers in options:, (*20)

'clientOptions' => [ 
    'start' => new JsExpression('function(e, data) { ... do something ... }'),
    'done' => new JsExpression('function(e, data) { ... do something ... }'),
    'fail' => new JsExpression('function(e, data) { ... do something ... }'),
    'always' => new JsExpression('function(e, data) { ... do something ... }'),
 ]

UploadBehavior

This behavior is designed to save uploaded files in the corresponding relation., (*21)

Somewhere in model:, (*22)

For multiple files, (*23)

 public function behaviors()
 {
    return [
        'file' => [
            'class' => 'trntv\filekit\behaviors\UploadBehavior',
            'filesStorage' => 'myfileStorage', // my custom fileStorage from configuration(for properly remove the file from disk)
            'multiple' => true,
            'attribute' => 'files',
            'uploadRelation' => 'uploadedFiles',
            'pathAttribute' => 'path',
            'baseUrlAttribute' => 'base_url',
            'typeAttribute' => 'type',
            'sizeAttribute' => 'size',
            'nameAttribute' => 'name',
            'orderAttribute' => 'order'
        ],
    ];
 }

For single file upload, (*24)

 public function behaviors()
 {
     return [
          'file' => [
              'class' => 'trntv\filekit\behaviors\UploadBehavior',
              'filesStorage' => 'fileStorageMy', // my custom fileStorage from configuration(for properly remove the file from disk)
              'attribute' => 'file',
              'pathAttribute' => 'path',
              'baseUrlAttribute' => 'base_url',
               ...
          ],
      ];
 }

See additional settings in the corresponding class., (*25)

Validation

There are two ways you can perform validation over uploads. On the client side validation is performed by Blueimp File Upload. Here is documentation about available options., (*26)

On the server side validation is performed by [[yii\web\UploadAction]], where you can configure validation rules for [[yii\base\DynamicModel]] that will be used in validation process, (*27)

Tips

Adding watermark

Install intervention/image library, (*28)

composer require intervention/image

Edit your upload actions as so, (*29)

public function actions(){
    return [
           'upload'=>[
               'class'=>'trntv\filekit\actions\UploadAction',
               ...
               'on afterSave' => function($event) {
                    /* @var $file \League\Flysystem\File */
                    $file = $event->file;

                    // create new Intervention Image
                    $img = Intervention\Image\ImageManager::make($file->read());

                    // insert watermark at bottom-right corner with 10px offset
                    $img->insert('public/watermark.png', 'bottom-right', 10, 10);

                    // save image
                    $file->put($img->encode());
               }
               ...
           ]
       ];
}

The Versions

14/01 2017

dev-master

9999999-dev

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

12/01 2017

1.2.0

1.2.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

11/03 2016

1.1.8

1.1.8.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

14/12 2015

1.1.7

1.1.7.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

20/11 2015

1.1.6

1.1.6.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

29/09 2015

1.1.5

1.1.5.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

12/09 2015

1.1.4

1.1.4.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

11/09 2015

1.1.3

1.1.3.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

04/09 2015

1.1.2

1.1.2.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

01/06 2015

1.1.1

1.1.1.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

25/05 2015

1.1.0

1.1.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

14/05 2015

1.0.3

1.0.3.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

26/04 2015

1.0.2

1.0.2.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

07/04 2015

1.0.1

1.0.1.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

24/03 2015

1.0.0

1.0.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

13/01 2015

0.4

0.4.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

by Eugine Terentev

24/09 2014

0.3

0.3.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

by Eugine Terentev

04/09 2014

0.1

0.1.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

by Eugine Terentev

04/09 2014

0.2

0.2.0.0

Yii2 file upload and storage kit

  Sources   Download

BSD-3-Clause

The Requires

 

by Eugine Terentev