1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
fileuploader.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Integration\Disk;
4
5use Bitrix\Disk\Driver;
6use Bitrix\Disk\Ui\Text;
7use Bitrix\Main\Error;
8use Bitrix\Main\Loader;
9use Bitrix\Main\Result;
10
11final class FileUploader
12{
13 private const NEW_FILE_PREFIX = 'n';
14
15 private readonly int $userId;
16
17 public function __construct(int $userId)
18 {
19 $this->userId = $userId;
20 }
21
30 public function addFile(int $fileId): Result
31 {
32 $result = new Result();
33
34 if (!$this->isAvailable())
35 {
36 return $result->addError(new Error('Module disk not available'));
37 }
38
39 $storage = Driver::getInstance()->getStorageByUserId($this->userId);
40 if (!$storage)
41 {
42 return $result->addError(new Error('Storage not found'));
43 }
44
45 $folder = $storage->getFolderForUploadedFiles();
46 if (!$folder)
47 {
48 return $result->addError(new Error('Upload folder not found'));
49 }
50
51 if (!$folder->canAdd($storage->getSecurityContext($this->userId)))
52 {
53 return $result->addError(new Error('Access denied'));
54 }
55
56 $fileArray = \CFile::GetFileArray($fileId);
57
58 $addedFile = $folder->addFile(
59 [
60 'NAME' => Text::correctFilename($fileArray['ORIGINAL_NAME']),
61 'FILE_ID' => $fileId,
62 'CONTENT_PROVIDER' => null,
63 'SIZE' => $fileArray['FILE_SIZE'],
64 'CREATED_BY' => $this->userId,
65 'UPDATE_TIME' => null,
66 ],
67 [],
68 true,
69 );
70
71 if (!$addedFile)
72 {
73 return $result->addError(new Error('Error while uploading file'));
74 }
75
76 return $result->setData([
77 'FILE' => $addedFile,
78 'ATTACHMENT_ID' => self::NEW_FILE_PREFIX . $addedFile->getId(),
79 ]);
80 }
81
86 private function isAvailable(): bool
87 {
88 return Loader::includeModule('disk');
89 }
90}
Определения error.php:15
$result
Определения get_property_values.php:14
if(empty($decryptedData)) $storage
Определения quickway.php:270