-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from insma/feature/Image
New Feature/image
- Loading branch information
Showing
4 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |