Skip to content

Commit

Permalink
Merge pull request #88 from dmkdesign/master
Browse files Browse the repository at this point in the history
Allow file controller do download inline and adjust preview configs
  • Loading branch information
dmkdesign authored Sep 13, 2023
2 parents ae041ad + e83ca75 commit f0f1d85
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
53 changes: 39 additions & 14 deletions src/behaviors/FileBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,7 @@ public function getInitialPreview()
}

foreach ($this->getFiles() as $file) {
if (substr($file->mime, 0, 5) === 'image') {
$initialPreview[] = Html::img($file->getUrl(), ['class' => 'file-preview-image']);
} else {
$initialPreview[] = Html::beginTag('div', ['class' => 'file-preview-other']) .
Html::beginTag('h2') .
Html::tag('i', '', ['class' => 'glyphicon glyphicon-file']) .
Html::endTag('h2') .
Html::endTag('div');
}
$initialPreview[] =$file->getUrl(true);
}

return $initialPreview;
Expand All @@ -132,14 +124,47 @@ public function getInitialPreviewConfig()
}

foreach ($this->getFiles() as $index => $file) {
$initialPreviewConfig[] = [
if(str_contains($file->mime,"image"))
{
$initialPreviewConfig[] = [
'caption' => "$file->name.$file->type",
'url' => Url::toRoute(['/attachments/file/delete',
'id' => $file->id])
];
}
elseif(str_contains($file->mime,"text")){
$initialPreviewConfig[] = [
'type'=> "text",
'caption' => "$file->name.$file->type",
'url' => Url::toRoute(['/attachments/file/delete',
'id' => $file->id
]),
'id' => $file->id])
];
}
elseif(str_contains($file->mime,"video")){
$initialPreviewConfig[] = [
'type'=> "video",
'caption' => "$file->name.$file->type",
'url' => Url::toRoute(['/attachments/file/delete',
'id' => $file->id])
];
}
elseif(str_contains($file->mime,"doc")||str_contains($file->mime,".ppt")||str_contains($file->mime,".xls")){
$initialPreviewConfig[] = [
'type'=> "office",
'caption' => "$file->name.$file->type",
'url' => Url::toRoute(['/attachments/file/delete',
'id' => $file->id])
];
}
else{
$initialPreviewConfig[] = [
'type'=> $file->type,
'caption' => "$file->name.$file->type",
'url' => Url::toRoute(['/attachments/file/delete',
'id' => $file->id])
];
}
}

return $initialPreviewConfig;
}
}
}
2 changes: 2 additions & 0 deletions src/components/AttachmentsInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public function init()
FileHelper::removeDirectory($this->getModule()->getUserDirPath()); // Delete all uploaded files in past

$this->pluginOptions = array_replace($this->pluginOptions, [
'initialPreviewFileType'=> 'image',
'initialPreviewAsData'=> 'true', //let kartik deal with the previews!
'uploadUrl' => Url::toRoute('/attachments/file/upload'),
'uploadAsync' => false
]);
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public function actionUpload()
}
}

public function actionDownload($id)
public function actionDownload($id,$inline=false)
{
$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");
return Yii::$app->response->sendFile($filePath, "$file->name.$file->type",['inline'=>$inline]);
}

public function actionDelete($id)
Expand Down
4 changes: 2 additions & 2 deletions src/models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public function attributeLabels()
];
}

public function getUrl()
public function getUrl($inline = false)
{
return Url::to(['/attachments/file/download', 'id' => $this->id]);
return Url::to(['/attachments/file/download', 'id' => $this->id,'inline'=>$inline]);
}

public function getPath()
Expand Down

0 comments on commit f0f1d85

Please sign in to comment.