Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
callbackhandler.php
1<?php
2
4
9use Bitrix\Transformer;
10
11if (Loader::includeModule('transformer'))
12{
13 final class CallbackHandler implements Transformer\InterfaceCallback
14 {
32 public static function call($status, $command, $params, $result = array())
33 {
34 if (isset($params['fileId']) && $params['fileId'] > 0)
35 {
36 Transformer\FileTransformer::clearInfoCache($params['fileId']);
37 }
38 if ($status == Transformer\Command::STATUS_ERROR && isset($params['id']))
39 {
40 return true;
41 }
42
43 if (!isset($params['id']) || !isset($params['fileId']) || !isset($result['files']))
44 {
45 return 'wrong parameters';
46 }
47
48 if ($status != Transformer\Command::STATUS_UPLOAD)
49 {
50 return 'wrong command status';
51 }
52
53 $previewLogId = $previewId = $previewImageId = 0;
54 $previewLog = FilePreviewTable::getList([
55 'select' => ['ID'],
56 'filter' => ['FILE_ID' => $params['fileId']],
57 'limit' => 1,
58 ])->fetch();
59
60 if (isset($previewLog['ID']))
61 {
62 $previewLogId = $previewLog['ID'];
63 }
64
65 foreach ($result['files'] as $ext => $filePath)
66 {
67 if ($ext === 'jpg')
68 {
69 $previewImageId = self::saveFile($filePath, 'image/jpeg');
70 if ($previewImageId)
71 {
72 if ($previewLogId)
73 {
74 FilePreviewTable::update($previewLogId, [
75 'PREVIEW_IMAGE_ID' => $previewImageId,
76 ]);
77 }
78 else
79 {
80 $resultAdd = FilePreviewTable::add([
81 'FILE_ID' => $params['fileId'],
82 'PREVIEW_IMAGE_ID' => $previewImageId,
83 ]);
84
85 if ($resultAdd->getId())
86 {
87 $previewLogId = $resultAdd->getId();
88 }
89 }
90 }
91 }
92 else
93 {
94 $previewId = self::saveFile($filePath, MimeType::getByFileExtension($ext));
95 if ($previewLogId)
96 {
97 FilePreviewTable::update($previewLogId, [
98 'PREVIEW_ID' => $previewId,
99 ]);
100 }
101 else
102 {
103 FilePreviewTable::add([
104 'FILE_ID' => $params['fileId'],
105 'PREVIEW_ID' => $previewId,
106 ]);
107 }
108
109 self::sendNotifyAboutTransformation($params['fileId']);
110 }
111 }
112
113 (new Event('main', 'onFileTransformationComplete', [
114 'fileId' => $params['fileId'],
115 ]))->send();
116
117 return true;
118 }
119
120 protected static function sendNotifyAboutTransformation($fileId)
121 {
122 if (Loader::includeModule('pull'))
123 {
124 \CPullWatch::addToStack(
125 TransformerManager::getPullTag($fileId),
126 [
127 'module_id' => 'main',
128 'command' => 'transformationComplete',
129 'params' => [
130 'fileId' => $fileId,
131 ],
132 ]
133 );
134 }
135 }
136
137 protected static function saveFile($file, $type)
138 {
139 $fileArray = \CFile::makeFileArray($file, $type);
140 $fileArray['MODULE_ID'] = 'main';
141 $fileId = \CFile::saveFile($fileArray, 'main_preview');
142 if ($fileId)
143 {
144 return $fileId;
145 }
146
147 return false;
148 }
149
150 public static function existSavedFile($fileId)
151 {
152 $previewRow = FilePreviewTable::getList([
153 'select' => ['ID', 'PID' => 'PREVIEW.ID'],
154 'filter' => [
155 '=FILE_ID' => $fileId,
156 ],
157 'limit' => 1,
158 ])->fetch();
159
160 return !empty($previewRow['PID']);
161 }
162 }
163}
static includeModule($moduleName)
Definition loader.php:69