Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
FileService.php
1<?php
2
4
5use Bitrix\Disk\Driver;
6use Bitrix\Disk\File;
7use Bitrix\Disk\Security\DiskSecurityContext;
9use Bitrix\Im\V2\Common\ContextCustomer;
15
17{
18 use ContextCustomer;
19
20 protected const ADD_FILE_EVENT = 'fileAdd';
21 protected const DELETE_FILE_EVENT = 'fileDelete';
22
23 protected bool $isMigrationFinished;
24
25 public function __construct()
26 {
27 $this->isMigrationFinished = Option::get('im', 'im_link_file_migration', 'N') === 'Y';
28 }
29
35 public function saveFilesFromMessage(array $files, Message $message): Result
36 {
37 $result = new Result();
38
39 if (empty($files))
40 {
41 return $result;
42 }
43
44 $entities = new \Bitrix\Im\V2\Entity\File\FileCollection($files, $message->getChatId());
46 $links = FileCollection::linkEntityToMessage($entities, $message);
47
48 $saveResult = $this->saveFiles($links);
49
50 if ($links->count() === 0)
51 {
52 return $result;
53 }
54
55 if (!$saveResult->isSuccess())
56 {
57 $result->addErrors($saveResult->getErrors());
58 }
59
60 if ($saveResult->isSuccess())
61 {
62 foreach ($links as $link)
63 {
65 ->setContext($this->context)
66 ->sendFull($link, self::ADD_FILE_EVENT, ['CHAT_ID' => $link->getChatId()])
67 ;
68 }
69 }
70
71 return $result;
72 }
73
74 protected function saveFiles(FileCollection $fileCollection): Result
75 {
76 return $fileCollection->save();
77 }
78
79 public function deleteFilesByDiskFileId(int $diskFileId): Result
80 {
81 $result = new Result();
82
83 $link = FileItem::getByDiskFileId($diskFileId);
84
85 if ($link === null)
86 {
87 return $result;
88 }
89
90 $deleteResult = $link->delete();
91
92 if (!$deleteResult->isSuccess())
93 {
94 return $result->addErrors($deleteResult->getErrors());
95 }
96
97 if (!$this->isMigrationFinished)
98 {
99 return $result;
100 }
101
103 ->setContext($this->context)
104 ->sendIdOnly($link, self::DELETE_FILE_EVENT, ['CHAT_ID' => $link->getChatId()])
105 ;
106
107 return $result;
108 }
109
110 public function isMigrationFinished(): bool
111 {
113 }
114
115 public function getFilesBeforeMigrationFinished(int $chatId, int $limit, ?int $lastId = null, ?string $filename = null): \Bitrix\Im\V2\Entity\File\FileCollection
116 {
117 $folderModel = \CIMDisk::getFolderModel($chatId, false);
118 if ($folderModel === false)
119 {
120 return new \Bitrix\Im\V2\Entity\File\FileCollection();
121 }
122 $relation = \CIMChat::GetRelationById($chatId, $this->getContext()->getUserId(), true, false);
123 $filter = Query::filter()
124 ->where('PARENT_ID', $folderModel->getId())
125 ->where('STORAGE_ID', $folderModel->getStorageId())
126 ->where('ID', '>', $relation['LAST_FILE_ID'])
127 ;
128 if (isset($lastId))
129 {
130 $filter->where('ID', '<', $lastId);
131 }
132 if (isset($filename))
133 {
134 $clearFileName = str_replace("%", '', $filename);
135 $filter->whereLike('NAME', "$clearFileName%");
136 }
137 $parameters = [
138 'filter' => $filter,
139 'with' => ['CREATE_USER'],
140 'limit' => $limit,
141 'order' => ['ID' => 'DESC']
142 ];
143 $securityContext = new DiskSecurityContext($this->getContext()->getUserId());
144 $parameters = Driver::getInstance()->getRightsManager()->addRightsCheck($securityContext, $parameters, ['ID', 'CREATED_BY']);
145 $diskFiles = File::getModelList($parameters);
146
147 return new \Bitrix\Im\V2\Entity\File\FileCollection($diskFiles, $chatId);
148 }
149}