Skip to content

Commit

Permalink
Merge pull request #2 from insma/feature/Image
Browse files Browse the repository at this point in the history
New Feature/image
  • Loading branch information
mklemarczyk committed Jun 19, 2015
2 parents e4b5341 + ef2e0dc commit 77b9e1b
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 0 deletions.
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
{
}

0 comments on commit 77b9e1b

Please sign in to comment.