Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
FileItem.php
1<?php
2
4
5use Bitrix\Disk\Document\OnlyOffice\Templates\CreateDocumentByCallTemplateScenario;
6use Bitrix\Disk\File;
7use Bitrix\Disk\Driver;
8use Bitrix\Disk\Folder;
9use Bitrix\Disk\Storage;
10use Bitrix\Disk\TypeFile;
11use Bitrix\Disk\Ui\FileAttributes;
13use Bitrix\Im\V2\Common\ContextCustomer;
21
23{
24 use ContextCustomer;
25
26 protected ?int $chatId = null;
27 protected ?int $diskFileId = null;
28 protected ?File $diskFile = null;
29 protected ?string $contentType = null;
30
35 public function __construct($diskFile, ?int $chatId = null)
36 {
37 if ($diskFile instanceof File)
38 {
39 $this->setDiskFile($diskFile);
40 }
41 elseif (is_numeric($diskFile))
42 {
43 $this->diskFileId = (int)$diskFile;
44 }
45 if ($chatId)
46 {
47 $this->setChatId($chatId);
48 }
49 }
50
51 public static function getRestEntityName(): string
52 {
53 return 'file';
54 }
55
56 public static function initByDiskFileId(int $diskFileId, ?int $chatId = null): ?self
57 {
59
60 if ($diskFile === null)
61 {
62 return null;
63 }
64
65 return new static($diskFile, $chatId);
66 }
67
68 public function setDiskFile(File $diskFile): self
69 {
70 $this->diskFile = $diskFile;
71 $this->diskFileId = $diskFile->getId();
72
73 return $this;
74 }
75
76 public function getDiskFile(): ?File
77 {
78 if (!$this->diskFile instanceof File)
79 {
80 $this->diskFile = File::getById($this->diskFileId);
81 }
82
83 return $this->diskFile;
84 }
85
86 public function getDiskFileId(): int
87 {
88 if ($this->diskFileId)
89 {
90 return $this->diskFileId;
91 }
92
93 return $this->getDiskFile()->getId();
94 }
95
96 public function getChatId(): ?int
97 {
98 return $this->chatId;
99 }
100
101 public function setChatId(?int $chatId): self
102 {
103 $this->chatId = $chatId;
104 return $this;
105 }
106
107 public function getCopy(?Storage $storage = null): ?self
108 {
109 $userId = $this->getContext()->getUserId();
110 $storage = $storage ?? Driver::getInstance()->getStorageByUserId($userId);
111
112 if ($storage === null)
113 {
114 return null;
115 }
116
117 $folder = $storage->getFolderForUploadedFiles();
118
119 if ($folder === null)
120 {
121 return null;
122 }
123
124 $diskFile = $this->getDiskFile();
125
126 if ($diskFile === null)
127 {
128 return null;
129 }
130
131 $copy = $diskFile->copyTo($folder, $userId, true);
132
133 if (!$copy instanceof File)
134 {
135 return null;
136 }
137
138 return new static($copy, $this->getChatId());
139 }
140
141 public function getCopyToChat(Chat $chat): ?self
142 {
143 if (!Loader::includeModule('disk'))
144 {
145 return null;
146 }
147
148 $folder = $chat->getOrCreateDiskFolder();
149 $diskFile = $this->getDiskFile();
150
151 if (!($folder instanceof Folder) || $diskFile === null)
152 {
153 return null;
154 }
155
156 $newFileModel = $diskFile->copyTo($folder, $chat->getContext()->getUserId(), true);
157
158 if (!($newFileModel instanceof File))
159 {
160 return null;
161 }
162
163 if ($diskFile->getCode() === \Bitrix\Im\V2\Link\File\FileItem::MEDIA_ORIGINAL_CODE)
164 {
165 $newFileModel->changeCode(\Bitrix\Im\V2\Link\File\FileItem::MEDIA_ORIGINAL_CODE);
166 }
167
168 $newFileModel->increaseGlobalContentVersion();
169
170 return new static($newFileModel, $chat->getId());
171 }
172
173 public function getPopupData(array $excludedList = []): PopupData
174 {
175 return new PopupData([new UserPopupItem([$this->getDiskFile()->getCreatedBy()])], $excludedList);
176 }
177
178 public function toRestFormat(array $option = []): array
179 {
180 $diskFile = $this->getDiskFile();
181 return [
182 'id' => (int)$diskFile->getId(),
183 'chatId' => (int)$this->getChatId(),
184 'date' => $diskFile->getCreateTime()->format('c'),
185 'type' => $this->getContentType(),
186 'name' => $diskFile->getName(),
187 'extension' => mb_strtolower($diskFile->getExtension()),
188 'size' => (int)$diskFile->getSize(),
189 'image' => $this->getPreviewSizes() ?? false,
190 'status' => $diskFile->getGlobalContentVersion() > 1? 'done': 'upload',
191 'progress' => $diskFile->getGlobalContentVersion() > 1? 100: -1,
192 'authorId' => (int)$diskFile->getCreatedBy(),
193 'authorName' => \Bitrix\Im\User::formatFullNameFromDatabase($diskFile->getCreateUser()),
194 'urlPreview' => $this->getPreviewLink(),
195 'urlShow' => $this->getShowLink(),
196 'urlDownload' => $this->getDownloadLink(),
197 'viewerAttrs' => $this->getViewerAttributes(),
198 ];
199 }
200
206 public function getContentType(): string
207 {
208 if (isset($this->contentType))
209 {
210 return $this->contentType;
211 }
212
213 if ($this->getDiskFile()->getCode() === \Bitrix\Im\V2\Link\File\FileItem::MEDIA_ORIGINAL_CODE)
214 {
215 return 'file';
216 }
217
218 $diskTypeFile = $this->getDiskFile()->getTypeFile();
219
220 switch ($diskTypeFile)
221 {
222 case TypeFile::IMAGE:
223 return 'image';
224 case TypeFile::VIDEO:
225 return 'video';
226 case TypeFile::AUDIO:
227 return 'audio';
228 default:
229 return 'file';
230 }
231 }
232
233 public function setContentType(string $contentType): self
234 {
235 $this->contentType = $contentType;
236
237 return $this;
238 }
239
240 private function getPreviewSizes(): ?array
241 {
242 $previewParameters = [];
243 $diskFile = $this->getDiskFile();
244
245 if (TypeFile::isImage($diskFile))
246 {
247 $previewParameters = $diskFile->getFile();
248 }
249 if (TypeFile::isVideo($diskFile->getName()))
250 {
251 $previewParameters = $diskFile->getView()->getPreviewData();
252 }
253
254 if (empty($previewParameters))
255 {
256 return null;
257 }
258
259 return [
260 'height' => (int)$previewParameters['HEIGHT'],
261 'width' => (int)$previewParameters['WIDTH'],
262 ];
263 }
264
265 private function getPreviewLink(): string
266 {
267 $urlManager = UrlManager::getInstance();
268 $diskFile = $this->getDiskFile();
269
270 if ($diskFile->getView()->getPreviewData())
271 {
272 $linkType = 'disk.api.file.showPreview';
273 $fileName = 'preview.jpg';
274 }
275 elseif (TypeFile::isImage($diskFile))
276 {
277 $linkType = 'disk.api.file.showImage';
278 $fileName = $diskFile->getName();
279 }
280 else
281 {
282 return '';
283 }
284
285 return \Bitrix\Im\Common::getPublicDomain() . $urlManager->create($linkType, [
286 'humanRE' => 1,
287 'width' => 640,
288 'height' => 640,
289 'signature' => \Bitrix\Disk\Security\ParameterSigner::getImageSignature($diskFile->getId(), 640, 640),
290 'fileId' => $diskFile->getId(),
291 'fileName' => $fileName
292 ])->getUri();
293 }
294
295 private function getShowLink(): string
296 {
297 $urlManager = UrlManager::getInstance();
298 $diskFile = $this->getDiskFile();
299
300 if (TypeFile::isImage($diskFile))
301 {
302 $linkType = 'disk.api.file.showImage';
303 }
304 else
305 {
306 $linkType = 'disk.api.file.download';
307 }
308
309 return \Bitrix\Im\Common::getPublicDomain() . $urlManager->create($linkType, [
310 'humanRE' => 1,
311 'fileId' => $diskFile->getId(),
312 'fileName' => $diskFile->getName()
313 ])->getUri();
314 }
315
316 private function getDownloadLink(): string
317 {
318 $urlManager = UrlManager::getInstance();
319 $diskFile = $this->getDiskFile();
320
321 return \Bitrix\Im\Common::getPublicDomain() . $urlManager->create('disk.api.file.download', [
322 'humanRE' => 1,
323 'fileId' => $diskFile->getId(),
324 'fileName' => $diskFile->getName()
325 ])->getUri();
326 }
327
328 private function getViewerAttributes(): ?array
329 {
330 $diskFile = $this->getDiskFile();
331 try
332 {
333 $viewerType = FileAttributes::buildByFileData($diskFile->getFile() ?? [], $this->getDownloadLink())
334 ->setObjectId($diskFile->getId())
335 ->setGroupBy($this->getChatId() ?? $diskFile->getParentId())
336 ->setAttribute('data-im-chat-id', $this->getChatId())
337 ->setTitle($diskFile->getName())
338 ->addAction([
339 'type' => 'download',
340 ])
341 ->addAction([
342 'type' => 'copyToMe',
343 'text' => Loc::getMessage('IM_FILE_ITEM_ACTION_SAVE_TO_OWN_FILES'),
344 'action' => 'BXIM.disk.saveToDiskAction',
345 'params' => [
346 'fileId' => $diskFile->getId(),
347 ],
348 'extension' => 'disk.viewer.actions',
349 'buttonIconClass' => 'ui-btn-icon-cloud',
350 ])
351 ;
352
353 if ($viewerType->getTypeClass() === FileAttributes::JS_TYPE_CLASS_ONLYOFFICE)
354 {
355 $viewerType->setTypeClass('BX.Messenger.Integration.Viewer.OnlyOfficeChatItem');
356 if (
357 $diskFile->getCode() === CreateDocumentByCallTemplateScenario::CODE_RESUME
358 || $diskFile->getRealObject()->getCode() === CreateDocumentByCallTemplateScenario::CODE_RESUME
359 )
360 {
361 $viewerType->setTypeClass('BX.Messenger.Integration.Viewer.OnlyOfficeResumeItem');
362 }
363
364 $viewerType->setExtension('im.integration.viewer');
365 }
366 if ($viewerType->getViewerType() !== \Bitrix\Main\UI\Viewer\Renderer\Renderer::JS_TYPE_UNKNOWN)
367 {
368 return $viewerType->toDataSet();
369 }
370 }
371 catch (\Bitrix\Main\ArgumentException $exception)
372 {
373 return null;
374 }
375
376 return null;
377 }
378
379 public function getId(): int
380 {
381 return $this->getDiskFileId();
382 }
383}
static getById(int $id)
getCopy(?Storage $storage=null)
Definition FileItem.php:107
getPopupData(array $excludedList=[])
Definition FileItem.php:173
setContentType(string $contentType)
Definition FileItem.php:233
static initByDiskFileId(int $diskFileId, ?int $chatId=null)
Definition FileItem.php:56
__construct($diskFile, ?int $chatId=null)
Definition FileItem.php:35
getExtension()
Definition fileentry.php:12
getName()
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29