Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
base.php
1<?php
2
4
11use Bitrix\Disk\SystemUser;
12use Bitrix\Disk\Internals\FolderTable;
13use Bitrix\Disk\Internals\ObjectTable;
14use Bitrix\Disk\Driver;
18
24class Base extends ProviderBase
25{
26 private $storage;
27 private $folderFilter = [];
28
33 public function __construct(array $setting)
34 {
35 parent::__construct($setting);
36 if (!Loader::includeModule('disk'))
37 {
38 throw new SystemException(
39 'Can\'t include module disk',
40 'ERROR_INCLUDE_DISK'
41 );
42 }
43
44 if (!$this->loadStorage())
45 {
46 throw new SystemException(
47 'Can\'t load storage',
48 'ERROR_LOAD_STORAGE'
49 );
50 }
51 }
52
62 public function addContent(string $code, $content, $type = false): bool
63 {
64 $result = false;
65
66 $content = $this->packageContent($content);
67 if (is_null($content))
68 {
69 return $result;
70 }
71
73
74 $folder = $this->getFolder(true);
75 if (!$folder)
76 {
77 if ($type !== false)
78 {
79 $subFolder = $folder->getChild(
80 [
81 '=NAME' => $type,
82 '=TYPE' => FolderTable::TYPE_FOLDER,
83 ]
84 );
85 if (!$subFolder)
86 {
87 $folder = $folder->addSubFolder(
88 [
89 'NAME' => $type,
90 'CREATED_BY' => SystemUser::SYSTEM_USER_ID,
91 ]
92 );
93 }
94 else
95 {
96 $folder = $subFolder;
97 }
98 }
99
100 if ($folder)
101 {
102 $file = $folder->uploadFile(
103 [
104 'name' => $name,
105 'content' => $content,
106 'type' => 'application/json',
107 ],
108 [
109 'NAME' => $name,
110 'CREATED_BY' => SystemUser::SYSTEM_USER_ID,
111 ]
112 );
113 if ($file)
114 {
115 $result = true;
116 }
117 }
118 }
119
120 return $result;
121 }
122
131 public function get(string $path, int $step): ?array
132 {
133 $result = null;
134 $folder = $this->getFolder();
135 if ($folder)
136 {
137 $folder = $folder->getChild(
138 [
139 '=NAME' => $path,
140 ]
141 );
142 }
143
144 if ($folder)
145 {
146 $fakeSecurityContext = Driver::getInstance()->getFakeSecurityContext();
147 $fileList = $folder->getChildren(
148 $fakeSecurityContext,
149 [
150 'filter' => [
151 '=TYPE' => ObjectTable::TYPE_FILE,
152 ]
153 ]
154 );
155 $i = 0;
156 foreach ($fileList as $child)
157 {
158 if ($i == $step && $child instanceof \Bitrix\Disk\File)
159 {
160 $server = Application::getInstance()->getContext()->getServer();
161 $documentRoot = $server->getDocumentRoot();
162 $filePath = $documentRoot . \CFile::GetPath(
163 $child->getFileId()
164 );
165 $file = new File($filePath);
166 try
167 {
168 $result = [
169 'DATA' => $file->getContents(),
170 'FILE_NAME' => $child->getName(),
171 ];
172 }
173 catch (FileNotFoundException $exception)
174 {
175 $result = null;
176 }
177
178 break;
179 }
180 $i++;
181 }
182 $result['COUNT'] = count($fileList);
183 }
184
185 return $result;
186 }
187
194 public function addFiles(array $files): array
195 {
196 $result = [
197 'success' => false,
198 'result' => [],
199 ];
200
201 $structure = new Structure($this->getUserContext());
202
203 $storage = $this->getStorage();
204 if ($storage)
205 {
206 $folder = $this->getFolder();
207 if (!$folder)
208 {
209 $folder = $storage->addFolder(
210 [
211 'NAME' => $this->getContext(),
212 'CREATED_BY' => SystemUser::SYSTEM_USER_ID,
213 ]
214 );
215 }
216
217 $subFolder = $folder->getChild(
218 [
220 '=TYPE' => FolderTable::TYPE_FOLDER,
221 ]
222 );
223 if (!$subFolder)
224 {
225 $subFolder = $folder->addSubFolder(
226 [
228 'CREATED_BY' => SystemUser::SYSTEM_USER_ID,
229 ]
230 );
231 }
232
233 if ($subFolder)
234 {
235 foreach ($files as $file)
236 {
237 if ($file['ID'])
238 {
239 $id = (int) $file['ID'];
240 $structure->saveFile($id, $file);
241
242 $fileData = \CFile::MakeFileArray($id);
243 $res = $subFolder->uploadFile(
244 $fileData,
245 [
246 'NAME' => $id,
247 'CREATED_BY' => SystemUser::SYSTEM_USER_ID,
248 ]
249 );
250 if (!is_null($res))
251 {
252 $result['success'] = true;
253 $result['result'][$id] = true;
254 }
255 else
256 {
257 $result['result'][$id] = false;
258 }
259 }
260 }
261 }
262 }
263
264 return $result;
265 }
266
272 public function deleteFolder()
273 {
274 $folder = $this->getFolder();
275 if ($folder)
276 {
277 global $USER;
278 if (!$folder->deleteTree($USER->GetID()))
279 {
280 //$this->setErrors($folder->getErrors());//todo: add errors
281 }
282 }
283
284 return true;
285 }
286
292 public function setFolderFilter(array $filter)
293 {
294 $this->folderFilter = $filter;
295 }
296
303 public function getFolder(bool $autoCreate = false)
304 {
305 $filter = $this->folderFilter;
306 $folderName = $this->getContext();
307 if (empty($filter))
308 {
309 $filter = [
310 '=NAME' => $folderName,
311 '=TYPE' => FolderTable::TYPE_FOLDER
312 ];
313 }
314
315 $folder = $this->getStorage()->getChild($filter);
316 if (!$folder && $autoCreate)
317 {
318 $folder = $this->getStorage()->addFolder(
319 [
320 'NAME' => $folderName,
321 'CREATED_BY' => SystemUser::SYSTEM_USER_ID,
322 ]
323 );
324 }
325
326 return $folder;
327 }
328
329 private function loadStorage(): bool
330 {
331 $this->storage = Driver::getInstance()->addStorageIfNotExist(
332 Helper::getInstance()->getStorageBackupParam()
333 );
334
335 return !is_null($this->storage);
336 }
337
343 public function getStorage()
344 {
345 return $this->storage;
346 }
347}
addContent(string $code, $content, $type=false)
Definition base.php:62