1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: floor12 |
5
|
|
|
* Date: 01.01.2018 |
6
|
|
|
* Time: 12:56 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace floor12\files\logic; |
10
|
|
|
|
11
|
|
|
use floor12\files\components\SimpleImage; |
12
|
|
|
use floor12\files\models\File; |
13
|
|
|
use floor12\files\models\FileType; |
14
|
|
|
use Yii; |
15
|
|
|
use yii\base\ErrorException; |
16
|
|
|
use yii\web\BadRequestHttpException; |
17
|
|
|
use yii\web\IdentityInterface; |
18
|
|
|
use yii\web\UploadedFile; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class FileCreateFromInstance |
22
|
|
|
* @package floor12\files\logic |
23
|
|
|
*/ |
24
|
|
|
class FileCreateFromInstance |
25
|
|
|
{ |
26
|
|
|
private $_model; |
27
|
|
|
private $_owner; |
28
|
|
|
private $_attribute; |
29
|
|
|
private $_instance; |
30
|
|
|
private $_fullPath; |
31
|
|
|
private $_onlyUploaded; |
32
|
|
|
|
33
|
|
|
public function __construct(UploadedFile $file, array $data, IdentityInterface $identity = null, $onlyUploaded = true) |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
$this->_onlyUploaded = $onlyUploaded; |
37
|
|
|
|
38
|
|
|
if (!isset($data['attribute']) || !$data['attribute'] || !isset($data['modelClass']) || !$data['modelClass']) |
39
|
|
|
throw new BadRequestHttpException("Attribute or class name not set."); |
40
|
|
|
|
41
|
|
|
// Загружаем полученные данные |
42
|
|
|
$this->_instance = $file; |
43
|
|
|
$this->_attribute = $data['attribute']; |
44
|
|
|
|
45
|
|
|
if (!file_exists($this->_instance->tempName)) |
46
|
|
|
throw new ErrorException("Tmp file not found on disk."); |
47
|
|
|
|
48
|
|
|
// Инициализируем класс владельца файла для валидаций и ставим сценарий |
49
|
|
|
$this->_owner = new $data['modelClass']; |
50
|
|
|
|
51
|
|
|
if (isset($data['scenario'])) |
52
|
|
|
$this->_owner->setScenario($data['scenario']); |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
if (isset($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'])) { |
56
|
|
|
foreach ($this->_owner->behaviors['files']->attributes[$this->_attribute]['validator'] as $validator) { |
57
|
|
|
if (!$validator->validate($this->_instance, $error)) |
|
|
|
|
58
|
|
|
throw new BadRequestHttpException($error); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Создаем модель нового файла и заполняем первоначальными данными |
64
|
|
|
$this->_model = new File(); |
65
|
|
|
$this->_model->created = time(); |
66
|
|
|
$this->_model->field = $this->_attribute; |
67
|
|
|
$this->_model->class = $data['modelClass']; |
68
|
|
|
|
69
|
|
|
$this->_model->filename = new PathGenerator(Yii::$app->getModule('files')->storageFullPath) . '.' . $this->_instance->extension; |
70
|
|
|
$this->_model->title = $this->_instance->name; |
71
|
|
|
$this->_model->content_type = $this->_instance->type; |
72
|
|
|
$this->_model->size = $this->_instance->size; |
73
|
|
|
$this->_model->type = $this->detectType(); |
|
|
|
|
74
|
|
|
if ($identity) |
75
|
|
|
$this->_model->user_id = $identity->id; |
|
|
|
|
76
|
|
|
if ($this->_model->type == FileType::VIDEO) |
77
|
|
|
$this->_model->video_status = 0; |
78
|
|
|
|
79
|
|
|
//Генерируем полный новый адрес сохранения файла |
80
|
|
|
$this->_fullPath = Yii::$app->getModule('files')->storageFullPath . DIRECTORY_SEPARATOR . $this->_model->filename; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function detectType() |
87
|
|
|
{ |
88
|
|
|
$contentTypeArray = explode('/', $this->_model->content_type); |
89
|
|
|
if ($contentTypeArray[0] == 'image') |
90
|
|
|
return FileType::IMAGE; |
91
|
|
|
if ($contentTypeArray[0] == 'video') |
92
|
|
|
return FileType::VIDEO; |
93
|
|
|
return FileType::FILE; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return File |
98
|
|
|
*/ |
99
|
|
|
|
100
|
|
|
public function execute() |
101
|
|
|
{ |
102
|
|
|
$path = Yii::$app->getModule('files')->storageFullPath . $this->_model->filename; |
|
|
|
|
103
|
|
|
|
104
|
|
|
if ($this->_model->save()) { |
105
|
|
|
if (!$this->_onlyUploaded) |
106
|
|
|
copy($this->_instance->tempName, $this->_fullPath); |
107
|
|
|
else |
108
|
|
|
$this->_instance->saveAs($this->_fullPath, false); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($this->_model->type == FileType::IMAGE) { |
112
|
|
|
$this->rotateAfterUpload(); |
113
|
|
|
$this->resizeAfterUpload(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->_model; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
protected function rotateAfterUpload() |
121
|
|
|
{ |
122
|
|
|
$exif = ''; |
|
|
|
|
123
|
|
|
@$exif = exif_read_data($this->_fullPath); |
124
|
|
|
if (isset($exif['Orientation'])) { |
125
|
|
|
$ort = $exif['Orientation']; |
126
|
|
|
$rotatingImage = new SimpleImage(); |
127
|
|
|
$rotatingImage->load($this->_fullPath); |
128
|
|
|
switch ($ort) { |
129
|
|
|
case 3: // 180 rotate left |
130
|
|
|
$rotatingImage->rotateDegrees(180); |
131
|
|
|
break; |
132
|
|
|
case 6: // 90 rotate right |
133
|
|
|
$rotatingImage->rotateDegrees(270); |
134
|
|
|
break; |
135
|
|
|
case 8: // 90 rotate left |
136
|
|
|
$rotatingImage->rotateDegrees(90); |
137
|
|
|
} |
138
|
|
|
$rotatingImage->save($this->_fullPath); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function resizeAfterUpload() |
143
|
|
|
{ |
144
|
|
|
$maxWidth = $this->_owner->behaviors['files']->attributes[$this->_attribute]['maxWidth'] ?? 0; |
145
|
|
|
$maxHeight = $this->_owner->behaviors['files']->attributes[$this->_attribute]['maxHeight'] ?? 0; |
146
|
|
|
|
147
|
|
|
if ($maxWidth && $maxHeight) { |
148
|
|
|
$resizer = new FileResize($this->_model, $maxWidth, $maxHeight); |
149
|
|
|
$resizer->execute(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|