2017 © Pedro Peláez
 

cakephp-plugin contents-file

CakePHP ContentsFile

image

satthi/contents-file

CakePHP ContentsFile

  • Thursday, January 11, 2018
  • by satthi
  • Repository
  • 2 Watchers
  • 3 Stars
  • 1,637 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 1 Open issues
  • 39 Versions
  • 44 % Grown

The README.md

Contents-file

このプラグインはCakePHP5用ファイルアップロードツールです。, (*1)

インストール

composer.json, (*2)

{
    "require": {
        "satthi/contents-file": "*"
    }
}

composer install, (*3)

使い方

(初期設定・ローカルファイル保存の場合)

① 設定を記述 bootstrap.phpなど, (*4)

Configure::write('ContentsFile.Setting', [
    'type' => 'normal',
    // trueでファイル名がランダム文字列に
    'randomFile' => true,
    // trueで拡張子付きでファイルを保存する。loaderを通さずに使用する場合は設定しておいたほうが良い。
    'ext' => true,
    'Normal' => [
        'tmpDir' => TMP . 'cache/files/',
        'fileDir' => ROOT . '/files/',
    ],
]);

② プラグイン読込 Application.php, (*5)

public function bootstrap()
{
    $this->addPlugin('Migrations');
    // 追加
    $this->addPlugin('ContentsFile', ['routes' => true]);
}

③ tmpDir及びfileDirを権限777で準備, (*6)

(初期設定・S3保存の場合)

① 設定を記述 bootstrap.phpなど, (*7)

Configure::write('ContentsFile.Setting', [
    'type' => 's3',
    // trueでファイル名がランダム文字列に
    'randomFile' => true,
    // trueで拡張子付きでファイルを保存する。awsの場合は別途ヘッダーを吐き出すため設定する必要性はあまり高くない。
    'ext' => true,
    'S3' => [
        'key' => 'KEY',
        'secret' => 'SECRET',      // IAM Roleを利用する場合、省略可能
        'bucket' => 'BUCKET_NAME', // IAM Roleを利用する場合、省略可能
        'tmpDir' => 'tmp',
        'fileDir' => 'file',
        'workingDir' => TMP,
        // ファイルのURLをloaderを通さず直接awsに接続したい場合に設定
        /*
        //s3-ap-northeast-1.amazonaws.com/BUCKET_NAME でも
        //BUCKET_NAME.s3-website-ap-northeast-1.amazonaws.com でも
        //指定の文字列.cloudfront.net でも使用したいものを設定
        */
        'static_domain' => '//s3-ap-northeast-1.amazonaws.com/BUCKET_NAME',
        // minio 使用時にendpointを使用
        //'endpoint' => 'http://{{ip_address}}:9000',
    ]
]);

② プラグイン読込 Application.php, (*8)

public function bootstrap()
{
    // 追加
    $this->addPlugin('ContentsFile', ['routes' => true]);
}

③ workingDirを権限777で準備, (*9)

④ S3のjバケットを準備し、IAMの権限について以下を設定, (*10)

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:DeleteObject",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::BUCKET_NAME",
                "arn:aws:s3:::BUCKET_NAME/*"
            ]
        }
    ]
}

※ 作業するbucketについてs3:GetObject、s3:PutObject、s3:DeleteObject、s3:ListBucketの権限があれば良いです, (*11)

各種基本設定(共通)

マイグレーション実行, (*12)

ContentsFile/config/Migrations/20161109095904_AttachmentsAdd, (*13)

にファイルを置いているので利用してください。, (*14)

※topicsを例とする Table, (*15)

