Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
downloadagent.php
1<?php
2
4
12
14{
15 protected const MAX_ATTEMPTS = 3;
16
17 public static function scheduleDownload(int $activityId, int $recordingId, $siteId = SITE_ID)
18 {
19 if ($activityId > 0 && $recordingId > 0)
20 {
21 $siteId = Application::getConnection()->getSqlHelper()->forSql($siteId);
22 \CAgent::AddAgent(
23 __CLASS__."::run({$activityId}, {$recordingId}, '{$siteId}');",
24 'socialservices',
25 'N',
26 3600,
27 '',
28 'Y',
29 \ConvertTimeStamp(time() + \CTimeZone::GetOffset(), 'FULL')
30 );
31 }
32 }
33
34 public static function run($activityId, $recordingId, $siteId = SITE_ID, $attempt = 0)
35 {
36 $recordingFields = ZoomMeetingRecordingTable::getRowById($recordingId);
37 if (!is_array($recordingFields))
38 {
39 return '';
40 }
41
42 $attachResult = static::attach($activityId, $recordingFields, $siteId);
43 if ($attachResult->isSuccess())
44 {
45 return '';
46 }
47
48 $attempt++;
49 if ($attempt > static::MAX_ATTEMPTS)
50 {
51 return '';
52 }
53 $siteId = Application::getConnection()->getSqlHelper()->forSql($siteId);
54 return static::class."::run({$activityId}, {$recordingId}, '{$siteId}', {$attempt});";
55 }
56
57 public static function attach($activityId, array $recordingFields, $siteId = SITE_ID)
58 {
59 $result = new Result();
60
61 $validSymbolsPattern = '/^[a-z0-9\-]+$/i';
62 if (!preg_match($validSymbolsPattern, $recordingFields['EXTERNAL_ID']))
63 {
64 return $result->addError(new Error('Can not create recording file name, external_id contains invalid symbols'));
65 }
66 if (!preg_match($validSymbolsPattern, $recordingFields['FILE_TYPE']))
67 {
68 return $result->addError(new Error('Can not create recording file name, external_id contains invalid symbols'));
69 }
70 if (!Loader::includeModule('crm'))
71 {
72 return $result->addError(new Error('CRM module is not installed'));
73 }
74 $fileName = mb_strtolower("{$recordingFields['EXTERNAL_ID']}.{$recordingFields['FILE_TYPE']}");
75 $downloadResult = static::download($recordingFields['DOWNLOAD_URL'], $recordingFields['DOWNLOAD_TOKEN'], $fileName);
76 if(!$downloadResult->isSuccess())
77 {
78 return $result->addErrors($downloadResult->getErrors());
79 }
80
81 $activityFields = \CCrmActivity::GetByID($activityId, false);
82 if(!$activityFields)
83 {
84 return $result->addError(new Error('Activity is not found'));
85 }
86 $responsible = $activityFields['RESPONSIBLE_ID'];
87
88 $tempPath = $downloadResult->getData()['file'];
89 $recordFile = \CFile::MakeFileArray($tempPath, $recordingFields['FILE_TYPE']);
90 $recordFile['MODULE_ID'] = 'crm';
91 $saveResult = DiskHelper::saveFile($recordFile, $responsible, $siteId);
92
93 if(!$saveResult->isSuccess())
94 {
95 return $result->addErrors($saveResult->getErrors());
96 }
97
98 $fileId = $saveResult->getData()['fileId'];
99 ZoomMeetingRecordingTable::update($recordingFields['ID'], [
100 'FILE_ID' => $fileId
101 ]);
102
103 $storageElementIds = \unserialize($activityFields['STORAGE_ELEMENT_IDS'], ['allowed_classes' => false]) ?: [];
104 $storageElementIds[] = $fileId;
105
106 $activityFields['STORAGE_TYPE_ID'] = \Bitrix\Crm\Integration\StorageType::Disk;
107 $activityFields['STORAGE_ELEMENT_IDS'] = $storageElementIds;
108
109 $updateResult = \CCrmActivity::Update($activityId, $activityFields, false);
110 if (!$updateResult)
111 {
112 return $result->addError(new Error(\CCrmActivity::GetLastErrorMessage()));
113 }
114 return $result;
115 }
116
117 public static function download(string $recordingUrl, string $downloadToken, string $fileName): Result
118 {
119 $result = new Result();
120
121 $tempPath = \CFile::GetTempName('', $fileName);
122 IO\Directory::createDirectory(IO\Path::getDirectory($tempPath));
123 if(IO\Directory::isDirectoryExists(IO\Path::getDirectory($tempPath)) === false)
124 {
125 return $result->addError(new Error("Error creating temporary directory {$tempPath}"));
126 }
127 $tempFile = new IO\File($tempPath);
128 $handler = $tempFile->open("w+");
129 if($handler === false)
130 {
131 return $result->addError(new Error("Error opening temporary file {$tempPath}"));
132 }
133
134 $http = new HttpClient();
135 $http->setHeader("Authorization", "Bearer {$downloadToken}");
136 $http->setOutputStream($handler);
137 $http->query("GET", $recordingUrl);
138 $statusCode = $http->getStatus();
139 if($statusCode !== 200)
140 {
141 return $result->addError(new Error("Service response status code is {$statusCode}"));
142 }
143
144 $http->getResult();
145 $tempFile->close();
146
147 return $result->setData([
148 'file' => $tempPath
149 ]);
150 }
151}
static getConnection($name="")
static isDirectoryExists($path)
static getDirectory($path)
Definition path.php:109
static scheduleDownload(int $activityId, int $recordingId, $siteId=SITE_ID)
static run($activityId, $recordingId, $siteId=SITE_ID, $attempt=0)
static download(string $recordingUrl, string $downloadToken, string $fileName)
static attach($activityId, array $recordingFields, $siteId=SITE_ID)