1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
transformermanager.php
См. документацию.
1<?php
2
3namespace Bitrix\Main\UI\Viewer\Transformation;
4
5use Bitrix\Main\Engine\CurrentUser;
6use Bitrix\Main\Error;
7use Bitrix\Main\Event;
8use Bitrix\Main\EventResult;
9use Bitrix\Main\Loader;
10use Bitrix\Main\ModuleManager;
11use Bitrix\Main\Result;
12use Bitrix\Main\SystemException;
13use Bitrix\Main\Web\MimeType;
14
16{
17 const QUEUE_NAME = 'main_preview';
18 const PULL_TAG = 'mainTransform';
19
20 protected static $transformationList = [];
21
22 public function __construct()
23 {
24 //optimize
25 $this->buildTransformationList();
26 }
27
28 public static function getPullTag($fileId)
29 {
30 return self::PULL_TAG . $fileId;
31 }
32
33 private function buildTransformationList()
34 {
35 if (!empty(static::$transformationList))
36 {
37 return;
38 }
39
40 $default = [
41 Document::class,
42 Video::class,
43 ];
44
45 $event = new Event('main', 'onTransformationBuildList');
46 $event->send();
47
48 $additionalList = [];
49 foreach ($event->getResults() as $result)
50 {
51 if ($result->getType() != EventResult::SUCCESS)
52 {
53 continue;
54 }
55 $result = $result->getParameters();
56 if (!is_array($result))
57 {
58 throw new SystemException('Wrong event result. Must be array.');
59 }
60
61 foreach ($result as $class)
62 {
63 if (!is_string($class) || !class_exists($class))
64 {
65 throw new SystemException('Wrong event result. There is not a class.');
66 }
67
68 if (!is_subclass_of($class, Transformation::class, true))
69 {
70 throw new SystemException("Wrong event result. {$class} is not a subclass of " . Transformation::class);
71 }
72
73 $additionalList[] = $class;
74 }
75 }
76
77 static::$transformationList = array_merge($default, $additionalList);
78 }
79
80 public function isAvailable()
81 {
82 return ModuleManager::isModuleInstalled('transformer');
83 }
84
85 public function transform($fileId)
86 {
87 $result = new Result();
88 if (!Loader::includeModule('transformer'))
89 {
90 $result->addError(new Error('Could not include module transformer'));
91
92 return $result;
93 }
94
95 $fileData = \CFile::getByID($fileId)->fetch();
96 if (!$fileData)
97 {
98 $result->addError(new Error('Could not find file'));
99
100 return $result;
101 }
102
103 $transformation = $this->buildTransformationByFile($fileData);
104 if (!$transformation)
105 {
106 $result->addError(new Error('There is no transformation for file'));
107
108 return $result;
109 }
110
111 if(
112 $transformation->getInputMaxSize() > 0 &&
113 $fileData['FILE_SIZE'] > $transformation->getInputMaxSize()
114 )
115 {
116 $result->addError(new Error('Too big file for transformation'));
117
118 return $result;
119 }
120
121 $transformer = $transformation->buildTransformer();
122 if (!$transformer)
123 {
124 $result->addError(new Error('Could not build transformer'));
125
126 return $result;
127 }
128
129 if (!$this->shouldSendToTransformation($fileData))
130 {
131 return $result;
132 }
133
134 $result = $transformer->transform(
135 (int)$fileId,
136 [$transformation->getOutputExtension()],
137 'main',
138 CallbackHandler::class,
139 ['id' => (int)$fileId, 'fileId' => (int)$fileId, 'queue' => self::QUEUE_NAME]
140 );
141
142 if ($result->isSuccess())
143 {
144 $pullTag = $this->subscribeCurrentUserForTransformation($fileId);
145 $result->setData([
146 'pullTag' => $pullTag,
147 ]);
148 }
149
150 return $result;
151 }
152
153 protected function shouldSendToTransformation(array $fileData): bool
154 {
155 $id = (int)($fileData['ID'] ?? 0);
156 if ($id <= 0)
157 {
158 return true;
159 }
160
161 return !CallbackHandler::existSavedFile($id);
162 }
163
165 {
166 if (!Loader::includeModule('pull'))
167 {
168 return null;
169 }
170
171 $pullTag = self::getPullTag($fileId);
172 \CPullWatch::add(CurrentUser::get()->getId(), $pullTag);
173
174 return $pullTag;
175 }
176
183 public function buildTransformationByFile(array $fileData)
184 {
185 if (empty($fileData['CONTENT_TYPE']))
186 {
187 return null;
188 }
189
190 $transformation = $this->buildTransformationByContentType($fileData['CONTENT_TYPE']);
191 if ($transformation)
192 {
193 return $transformation;
194 }
195
196 if (empty($fileData['ORIGINAL_NAME']))
197 {
198 return null;
199 }
200
201 $mimeType = MimeType::getByFilename($fileData['ORIGINAL_NAME']);
202
203 return $this->buildTransformationByContentType($mimeType);
204 }
205
212 private function buildTransformationByContentType($contentType)
213 {
214 foreach (static::$transformationList as $transformationClass)
215 {
217 if (in_array($contentType, $transformationClass::getInputContentTypes(), true))
218 {
219 $reflectionClass = new \ReflectionClass($transformationClass);
220
221 return $reflectionClass->newInstance();
222 }
223 }
224
225 return null;
226 }
227}
Определения error.php:15
Определения event.php:5
static includeModule($moduleName)
Определения loader.php:67
static isModuleInstalled($moduleName)
Определения modulemanager.php:125
static getByFilename($filename)
Определения mimetype.php:258
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$event
Определения prolog_after.php:141
$contentType
Определения quickway.php:301