setTable('topics');
        $this->setPrimaryKey('id');
        // 追加項目
        $this->addBehavior('ContentsFile.ContentsFile');
        $this->addBehavior('Timestamp');
    }

    public function validationDefault(Validator $validator)
    {
        // providerを読み込み
        $validator->setProvider('contents_file', 'ContentsFile\Validation\ContentsFileValidation');
        $validator
            ->notEmpty('img', 'ファイルを添付してください' , function ($context){
                // fileValidationWhenメソッドを追加しました。
                return $this->fileValidationWhen($context, 'img');
            })
            ->add('img', 'uploadMaxSizeCheck', [
                'rule' => 'uploadMaxSizeCheck',
                'provider' => 'contents_file',
                'message' => 'ファイルアップロード容量オーバーです',
                'last' => true,
            ])
            ->add('img', 'checkMaxSize', [
                'rule' => ['checkMaxSize' , '1M'],
                'provider' => 'contents_file',
                'message' => 'ファイルアップロード容量オーバーです',
                'last' => true,
            ])
            ->add('img', 'extension', [
                'rule' => ['extension', ['jpg', 'jpeg', 'gif', 'png',]],
                'message' => '画像のみを添付して下さい',
                'last' => true,
            ])
            ;
        return $validator;
    }
}
```

Entity
```php
 [
            // 使用したいフィールドを設定
            'file' => [
                'resize' => false,
            ],
            'img' => [
                'resize' => [
                    // 画像のリサイズが必要な場合
                    ['width' => 300],
                    ['width' => 300, 'height' => 400],
                    // typeには
                    // normal(default) 長い方を基準に画像をリサイズする
                    // normal_s 短い方を基準に画像をリサイズする
                    // scoop 短い方を基準に画像をリサイズし、中央でくりぬきする
                    ['width' => 300, 'height' => 400, 'type' => 'scoop'],
                ],
            ],
        ],
    ];

    protected array $_accessible = [
        'title' => true,
        // 初期状態に追記
        'file' => true,
        'contents_file_file' => true,
        'delete_file' => true,
        'img' => true,
        'contents_file_img' => true,
        'delete_img' => true,
    ];


    //&getメソッドをoverride
    public function &get(string $property): mixed
    {
        $value = parent::get($property);
        $value = $this->getContentsFile($property, $value);
        return $value;
    }

    //setメソッドをoverride
    public function set(array|string $field, mixed $value = null, array $options = []){
        parent::set($field, $value , $options);
        $this->setContentsFile();
        return $this;
    }
}
```

Controller(※ほとんど変更の必要なし)
```php
viewBuilder()->setHelpers(['ContentsFile.ContentsFile']);
    }
}
```

Template
form.php
```php
    = $this->Form->create($topic, ['type' => 'file']) ?>
    <fieldset>
        <legend><?= __('Edit Topic') ?></legend>
        <?php
            echo $this->Form->control('file', ['type' => 'file']);
            // バリデーションに引っかかった際に、再度ファイルを登録しなくて済むための対応
            echo $this->ContentsFile->contentsFileHidden($topic->contents_file_file, 'contents_file_file');
            if (!empty($topic->contents_file_file)) {
                echo $this->ContentsFile->link($topic->contents_file_file);
                // 「delete_フィールド名」がtrueでファイルを削除
                echo $this->Form->control('delete_file', ['type' => 'checkbox', 'label' => 'delete']);
            }
            echo $this->Form->control('img', ['type' => 'file']);
            echo $this->ContentsFile->contentsFileHidden($topic->contents_file_img, 'contents_file_img');
            if (!empty($topic->contents_file_img)) {
                echo $this->ContentsFile->image($topic->contents_file_img);
                echo $this->Form->control('delete_img', ['type' => 'checkbox', 'label' => 'delete']);
            }
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>

view.php, (*16)


= $this->ContentsFile->link($topic->contents_file_file);?>

= $this->ContentsFile->image($topic->contents_file_img, ['resize' => ['width' => 300, 'height' => 400, 'type' => 'scoop']]);?>



= $this->ContentsFile->link($topic->contents_file_file, ['static_s3' => true]);?>

= $this->ContentsFile->image($topic->contents_file_img, ['resize' => ['width' => 300], 'static_s3' => true]);?>

The Versions

11/01 2018

dev-master

9999999-dev https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

11/01 2018

3.3.13

3.3.13.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

11/01 2018

dev-exif_error_check_fix

dev-exif_error_check_fix https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

27/10 2017

3.3.12

3.3.12.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

27/10 2017

dev-exif_rotate_check_add

dev-exif_rotate_check_add https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

14/09 2017

3.3.11

3.3.11.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

14/09 2017

dev-disabled_check

dev-disabled_check https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

11/09 2017

3.3.10

3.3.10.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

11/09 2017

3.3.9

3.3.9.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

07/09 2017

3.3.8

3.3.8.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

09/08 2017

dev-drag_drop_upload_base

dev-drag_drop_upload_base https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

02/08 2017

3.3.7

3.3.7.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

10/07 2017

3.3.6

3.3.6.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

17/05 2017

3.3.5

3.3.5.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

17/05 2017

3.3.4

3.3.4.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

07/03 2017

3.3.3

3.3.3.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

22/02 2017

3.3.2

3.3.2.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

21/02 2017

3.3.1

3.3.1.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

14/02 2017

3.3.0

3.3.0.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

08/02 2017

3.2.1

3.2.1.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

08/02 2017

3.2.0

3.2.0.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

07/02 2017

3.1.14

3.1.14.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

06/02 2017

3.1.13

3.1.13.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

01/02 2017

3.1.12

3.1.12.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

31/01 2017

3.1.11

3.1.11.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

31/01 2017

3.1.10

3.1.10.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

30/01 2017

3.1.9

3.1.9.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

15/12 2016

3.1.7

3.1.7.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

15/12 2016

3.1.6

3.1.6.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

09/12 2016

3.1.5

3.1.5.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

06/12 2016

3.1.4

3.1.4.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

06/12 2016

3.1.3

3.1.3.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

06/12 2016

3.1.2

3.1.2.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

06/12 2016

3.1.1

3.1.1.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

06/12 2016

3.1.0

3.1.0.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

06/12 2016

3.0.0

3.0.0.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

26/08 2016

3.0.1

3.0.1.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

 

The Development Requires

by Satoru Hagiwara

file cakephp

04/04 2016

dev-2.0dev

dev-2.0dev https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

  • ext-gd *

 

by Satoru Hagiwara

file cakephp

04/04 2016

2.0

2.0.0.0 https://github.com/satthi/Contents-file

CakePHP ContentsFile

  Sources   Download

MIT

The Requires

  • ext-gd *

 

by Satoru Hagiwara

file cakephp