slexx / yii2-file-kit

Yii2 文件上传和存储工具包

安装: 4

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 0

开放问题: 0

语言:JavaScript

类型:yii2-extension

v1.0.0 2020-02-27 11:02 UTC

This package is auto-updated.

Last update: 2024-09-27 21:19:33 UTC


README

扩展仍在积极开发中 ;) 存储

'fileStorage'=>[
    'class'=>'trntv\filekit\storage\FileStorage',
    'repositories'=>[
        'uploads'=>[
            'class'=>'trntv\filekit\storage\repository\FilesystemRepository',
            'basePath'=>'@webroot/uploads',
            'baseUrl'=>'@web/uploads'
        ],
        'other-path'=>[
            'class'=>'trntv\filekit\storage\repository\FilesystemRepository',
            'basePath'=>'@webroot/other',
            'baseUrl'=>'@web/other',
            'createDbRecord'=>false
        ]
    ],
],

不要忘记应用包含的迁移 - m140805_084737_file_storage_item.php

\Yii::$app->fileStorage->getRepository('uploads')->save(UploadedFile::getInstanceByName('image')); \Yii::$app->fileStorage->saveAll(UploadedFile::getInstancesByName('files')); \Yii::$app->fileStorage->save('http://external_host.com/file.pdf', 'awesome documents', 'other-path');

小部件

echo \trntv\filekit\widget\Upload::widget([
    'model'=>$model,
    'attribute'=>'files',
    'url'=>['upload'],
    'sortable'=>true,
    'fileuploadOptions'=>[
        'maxFileSize'=>10000000, // 10 MiB
        'maxNumberOfFiles'=>3
    ]
])

行为

public function actions(){
   return [
           'upload'=>[
               'class'=>'trntv\filekit\actions\UploadAction',
               'responseUrlParam'=>'file-url',
               'fileProcessing'=>function($file, $uploadAction){
                    // do something (resize, add watermark etc)
               }
           ]
       ];
   }

行为

 public function behaviors()
 {
     return [
          'file' => [
              'class' => 'trntv\filekit\behaviors\UploadBehavior',
              'uploadAttribute' => 'file',
              'resultAttribute' => 'path',
              'fileCategory' => 'products',
              'fileRepository' => 'uploads',
              'fileProcessing'=>function($file, $uploadAction){
                  // resize etc
              }
          ],
      ];
 }

路径助手

$path = new \trntv\filekit\base\Path(['path'=>'/var/www/images/product.jpg'])
echo $path->filename; // product.jpg
$path->filename = 'service.jpg';
echo $path; // /var/www/images/service.jpg
$path->addFilenamePrefix('_thumb');
echo $path->filename; // service_thumb.jpg
echo $path; // /var/www/images/service_thumb.jpg

URL 助手

$url = new \trntv\filekit\base\Url(['url'=>'http://example.com/1/test.php'])
echo $url->host; // example.com
echo $url->path->filename; // test.php;
$url->port = 88;
$url->path->filename = 'product.jpg';
echo $url; // http://example.com:88/1/product.jpg
$url->path->addFilenamePrefix('_thumb');
echo $url; // http://example.com:88/1/product_thumb.jpg

待办事项

  • MongoDB 存储库
  • 云存储库