Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add view action #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ Installation

6. Make sure that you specified `maxFiles` in module rules and `maxFileCount` on `AttachmentsInput` to the number that you want

7. If you want to use resize features install [himiklab/yii2-easy-thumbnail-image-helper](https://github.com/himiklab/yii2-easy-thumbnail-image-helper)

Usage
-----

Expand Down Expand Up @@ -113,6 +115,8 @@ Usage
Change log
----------

- **May 13, 2015** - Added "view" action to show image inline instead of forcing dowload. The model has now getDownloadUrl and getViewUrl (maxxer)
- **May 13, 2015** - Added optional image resize, using himiklab/yii2-easy-thumbnail-image-helper (maxxer)
- **May 1, 2015** - Fixed uploading when connection is slow or uploading time is long. Now ```onclick``` event on submit button is deprecated
- **Apr 16, 2015** - Allow users to have a custom behavior class inheriting from FileBehavior.
- **Apr 4, 2015** - Now all temp uploaded files will be deleted on every new form opened.
Expand Down
51 changes: 49 additions & 2 deletions controllers/FileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,59 @@ public function actionUpload()
}
}

public function actionDownload($id)
/**
* Download action for the file
* @param integer $id ID of the image to fetch
* @param integer $size Optional maximum size of the image. Requires yii2-easy-thumbnail-image-helper
* @param boolean $crop Optional cropping. Requires yii2-easy-thumbnail-image-helper
* @param boolean $forceDownload If TRUE forces the download of the file, otherwise shows inline
* @return static Image data
*/
public function actionDownload($id, $size = NULL, $crop = FALSE, $forceDownload = TRUE)
{
if (!is_null($size))
$this->checkResizeRequirements ();

$file = File::findOne(['id' => $id]);
$filePath = $this->getModule()->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type;

// Resize if requested
if (!is_null($size)) {
$filePath = \himiklab\thumbnail\EasyThumbnailImage::thumbnailFile(
$filePath,
$size,
$size,
$crop ? \himiklab\thumbnail\EasyThumbnailImage::THUMBNAIL_INSET : \himiklab\thumbnail\EasyThumbnailImage::THUMBNAIL_OUTBOUND);
}

return \Yii::$app->response->sendFile($filePath, "$file->name.$file->type");
return \Yii::$app->response->sendFile(
$filePath,
$file->name.$file->type,
['inline' => !$forceDownload, 'mimeType' => $file->mime]
);
}

/**
* View action for the file
* @param integer $id ID of the image to fetch
* @param integer $size Optional maximum size of the image. Requires yii2-easy-thumbnail-image-helper
* @param boolean $crop Optional cropping. Requires yii2-easy-thumbnail-image-helper
* @return static Image data
*/
public function actionView($id, $size = NULL, $crop = FALSE)
{
$this->actionDownload($id, $size, $crop, FALSE);
}

/**
* Check if we have the resize functionalities
* @throws Exception
*/
private function checkResizeRequirements ()
{
if (!class_exists("\\himiklab\\thumbnail\\EasyThumbnailImage")) {
throw new \Exception("You need himiklab/yii2-easy-thumbnail-image-helper for image resize features");
}
}

public function actionDelete($id)
Expand Down
29 changes: 28 additions & 1 deletion models/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
* @property integer $size
* @property string $type
* @property string $mime
*
* @property string $url Alias for downloadUrl
* @property string $downloadUrl Get the download url of the image
* @property string $viewUrl Get the inline view url of the image
* @property string $path Get the local file path of the image
*/
class File extends ActiveRecord
{
Expand Down Expand Up @@ -60,10 +65,32 @@ public function attributeLabels()
];
}

public function getUrl()
/**
* Obtains the download url of hte image
* @return string
*/
public function getDownloadUrl()
{
return Url::to(['/attachments/file/download', 'id' => $this->id]);
}

/**
* Obtains the inline view url of hte image
* @return string
*/
public function getViewUrl()
{
return Url::to(['/attachments/file/view', 'id' => $this->id]);
}

/**
* Support for legacy method
* @return string
*/
public function getUrl()
{
return $this->getDownloadUrl();
}

public function getPath()
{
Expand Down