1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: floor12 |
5
|
|
|
* Date: 01.01.2018 |
6
|
|
|
* Time: 12:03 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace floor12\files\controllers; |
10
|
|
|
|
11
|
|
|
use floor12\files\actions\GetFileAction; |
12
|
|
|
use floor12\files\actions\GetPreviewAction; |
13
|
|
|
use floor12\files\components\FileInputWidget; |
14
|
|
|
use floor12\files\logic\FileCreateFromInstance; |
15
|
|
|
use floor12\files\logic\FileCropRotate; |
16
|
|
|
use floor12\files\logic\FileRename; |
17
|
|
|
use floor12\files\models\File; |
18
|
|
|
use Yii; |
19
|
|
|
use yii\filters\VerbFilter; |
20
|
|
|
use yii\web\BadRequestHttpException; |
21
|
|
|
use yii\web\Controller; |
22
|
|
|
use yii\web\NotFoundHttpException; |
23
|
|
|
use yii\web\Response; |
24
|
|
|
use yii\web\UploadedFile; |
25
|
|
|
use ZipArchive; |
26
|
|
|
|
27
|
|
|
class DefaultController extends Controller |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $actionsToCheck = [ |
33
|
|
|
'crop', |
34
|
|
|
'rename', |
35
|
|
|
'upload', |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritDoc |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
public function behaviors() |
43
|
|
|
{ |
44
|
|
|
return [ |
45
|
|
|
'verbs' => [ |
46
|
|
|
'class' => VerbFilter::class, |
47
|
|
|
'actions' => [ |
48
|
|
|
'zip' => ['GET', 'HEAD'], |
49
|
|
|
'cropper' => ['GET', 'HEAD'], |
50
|
|
|
'crop' => ['POST', 'HEAD'], |
51
|
|
|
'rename' => ['POST', 'HEAD'], |
52
|
|
|
'upload' => ['POST', 'HEAD'], |
53
|
|
|
'get' => ['GET', 'HEAD'], |
54
|
|
|
'preview' => ['GET', 'HEAD'], |
55
|
|
|
], |
56
|
|
|
], |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function beforeAction($action) |
65
|
|
|
{ |
66
|
|
|
$this->checkFormToken(); |
67
|
|
|
return parent::beforeAction($action); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** Првоеряем токен |
71
|
|
|
* @throws BadRequestHttpException |
72
|
|
|
*/ |
73
|
|
|
private function checkFormToken() |
74
|
|
|
{ |
75
|
|
|
if (in_array($this->action->id, $this->actionsToCheck) && FileInputWidget::generateToken() != Yii::$app->request->post('_fileFormToken')) |
76
|
|
|
throw new BadRequestHttpException('File-form token is wrong or missing.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param array $hashes |
81
|
|
|
* @param string $title |
82
|
|
|
*/ |
83
|
|
|
public function actionZip(array $hash, $title = 'files') |
84
|
|
|
{ |
85
|
|
|
$files = File::find()->where(["IN", "hash", $hash])->all(); |
86
|
|
|
|
87
|
|
|
$zip = new ZipArchive; |
88
|
|
|
$filename = Yii::getAlias("@webroot/assets/files.zip"); |
89
|
|
|
if (file_exists($filename)) |
90
|
|
|
@unlink($filename); |
|
|
|
|
91
|
|
|
if (sizeof($files) && $zip->open($filename, ZipArchive::CREATE)) { |
92
|
|
|
|
93
|
|
|
foreach ($files as $file) |
94
|
|
|
$zip->addFile($file->rootPath, $file->title); |
|
|
|
|
95
|
|
|
|
96
|
|
|
$zip->close(); |
97
|
|
|
header("Pragma: public"); |
98
|
|
|
header("Expires: 0"); |
99
|
|
|
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); |
100
|
|
|
header("Cache-Control: public"); |
101
|
|
|
header("Content-Description: File Transfer"); |
102
|
|
|
header("Content-type: application/octet-stream"); |
103
|
|
|
header("Content-Disposition: attachment; filename={$title}.zip"); |
104
|
|
|
header("Content-Transfer-Encoding: binary"); |
105
|
|
|
header("Content-Length: " . filesize($filename)); |
106
|
|
|
|
107
|
|
|
while (ob_get_level()) { |
108
|
|
|
ob_end_clean(); |
109
|
|
|
} |
110
|
|
|
readfile($filename); |
111
|
|
|
} else { |
112
|
|
|
echo 'Failed!'; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** Возвращаем HTML шаблон для внедрения в основной макет |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function actionCropper() |
120
|
|
|
{ |
121
|
|
|
return $this->renderPartial('_cropper'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** Кропаем и поворачиваем картинку, возращая ее новый адрес. |
125
|
|
|
* @return string |
126
|
|
|
* @throws \yii\base\InvalidConfigException |
127
|
|
|
*/ |
128
|
|
|
public function actionCrop() |
129
|
|
|
{ |
130
|
|
|
return Yii::createObject(FileCropRotate::class, [Yii::$app->request->post()])->execute(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** Переименовываем файл |
134
|
|
|
* @return string |
135
|
|
|
* @throws BadRequestHttpException |
136
|
|
|
* @throws \yii\base\InvalidConfigException |
137
|
|
|
*/ |
138
|
|
|
public function actionRename() |
139
|
|
|
{ |
140
|
|
|
return Yii::createObject(FileRename::class, [Yii::$app->request->post()])->execute(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
/** Создаем новый файл |
145
|
|
|
* @return string |
146
|
|
|
* @throws BadRequestHttpException |
147
|
|
|
* @throws \yii\base\InvalidConfigException |
148
|
|
|
*/ |
149
|
|
|
public function actionUpload() |
150
|
|
|
{ |
151
|
|
|
$model = Yii::createObject(FileCreateFromInstance::class, [ |
152
|
|
|
UploadedFile::getInstanceByName('file'), |
153
|
|
|
Yii::$app->request->post(), |
154
|
|
|
Yii::$app->user->identity, |
155
|
|
|
])->execute(); |
156
|
|
|
|
157
|
|
|
|
158
|
|
|
if ($model->errors) { |
159
|
|
|
throw new BadRequestHttpException('Ошибки валидации модели'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$ratio = Yii::$app->request->post('ratio') ?? null; |
163
|
|
|
|
164
|
|
|
$view = Yii::$app->request->post('mode') == 'single' ? "_single" : "_file"; |
165
|
|
|
|
166
|
|
|
if ($ratio) |
167
|
|
|
$this->getView()->registerJs("initCropper({$model->id}, '{$model->href}', {$ratio}, true);"); |
168
|
|
|
|
169
|
|
|
return $this->renderAjax($view, [ |
170
|
|
|
'model' => $model, |
171
|
|
|
'ratio' => $ratio |
172
|
|
|
]); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return array|string[] |
177
|
|
|
*/ |
178
|
|
|
public function actions() |
179
|
|
|
{ |
180
|
|
|
return [ |
181
|
|
|
'get' => GetFileAction::class, |
182
|
|
|
'image' => GetPreviewAction::class |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/* |
187
|
|
|
* Выдача файлов через контроллер. |
188
|
|
|
*/ |
189
|
|
|
public function actionPreview($hash) |
190
|
|
|
{ |
191
|
|
|
$model = File::findOne(['hash' => $hash]); |
192
|
|
|
|
193
|
|
|
if (!$model) |
|
|
|
|
194
|
|
|
throw new NotFoundHttpException("Запрашиваемый файл не найден в базе."); |
195
|
|
|
|
196
|
|
|
$response = Yii::$app->response; |
|
|
|
|
197
|
|
|
$response->format = Response::FORMAT_RAW; |
198
|
|
|
$response->getHeaders()->set('Content-Type', 'image/jpeg; charset=utf-8'); |
199
|
|
|
|
200
|
|
|
Yii::$app->response->headers->set('Last-Modified', date("c", $model->created)); |
201
|
|
|
Yii::$app->response->headers->set('Cache-Control', 'public, max-age=' . (60 * 60 * 24 * 15)); |
202
|
|
|
|
203
|
|
|
if (!file_exists($model->rootPreviewPath)) |
204
|
|
|
throw new NotFoundHttpException('Preview not found.'); |
205
|
|
|
|
206
|
|
|
$response->sendFile($model->rootPreviewPath, 'preview.jpg'); |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: