satthi / contents-file
CakePHP ContentsFile
5.0.0
2023-11-08 08:18 UTC
Requires
- ext-gd: *
- aws/aws-sdk-php: 3.*
- cakephp/cakephp: ~5.0
- symfony/filesystem: 5.*
- symfony/finder: 5.*
Requires (Dev)
- dev-master
- 5.0.0
- 4.0.12
- 4.0.11
- 4.0.10
- 4.0.9
- 4.0.8
- 4.0.7
- 4.0.6
- 4.0.5
- 4.0.4
- 4.0.3
- 4.0.2
- 4.0.1
- 4.0.0
- 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- 3.4.8
- 3.4.7
- 3.4.6
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.13
- 3.3.12
- 3.3.11
- 3.3.10
- 3.3.9
- 3.3.8
- 3.3.7
- 3.3.6
- 3.3.5
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.14
- 3.1.13
- 3.1.12
- 3.1.11
- 3.1.10
- 3.1.9
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.1
- 3.0.0
- 2.7
- 2.6
- 2.5
- 2.4
- 2.3
- 2.2
- 2.1
- 2.0
- dev-4dev
- dev-header_css_add
- dev-3dev
- dev-2.0dev
This package is auto-updated.
Last update: 2024-09-08 10:16:23 UTC
README
此插件是用于CakePHP5的文件上传工具。
安装
composer.json
{
"require": {
"satthi/contents-file": "*"
}
}
composer install
使用方法
(初始设置·本地文件保存时)
① 在 bootstrap.php 等处描述设置
Configure::write('ContentsFile.Setting', [ 'type' => 'normal', // trueでファイル名がランダム文字列に 'randomFile' => true, // trueで拡張子付きでファイルを保存する。loaderを通さずに使用する場合は設定しておいたほうが良い。 'ext' => true, 'Normal' => [ 'tmpDir' => TMP . 'cache/files/', 'fileDir' => ROOT . '/files/', ], ]);
② 在 Application.php 中读取插件
public function bootstrap() { $this->addPlugin('Migrations'); // 追加 $this->addPlugin('ContentsFile', ['routes' => true]); }
③ 将 tmpDir 和 fileDir 准备为权限 777
(初始设置·S3 保存时)
① 在 bootstrap.php 等处描述设置
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 中读取插件
public function bootstrap() { // 追加 $this->addPlugin('ContentsFile', ['routes' => true]); }
③ 将 workingDir 准备为权限 777
④ 准备 S3 的 jbucket 并设置 IAM 权限
{
"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 的权限
各种基本设置(通用)
执行迁移
ContentsFile/config/Migrations/20161109095904_AttachmentsAdd
中的文件,请使用。
※ 以 topics 为例的 Table
<?php namespace App\Model\Table; use Cake\ORM\Table; class TopicsTable extends Table { public function initialize(array $config) { parent::initialize($config); $this->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; } }
实体
<?php namespace App\Model\Entity; use Cake\ORM\Entity; // 追加項目 use ContentsFile\Model\Entity\ContentsFileTrait; class Topic extends Entity { // 追加項目 use ContentsFileTrait; // 追加項目 public $contentsFileConfig = [ 'fields' => [ // 使用したいフィールドを設定 '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; } }
控制器(※几乎无需更改)
<?php namespace App\Controller; use Cake\Event\EventInterface; use App\Controller\AppController; class TopicsController extends AppController { public function beforeFilter(EventInterface $event) { parent::beforeFilter($event); $this->viewBuilder()->setHelpers(['ContentsFile.ContentsFile']); } }
模板 form.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
<?php // linkでファイルのリンク作成?> <?= $this->ContentsFile->link($topic->contents_file_file);?> <?php // imgでimgタグ作成 リサイズお画像の指定はオプションで指定?> <?= $this->ContentsFile->image($topic->contents_file_img, ['resize' => ['width' => 300, 'height' => 400, 'type' => 'scoop']]);?> <?php // 静的ホスティングにアクセス?> <?php // linkでファイルのリンク作成?> <?= $this->ContentsFile->link($topic->contents_file_file, ['static_s3' => true]);?> <?php // imgでimgタグ作成 リサイズお画像の指定はオプションで指定?> <?= $this->ContentsFile->image($topic->contents_file_img, ['resize' => ['width' => 300], 'static_s3' => true]);?>