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

New Feature/image #2

Merged
merged 5 commits into from
Jun 19, 2015
Merged
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
5 changes: 5 additions & 0 deletions src/StorageModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class StorageModule extends \yii\base\Module
*/
public $databaseStorage = null;

/**
* @var array|null
*/
public $imageAllowedExtensions = ['jpg', 'png', 'gif', 'bmp', 'svg'];

/**
* @var array|null
*/
Expand Down
77 changes: 77 additions & 0 deletions src/actions/ImageManagerJsonAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @link https://github.com/insma/yii2-file-storage
* @copyright Copyright (c) 2015 Insma Software
* @license https://github.com/insma/yii2-file-storage/wiki/LICENSE
*/

namespace insma\storage\actions;

use Yii;
use insma\storage\models\StorageItem;
use yii\web\HttpException;
use yii\helpers\FileHelper;
use yii\helpers\Url;

/**
* @author Maciej Klemarczyk <[email protected]>
* @since 1.0
*/
class ImageManagerJsonAction extends \yii\base\Action
{
/**
* @inheritdoc
*/
public function init()
{
if (!Yii::$app->request->isAjax) {
//throw new HttpException(403, 'This action allow only ajaxRequest');
}
}

/**
* @inheritdoc
*/
public function run()
{
$models = StorageItem::find()->all();
$results = [];
if(Yii::$app->controller->module->webAccessUrl === false){
foreach($models as $model){
$filePath = Yii::$app->controller->module->getFilePath($model->file_path);
$url = Url::to(['view', 'id' => $model->id]);
$fileName = pathinfo($filePath, PATHINFO_FILENAME);
$results[] = $this->createEntry($filePath, $url, $fileName);
}
}else{
$onlyExtensions = array_map(function ($ext) {
return '*.' . $ext;
}, Yii::$app->controller->module->imageAllowedExtensions);
$filesPath = FileHelper::findFiles(Yii::$app->controller->module->getStorageDir(), [
'recursive' => true,
'only' => $onlyExtensions
]);
if (is_array($filesPath) && count($filesPath)) {
foreach ($filesPath as $filePath) {
$url = Yii::$app->controller->module->getUrl(pathinfo($filePath, PATHINFO_BASENAME));
$fileName = pathinfo($filePath, PATHINFO_FILENAME);
$results[] = $this->createEntry($filePath, $url, $fileName);
}
}
}
return $results;
}

/**
* @return array
*/
protected function createEntry($filePath, $url, $fileName)
{
return [
'thumb' => $url,
'image' => $url,
'title' => pathinfo($filePath, PATHINFO_FILENAME)
];
}
}
93 changes: 93 additions & 0 deletions src/controllers/ImageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/**
* @link https://github.com/insma/yii2-file-storage
* @copyright Copyright (c) 2015 Insma Software
* @license https://github.com/insma/yii2-file-storage/wiki/LICENSE
*/

namespace insma\storage\controllers;

use Yii;
use insma\storage\models\StorageItem;
use insma\storage\models\ImageUploadModel;
use insma\storage\actions\ImageManagerJsonAction;
use yii\web\Response;
use yii\filters\VerbFilter;
use yii\filters\ContentNegotiator;

/**
* @author Maciej Klemarczyk <[email protected]>
* @since 1.0
*/
class ImageController extends \yii\web\Controller
{
public $enableCsrfValidation = false;

public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'upload' => ['post'],
],
],
'negotiator' => [
'class' => ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON
],
]
];
}

public function actions()
{
return [
'json-index' => ImageManagerJsonAction::className(),
];
}

public function actionUpload()
{
$model = new ImageUploadModel([
'allowedExtensions' => Yii::$app->controller->module->imageAllowedExtensions,
]);
if ($model->upload()) {
return $model->getResponse();
} else {
return ['error' => 'Unable to save image file'];
}
}

public function actionView($id){
$model = $this->findModel($id);
$filePath = Yii::$app->controller->module->getFilePath($model->file_path);
if(is_file($filePath)){
$handle = fopen($filePath, "r");
$contents = fread($handle, filesize($filePath));
fclose($handle);
$this->layout = false;
Yii::$app->response->format = Response::FORMAT_RAW;
return $contents;
}
return null;
}

/**
* Finds the StorageItem model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return StorageItem the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = StorageItem::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
17 changes: 17 additions & 0 deletions src/models/ImageUploadModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* @link https://github.com/insma/yii2-file-storage
* @copyright Copyright (c) 2015 Insma Software
* @license https://github.com/insma/yii2-file-storage/wiki/LICENSE
*/

namespace insma\storage\models;

/**
* @author Maciej Klemarczyk <[email protected]>
* @since 1.0
*/
class ImageUploadModel extends FileUploadModel
{
}