Skip to content

Commit 4971983

Browse files
author
Igor Chepurnoy
committed
add action for image uploading, get list of all images
1 parent 3dc3ca0 commit 4971983

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
{
1717
"name": "Igor Chepurnoy",
18-
"email": "igorzfort@gmail.com"
18+
"email": "chepurnoi.igor@gmail.com"
1919
}
2020
],
2121
"require": {

controllers/ManageController.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Yii;
66
use yii\web\Controller;
77
use yii\web\NotFoundHttpException;
8+
use yii\web\UnprocessableEntityHttpException;
89
use yii\web\UploadedFile;
10+
use yii2mod\cms\models\AttachmentModel;
911
use yii2mod\cms\models\CmsModel;
1012
use yii2mod\editable\EditableAction;
1113

@@ -56,7 +58,8 @@ class ManageController extends Controller
5658
'create' => ['get', 'post'],
5759
'update' => ['get', 'post'],
5860
'delete' => ['post'],
59-
'file-upload' => ['post'],
61+
'image-upload' => ['post'],
62+
'images' => ['get'],
6063
],
6164
];
6265

@@ -177,21 +180,39 @@ public function actionDelete($id)
177180
}
178181

179182
/**
183+
* Upload an image
184+
*
180185
* @return \yii\web\Response
186+
*
187+
* @throws UnprocessableEntityHttpException
181188
*/
182-
public function actionFileUpload()
189+
public function actionImageUpload()
183190
{
184191
$model = Yii::createObject($this->attachmentModelClass);
185192
$model->file = UploadedFile::getInstanceByName('file');
186193

187-
if ($model->save()) {
188-
$result = [
189-
'filelink' => $model->getFileUrl(),
190-
'filename' => $model->getFileSelfName(),
191-
];
192-
} else {
193-
$result = [
194-
'error' => $model->getFirstError('file'),
194+
if (!$model->save()) {
195+
throw new UnprocessableEntityHttpException($model->getFirstError('file'));
196+
}
197+
198+
return $this->asJson([
199+
'link' => $model->getFileUrl('origin'),
200+
]);
201+
}
202+
203+
/**
204+
* Return list of all images
205+
*
206+
* @return \yii\web\Response
207+
*/
208+
public function actionImages()
209+
{
210+
$result = [];
211+
212+
foreach (AttachmentModel::find()->each() as $attachment) {
213+
$result[] = [
214+
'url' => $attachment->getFileUrl('origin'),
215+
'thumb' => $attachment->getFileUrl('thumbnail'),
195216
];
196217
}
197218

models/AttachmentModel.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Yii;
66
use yii\db\ActiveRecord;
7-
use yii2tech\ar\file\FileBehavior;
7+
use yii2tech\ar\file\ImageFileBehavior;
88

99
/**
1010
* This is the model class for table "{{%attachment}}".
@@ -31,7 +31,7 @@ public function rules()
3131
return [
3232
[['file_version'], 'integer'],
3333
[['file_extension'], 'string', 'max' => 255],
34-
['file', 'file', 'skipOnEmpty' => false],
34+
['file', 'file', 'mimeTypes' => ['image/jpeg', 'image/pjpeg', 'image/png', 'image/gif'], 'skipOnEmpty' => !$this->isNewRecord],
3535
];
3636
}
3737

@@ -54,10 +54,14 @@ public function behaviors()
5454
{
5555
return [
5656
'file' => [
57-
'class' => FileBehavior::class,
57+
'class' => ImageFileBehavior::class,
5858
'fileStorageBucket' => 'attachment',
5959
'fileExtensionAttribute' => 'file_extension',
6060
'fileVersionAttribute' => 'file_version',
61+
'fileTransformations' => [
62+
'origin',
63+
'thumbnail' => [250, 250],
64+
],
6165
],
6266
];
6367
}

0 commit comments

Comments
 (0)