From 06e8339a49fb371c26f38b2d428ffdc641002131 Mon Sep 17 00:00:00 2001 From: sokollondon Date: Mon, 21 May 2018 09:41:34 +0500 Subject: [PATCH 1/3] Add fields create_date, create_by --- src/Module.php | 2 ++ ...0327_050508_add_fields_create_date_etc.php | 32 +++++++++++++++++++ src/models/File.php | 6 +++- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/migrations/m180327_050508_add_fields_create_date_etc.php diff --git a/src/Module.php b/src/Module.php index 9c16117..1bea67e 100644 --- a/src/Module.php +++ b/src/Module.php @@ -142,6 +142,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/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', ]; } From 347c22ab396cc35c16c60f244bfea86d33c0f8c1 Mon Sep 17 00:00:00 2001 From: sokollondon Date: Thu, 14 Jun 2018 16:03:13 +0500 Subject: [PATCH 2/3] #69 Add event after attach files --- README.md | 15 +++++++++++++++ src/behaviors/FileBehavior.php | 12 +++++++++++- src/events/FileEvent.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/events/FileEvent.php diff --git a/README.md b/README.md index 3cb056c..9bf6fb8 100644 --- a/README.md +++ b/README.md @@ -132,6 +132,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/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/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; + } +} From ce3ec69baed66cc6c0afcbfd435be6b05e12b8af Mon Sep 17 00:00:00 2001 From: sokollondon Date: Mon, 30 Mar 2020 10:17:29 +0500 Subject: [PATCH 3/3] Open file instead of download #78 --- README.md | 1 + src/Module.php | 6 ++++++ src/controllers/FileController.php | 6 +++++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9bf6fb8..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' ] ... diff --git a/src/Module.php b/src/Module.php index 1bea67e..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'; 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)