diff --git a/README.md b/README.md index 3cb056c..a0bb1e8 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Installation 'mimeTypes' => 'image/png', // Only png images 'maxSize' => 1024 * 1024 // 1 MB ], + //'extForOpen' => 'pdf, png',//Extensions for open file instead of download 'tableName' => '{{%attachments}}' // Optional, default to 'attach_file' ] ... @@ -132,6 +133,21 @@ Usage } ``` +Using Events +------------ +You may add the following function to your model + +```php +public function init(){ + $this->on(\nemmo\attachments\behaviors\FileBehavior::EVENT_AFTER_ATTACH_FILES, function ($event) { + /** @var $files \nemmo\attachments\models\File[] */ + $files = $event->files; + //your custom code + }); + parent::init(); +} +``` + Change log ---------- diff --git a/src/Module.php b/src/Module.php index 9c16117..d13bc5a 100644 --- a/src/Module.php +++ b/src/Module.php @@ -16,6 +16,12 @@ class Module extends \yii\base\Module public $tempPath = '@app/uploads/temp'; + /** + * Extensions for open file instead of download. + * Example: 'pdf, jpg' + */ + public $extForOpen = ''; + public $rules = []; public $tableName = 'attach_file'; @@ -142,6 +148,8 @@ public function attachFile($filePath, $owner) $file->size = filesize($filePath); $file->type = $fileType; $file->mime = FileHelper::getMimeType($filePath); + $file->create_by = \Yii::$app->user->id; + $file->create_date = new \yii\db\Expression('NOW()'); if ($file->save()) { unlink($filePath); diff --git a/src/behaviors/FileBehavior.php b/src/behaviors/FileBehavior.php index ece63fb..4be4b3b 100644 --- a/src/behaviors/FileBehavior.php +++ b/src/behaviors/FileBehavior.php @@ -8,6 +8,7 @@ namespace nemmo\attachments\behaviors; +use nemmo\attachments\events\FileEvent; use nemmo\attachments\models\File; use nemmo\attachments\ModuleTrait; use yii\base\Behavior; @@ -21,6 +22,8 @@ class FileBehavior extends Behavior { use ModuleTrait; + const EVENT_AFTER_ATTACH_FILES = 'afterAttachFiles'; + public function events() { return [ @@ -43,11 +46,18 @@ public function saveUploads($event) } $userTempDir = $this->getModule()->getUserDirPath(); + $attachedFiles = []; foreach (FileHelper::findFiles($userTempDir) as $file) { - if (!$this->getModule()->attachFile($file, $this->owner)) { + if (!($attachedFile = $this->getModule()->attachFile($file, $this->owner))) { throw new \Exception(\Yii::t('yii', 'File upload failed.')); + }else{ + $attachedFiles[] = $attachedFile; } } + if(count($attachedFiles)){ + $event = \Yii::createObject(['class' => FileEvent::class, 'files' => $attachedFiles]); + $this->owner->trigger(self::EVENT_AFTER_ATTACH_FILES, $event); + } rmdir($userTempDir); } diff --git a/src/controllers/FileController.php b/src/controllers/FileController.php index 9d1ec50..44af3a9 100644 --- a/src/controllers/FileController.php +++ b/src/controllers/FileController.php @@ -52,7 +52,11 @@ public function actionDownload($id) $file = File::findOne(['id' => $id]); $filePath = $this->getModule()->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type; - return Yii::$app->response->sendFile($filePath, "$file->name.$file->type"); + $inline = false; + $extForOpen = array_map('trim', explode(',', $this->getModule()->extForOpen)); + if(@in_array($file->type, $extForOpen)) $inline = true; + + return Yii::$app->response->sendFile($filePath, "$file->name.$file->type", ['inline' => $inline]); } public function actionDelete($id) diff --git a/src/events/FileEvent.php b/src/events/FileEvent.php new file mode 100644 index 0000000..4192f72 --- /dev/null +++ b/src/events/FileEvent.php @@ -0,0 +1,29 @@ +_files; + } + + /** + * @param nemmo\attachments\models\File[] $files + */ + public function setFiles($files) + { + $this->_files = $files; + } +} diff --git a/src/migrations/m180327_050508_add_fields_create_date_etc.php b/src/migrations/m180327_050508_add_fields_create_date_etc.php new file mode 100644 index 0000000..db71582 --- /dev/null +++ b/src/migrations/m180327_050508_add_fields_create_date_etc.php @@ -0,0 +1,32 @@ +addColumn($this->getModule()->tableName, 'create_by', $this->integer()); + $this->addColumn($this->getModule()->tableName, 'create_date', $this->timestamp()->defaultValue(null)); + + $this->createIndex('file_create_by', $this->getModule()->tableName, 'create_by'); + } + + /** + * @inheritdoc + */ + public function safeDown() + { + $this->dropColumn($this->getModule()->tableName, 'create_by'); + $this->dropColumn($this->getModule()->tableName, 'create_date'); + } +} diff --git a/src/models/File.php b/src/models/File.php index f3d2870..e5c587c 100644 --- a/src/models/File.php +++ b/src/models/File.php @@ -18,6 +18,8 @@ * @property integer $size * @property string $type * @property string $mime + * @property integer $create_by + * @property string $create_date */ class File extends ActiveRecord { @@ -66,7 +68,9 @@ public function attributeLabels() 'hash' => 'Hash', 'size' => 'Size', 'type' => 'Type', - 'mime' => 'Mime' + 'mime' => 'Mime', + 'create_by' => 'Create by', + 'create_date' => 'Create date', ]; }