Skip to content

Commit

Permalink
added single image response instead of images list #137
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Jun 18, 2018
1 parent 2514e2f commit 1e904b7
Show file tree
Hide file tree
Showing 9 changed files with 5,932 additions and 32 deletions.
29 changes: 23 additions & 6 deletions src/apis/StorageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use yii\web\NotFoundHttpException;
use yii\data\ActiveDataProvider;
use luya\admin\file\Query;
use yii\data\ArrayDataProvider;
use luya\admin\models\StorageImage;

/**
* Filemanager and Storage API.
Expand Down Expand Up @@ -85,7 +87,7 @@ public function actionDataFolders()
*/
public function actionDataFiles($folderId = 0, $page = 0)
{
$perPage = 50;
$perPage = 10;
$totalCount = (new Query())->where(['folder_id' => $folderId, 'is_hidden' => false, 'is_deleted' => false])->count();

$files = $this->getOrSetHasCache(['storageApiDataFiles', $folderId, $page], function() use ($folderId, $page, $perPage) {
Expand Down Expand Up @@ -119,12 +121,17 @@ public function actionDataFiles($folderId = 0, $page = 0)
return $files;
}, 0, new DbDependency(['sql' => 'SELECT MAX(id) FROM admin_storage_file WHERE is_deleted=false']));

return $this->generatePaginationArrayResponse($page, $perPage, $totalCount, $files);
}

private function generatePaginationArrayResponse($currentPage, $perPage, $totalCount, array $files)
{
return [
'page' => (int) $currentPage,
'perPage' => $perPage,
'totalCount' => $totalCount,
'count' => count($files),
'data' => $files,
'page' => (int) $page,
];
}

Expand Down Expand Up @@ -161,13 +168,24 @@ public function actionDataImages()
*/
public function actionFileInfo($id)
{
$model = StorageFile::find()->where(['id' => $id])->with(['user'])->one();
$model = StorageFile::find()->where(['id' => $id])->with(['user', 'images'])->one();

if (!$model) {
throw new NotFoundHttpException("Unable to find the given storage file.");
}

return $model->toArray([], ['user', 'file']);
return $model->toArray([], ['user', 'file', 'images']);
}

public function actionImageInfo($id)
{
$model = StorageImage::find()->where(['id' => $id])->with(['file'])->one();

if (!$model) {
throw new NotFoundHttpException("Unable to find the given storage image.");
}

return $model->toArray(['id', 'file_id', 'filter_id', 'resolution_width', 'resolution_height'], ['source']);
}

/**
Expand Down Expand Up @@ -229,10 +247,9 @@ public function actionFilemanagerUpdateCaption()
*
* @return array
*/
public function actionImageUpload()
public function actionImageFilter()
{
$this->checkRouteAccess(self::PERMISSION_ROUTE);

try {
$create = Yii::$app->storage->addImage(Yii::$app->request->post('fileId', null), Yii::$app->request->post('filterId', null), true);
if ($create) {
Expand Down
12 changes: 11 additions & 1 deletion src/models/StorageFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ public function getUser()
return $this->hasOne(User::class, ['id' => 'upload_user_id']);
}

/**
* Get all images fro the given file.
*
* @return \yii\db\ActiveQuery
*/
public function getImages()
{
return $this->hasMany(StorageImage::class, ['file_id' => 'id']);
}

/**
* Get the file for the corresponding model.
*
Expand All @@ -132,6 +142,6 @@ public function getFile()
*/
public function extraFields()
{
return ['user', 'file'];
return ['user', 'file', 'images'];
}
}
29 changes: 29 additions & 0 deletions src/models/StorageImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
/**
* StorageImage Model.
*
* @property int $id
* @property int $file_id
* @property int $filter_id
* @property int $resolution_width
* @property int $resolution_height
*
* @author Basil Suter <basil@nadar.io>
* @since 1.0.0
*/
Expand Down Expand Up @@ -47,11 +53,29 @@ public function beforeDelete()
}
}

/**
*
* @return StorageFile
*/
public function getFile()
{
return $this->hasOne(StorageFile::className(), ['id' => 'file_id']);
}

/**
* Returns the current file source path for the current filter image.
* @return string
*/
public function getSource()
{
$fileName = $this->filter_id . '_' . $this->file->name_new_compound;

return Yii::$app->storage->fileAbsoluteHttpPath($fileName);
}

/**
* @return boolean
*/
public function deleteSource()
{
$image = Yii::$app->storage->getImage($this->id);
Expand All @@ -65,4 +89,9 @@ public function deleteSource()

return true;
}

public function extraFields()
{
return ['source'];
}
}
124 changes: 123 additions & 1 deletion src/resources/dist/js/login.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/resources/dist/js/login.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5,656 changes: 5,655 additions & 1 deletion src/resources/dist/js/main.uglified.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/resources/dist/js/main.uglified.js.map

Large diffs are not rendered by default.

Loading

0 comments on commit 1e904b7

Please sign in to comment.