Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
transformermanager.php
1<?
2
4
15use Bitrix\Transformer\Command;
16use Bitrix\Transformer\FileTransformer;
17
19{
20 const QUEUE_NAME = 'main_preview';
21 const PULL_TAG = 'mainTransform';
22
23 protected static $transformationList = [];
24
25 public function __construct()
26 {
27 //optimize
28 $this->buildTransformationList();
29 }
30
31 public static function getPullTag($fileId)
32 {
33 return self::PULL_TAG . $fileId;
34 }
35
36 private function buildTransformationList()
37 {
38 if (!empty(static::$transformationList))
39 {
40 return;
41 }
42
43 $default = [
44 Document::class,
45 Video::class,
46 ];
47
48 $event = new Event('main', 'onTransformationBuildList');
49 $event->send();
50
51 $additionalList = [];
52 foreach ($event->getResults() as $result)
53 {
54 if ($result->getType() != EventResult::SUCCESS)
55 {
56 continue;
57 }
58 $result = $result->getParameters();
59 if (!is_array($result))
60 {
61 throw new SystemException('Wrong event result. Must be array.');
62 }
63
64 foreach ($result as $class)
65 {
66 if (!is_string($class) || !class_exists($class))
67 {
68 throw new SystemException('Wrong event result. There is not a class.');
69 }
70
71 if (!is_subclass_of($class, Transformation::class, true))
72 {
73 throw new SystemException("Wrong event result. {$class} is not a subclass of " . Transformation::class);
74 }
75
76 $additionalList[] = $class;
77 }
78 }
79
80 static::$transformationList = array_merge($default, $additionalList);
81 }
82
83 public function isAvailable()
84 {
85 return ModuleManager::isModuleInstalled('transformer');
86 }
87
88 public function transform($fileId)
89 {
90 $result = new Result();
91 if (!Loader::includeModule('transformer'))
92 {
93 $result->addError(new Error('Could not include module transformer'));
94
95 return $result;
96 }
97
98 $fileData = \CFile::getByID($fileId)->fetch();
99 if (!$fileData)
100 {
101 $result->addError(new Error('Could not find file'));
102
103 return $result;
104 }
105
106 $transformation = $this->buildTransformationByFile($fileData);
107 if (!$transformation)
108 {
109 $result->addError(new Error('There is no transformation for file'));
110
111 return $result;
112 }
113
114 if(
115 $transformation->getInputMaxSize() > 0 &&
116 $fileData['FILE_SIZE'] > $transformation->getInputMaxSize()
117 )
118 {
119 $result->addError(new Error('Too big file for transformation'));
120
121 return $result;
122 }
123
124 $transformer = $transformation->buildTransformer();
125 if (!$transformer)
126 {
127 $result->addError(new Error('Could not build transformer'));
128
129 return $result;
130 }
131
132 $shouldSendPullTag = true;
133 $information = $this->getTransformationInformation($fileId);
134 if ($this->shouldSendToTransformation($information, $fileData))
135 {
136 $result = $transformer->transform(
137 (int)$fileId,
138 [$transformation->getOutputExtension()],
139 'main',
140 CallbackHandler::class,
141 ['id' => $fileId, 'fileId' => $fileId, 'queue' => self::QUEUE_NAME]
142 );
143
144 if (!$result->isSuccess())
145 {
146 $shouldSendPullTag = false;
147 }
148 }
149
150 if (isset($information['status']) && $information['status'] == Command::STATUS_ERROR)
151 {
152 $shouldSendPullTag = false;
153 $result->addError(new Error('Could not transform file', Command::STATUS_ERROR));
154 }
155
156 if ($shouldSendPullTag)
157 {
158 $pullTag = $this->subscribeCurrentUserForTransformation($fileId);
159 $result->setData([
160 'pullTag' => $pullTag,
161 ]);
162 }
163
164 return $result;
165 }
166
167 protected function shouldSendToTransformation($information, array $fileData)
168 {
169 if (!$information)
170 {
171 return true;
172 }
173
174 if (!isset($information['status']))
175 {
176 return true;
177 }
178
179 if ($information['status'] !== Command::STATUS_SUCCESS)
180 {
182 $date = $information['time'];
183 if ($date && (time() - $date->getTimestamp()) > 24 * 3600)
184 {
185 return true;
186 }
187 }
188
189 if ($information['status'] === Command::STATUS_SUCCESS && !CallbackHandler::existSavedFile($fileData['ID']))
190 {
191 return true;
192 }
193
194 return false;
195 }
196
197 protected function getTransformationInformation($fileId)
198 {
199 return FileTransformer::getTransformationInfoByFile((int)$fileId);
200 }
201
203 {
204 if (!Loader::includeModule('pull'))
205 {
206 return null;
207 }
208
209 $pullTag = self::getPullTag($fileId);
210 \CPullWatch::add(CurrentUser::get()->getId(), $pullTag);
211
212 return $pullTag;
213 }
214
221 public function buildTransformationByFile(array $fileData)
222 {
223 if (empty($fileData['CONTENT_TYPE']))
224 {
225 return null;
226 }
227
228 $transformation = $this->buildTransformationByContentType($fileData['CONTENT_TYPE']);
229 if ($transformation)
230 {
231 return $transformation;
232 }
233
234 if (empty($fileData['ORIGINAL_NAME']))
235 {
236 return null;
237 }
238
239 $mimeType = MimeType::getByFilename($fileData['ORIGINAL_NAME']);
240
241 return $this->buildTransformationByContentType($mimeType);
242 }
243
250 private function buildTransformationByContentType($contentType)
251 {
252 foreach (static::$transformationList as $transformationClass)
253 {
255 if (in_array($contentType, $transformationClass::getInputContentTypes(), true))
256 {
257 $reflectionClass = new \ReflectionClass($transformationClass);
258
259 return $reflectionClass->newInstance();
260 }
261 }
262
263 return null;
264 }
265}
static includeModule($moduleName)
Definition loader.php:69
static isModuleInstalled($moduleName)
static getByFilename($filename)
Definition mimetype.php:249