1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
storage_upload.php
См. документацию.
1<?php
2/*.
3 require_module 'standard';
4 require_module 'pcre';
5 require_module 'hash';
6 require_module 'bitrix_main';
7 require_module 'bitrix_clouds_classes_storage_service';
8 require_module 'bitrix_clouds_classes_storage_bucket';
9.*/
10IncludeModuleLangFile(__FILE__);
11
13{
14 protected /*.string.*/ $_filePath = '';
15 protected /*.string.*/ $_ID = '';
16 protected /*.CCloudStorageBucket.*/ $obBucket;
17 protected /*.int.*/ $_max_retries = 3;
18 protected /*.array[string]string.*/ $_cache = null;
19
24 public function __construct($filePath)
25 {
26 $this->_filePath = $filePath;
27 $this->_ID = '1' . mb_substr(md5($filePath), 1);
28 }
29
33 public function GetArray()
34 {
35 global $DB;
36
37 if (!isset($this->_cache))
38 {
39 $rs = $DB->Query("
40 SELECT *
41 FROM b_clouds_file_upload
42 WHERE ID = '" . $this->_ID . "'
43 ");
44 $this->_cache = $rs->Fetch();
45 }
46
47 return $this->_cache;
48 }
49
53 public function isStarted()
54 {
55 return is_array($this->GetArray());
56 }
57
61 public function Delete()
62 {
63 global $DB;
64 //TODO: clean up temp files in Clodo
65 $DB->Query("DELETE FROM b_clouds_file_upload WHERE ID = '" . $this->_ID . "'");
66 unset($this->_cache);
67 }
68
72 public function DeleteOld()
73 {
74 global $DB;
75 $DB->Query('DELETE FROM b_clouds_file_upload WHERE TIMESTAMP_X < ' . $DB->CharToDateFunction(ConvertTimeStamp(time() - 24 * 60 * 60)));
76 }
77
84 public function Start($bucket_id, $fileSize, $ContentType = 'binary/octet-stream', $tmpFileName = false)
85 {
86 global $DB;
87 global $APPLICATION;
88
89 if (is_object($bucket_id))
90 {
91 $obBucket = $bucket_id;
92 }
93 else
94 {
95 $obBucket = new CCloudStorageBucket(intval($bucket_id));
96 }
97
98 if (!$obBucket->Init())
99 {
100 return false;
101 }
102
103 if (!$this->isStarted())
104 {
105 $arUploadInfo = /*.(array[string]string).*/[];
106 $bStarted = $obBucket->getService()->InitiateMultipartUpload(
107 $obBucket->getBucketArray(),
108 $arUploadInfo,
109 $this->_filePath,
110 $fileSize,
111 $ContentType
112 );
113 if (!$bStarted && $obBucket->RenewToken())
114 {
115 $bStarted = $obBucket->getService()->InitiateMultipartUpload(
116 $obBucket->getBucketArray(),
117 $arUploadInfo,
118 $this->_filePath,
119 $fileSize,
120 $ContentType
121 );
122 }
123
124 if ($bStarted)
125 {
126 $bAdded = $DB->Add('b_clouds_file_upload', [
127 'ID' => $this->_ID,
128 '~TIMESTAMP_X' => $DB->CurrentTimeFunction(),
129 'FILE_PATH' => $this->_filePath,
130 'FILE_SIZE' => $fileSize,
131 'TMP_FILE' => $tmpFileName,
132 'BUCKET_ID' => intval($obBucket->ID),
133 'PART_SIZE' => $obBucket->getService()->GetMinUploadPartSize(),
134 'PART_NO' => 0,
135 'PART_FAIL_COUNTER' => 0,
136 'NEXT_STEP' => serialize($arUploadInfo),
137 ], ['NEXT_STEP']);
138 unset($this->_cache);
139
140 return $bAdded !== false;
141 }
142 else
143 {
144 $error = $obBucket->getService()->formatError();
145 if ($error)
146 {
147 $APPLICATION->ThrowException($error);
148 }
149 else
150 {
151 $APPLICATION->ThrowException(GetMessage('CLO_STORAGE_UPLOAD_ERROR', ['#errno#' => 6]));
152 }
153 }
154 }
155
156 return false;
157 }
158
163 public function Next($data, $obBucket = null)
164 {
165 global $APPLICATION;
166
167 if ($this->isStarted())
168 {
169 $ar = $this->GetArray();
170
171 if ($obBucket === null)
172 {
173 $obBucket = new CCloudStorageBucket(intval($ar['BUCKET_ID']));
174 }
175
176 if (!$obBucket->Init())
177 {
178 $APPLICATION->ThrowException(GetMessage('CLO_STORAGE_UPLOAD_ERROR', ['#errno#' => 1]));
179 return false;
180 }
181
182 $arUploadInfo = unserialize($ar['NEXT_STEP'], ['allowed_classes' => false]);
183 $bSuccess = $obBucket->getService()->UploadPart(
184 $obBucket->getBucketArray(),
185 $arUploadInfo,
186 $data
187 );
188
189 if (!$bSuccess)
190 {
191 $error = $obBucket->getService()->formatError();
192 if ($error)
193 {
194 $APPLICATION->ThrowException($error);
195 }
196 }
197
198 if (!$this->UpdateProgress($arUploadInfo, $bSuccess))
199 {
200 $APPLICATION->ThrowException(GetMessage('CLO_STORAGE_UPLOAD_ERROR', ['#errno#' => 2]));
201 return false;
202 }
203
204 return $bSuccess;
205 }
206
207 return false;
208 }
209
215 public function Part($data, $part_no, $obBucket = null)
216 {
217 global $APPLICATION;
218
219 if ($this->isStarted())
220 {
221 $ar = $this->GetArray();
222
223 if ($obBucket === null)
224 {
225 $obBucket = new CCloudStorageBucket(intval($ar['BUCKET_ID']));
226 }
227
228 if (!$obBucket->Init())
229 {
230 $APPLICATION->ThrowException(GetMessage('CLO_STORAGE_UPLOAD_ERROR', ['#errno#' => 3]));
231 return false;
232 }
233
234 $arUploadInfo = unserialize($ar['NEXT_STEP'], ['allowed_classes' => false]);
235 $bSuccess = $obBucket->getService()->UploadPartNo(
236 $obBucket->getBucketArray(),
237 $arUploadInfo,
238 $data,
239 $part_no
240 );
241
242 if (!$bSuccess)
243 {
244 $error = $obBucket->getService()->formatError();
245 if ($error)
246 {
247 $APPLICATION->ThrowException($error);
248 }
249 }
250
251 if (!$this->UpdateProgress($arUploadInfo, $bSuccess))
252 {
253 $APPLICATION->ThrowException(GetMessage('CLO_STORAGE_UPLOAD_ERROR', ['#errno#' => 5]));
254 return false;
255 }
256
257 return $bSuccess;
258 }
259
260 return false;
261 }
262
266 public function Finish($obBucket = null)
267 {
268 global $APPLICATION;
269
270 if ($this->isStarted())
271 {
272 $ar = $this->GetArray();
273
274 if ($obBucket === null)
275 {
276 $obBucket = new CCloudStorageBucket(intval($ar['BUCKET_ID']));
277 }
278 if (!$obBucket->Init())
279 {
280 return false;
281 }
282
283 $arUploadInfo = unserialize($ar['NEXT_STEP'], ['allowed_classes' => false]);
284 $bSuccess = $obBucket->getService()->CompleteMultipartUpload(
285 $obBucket->getBucketArray(),
286 $arUploadInfo
287 );
288
289 if ($bSuccess)
290 {
291 $this->Delete();
292
293 if ($obBucket->getQueueFlag())
294 {
295 CCloudFailover::queueCopy($obBucket, $this->_filePath);
296 }
297
298 foreach (GetModuleEvents('clouds', 'OnAfterCompleteMultipartUpload', true) as $arEvent)
299 {
300 ExecuteModuleEventEx($arEvent, [$obBucket, ['size' => $ar['FILE_SIZE']], $this->_filePath]);
301 }
302 }
303 else
304 {
305 $error = $obBucket->getService()->formatError();
306 if ($error)
307 {
308 $APPLICATION->ThrowException($error);
309 }
310 }
311
312 return $bSuccess;
313 }
314
315 return false;
316 }
317
321 public function GetPartCount()
322 {
323 $ar = $this->GetArray();
324
325 if (is_array($ar))
326 {
327 return intval($ar['PART_NO']);
328 }
329 else
330 {
331 return 0;
332 }
333 }
334
338 public function GetPos()
339 {
340 $ar = $this->GetArray();
341
342 if (is_array($ar))
343 {
344 return intval($ar['PART_NO']) * doubleval($ar['PART_SIZE']);
345 }
346 else
347 {
348 return 0;
349 }
350 }
351
355 public function getPartSize()
356 {
357 $ar = $this->GetArray();
358
359 if (is_array($ar))
360 {
361 return intval($ar['PART_SIZE']);
362 }
363 else
364 {
365 return 0;
366 }
367 }
368
372 public function hasRetries()
373 {
374 $ar = $this->GetArray();
375 return is_array($ar) && (intval($ar['PART_FAIL_COUNTER']) < $this->_max_retries);
376 }
377
381 public function getTempFileName()
382 {
383 $ar = $this->GetArray();
384 if (is_array($ar))
385 {
386 return $ar['TMP_FILE'];
387 }
388 else
389 {
390 return '';
391 }
392 }
393
399 protected function UpdateProgress($arUploadInfo, $bSuccess)
400 {
401 global $DB;
402
403 if ($bSuccess)
404 {
405 $arFields = [
406 'NEXT_STEP' => serialize($arUploadInfo),
407 '~PART_NO' => 'PART_NO + 1',
408 'PART_FAIL_COUNTER' => 0,
409 ];
410 $arBinds = [
411 'NEXT_STEP' => $arFields['NEXT_STEP'],
412 ];
413 }
414 else
415 {
416 $arFields = [
417 '~PART_FAIL_COUNTER' => 'PART_FAIL_COUNTER + 1',
418 ];
419 $arBinds = [
420 ];
421 }
422
423 $strUpdate = $DB->PrepareUpdate('b_clouds_file_upload', $arFields);
424 if ($strUpdate != '')
425 {
426 $strSql = 'UPDATE b_clouds_file_upload SET ' . $strUpdate . " WHERE ID = '" . $this->_ID . "'";
427 if (!$DB->QueryBind($strSql, $arBinds))
428 {
429 unset($this->_cache);
430 return false;
431 }
432 }
433
434 unset($this->_cache);
435 return true;
436 }
437
438 public static function CleanUp($ID = '')
439 {
441 $helper = $connection->getSqlHelper();
442 $rs = false;
443
444 if ($ID)
445 {
446 $rs = $connection->query("
447 SELECT ID, BUCKET_ID, NEXT_STEP
448 FROM b_clouds_file_upload
449 WHERE ID = '" . $helper->forSql($ID) . "'
450 ");
451 }
452 else
453 {
454 $days = COption::GetOptionInt('clouds', 'multipart_upload_keep_days');
455 if ($days > 0)
456 {
457 $rs = $connection->query('
458 SELECT ID, BUCKET_ID, NEXT_STEP
459 FROM b_clouds_file_upload
460 WHERE TIMESTAMP_X < ' . $helper->addDaysToDateTime(-$days)
461 );
462 }
463 }
464
465 if ($rs)
466 {
467 while ($arBucket = $rs->fetch())
468 {
469 $obBucket = new CCloudStorageBucket(intval($arBucket['BUCKET_ID']));
470 if ($obBucket->Init())
471 {
472 $arUploadInfo = unserialize($arBucket['NEXT_STEP'], ['allowed_classes' => false]);
473 $service = $obBucket->getService();
474 $service->CancelMultipartUpload($obBucket->getBucketArray(), $arUploadInfo);
475 }
476 $connection->query("DELETE FROM b_clouds_file_upload WHERE ID = '" . $helper->forSql($arBucket['ID']) . "'");
477 }
478 }
479 }
480}
$connection
Определения actionsdefinitions.php:38
global $APPLICATION
Определения include.php:80
static getConnection($name="")
Определения application.php:638
static queueCopy($obBucket, $FILE_PATH)
Определения failover.php:44
Finish($obBucket=null)
Определения storage_upload.php:266
GetPartCount()
Определения storage_upload.php:321
isStarted()
Определения storage_upload.php:53
GetArray()
Определения storage_upload.php:33
Start($bucket_id, $fileSize, $ContentType='binary/octet-stream', $tmpFileName=false)
Определения storage_upload.php:84
$_max_retries
Определения storage_upload.php:17
static CleanUp($ID='')
Определения storage_upload.php:438
UpdateProgress($arUploadInfo, $bSuccess)
Определения storage_upload.php:399
Next($data, $obBucket=null)
Определения storage_upload.php:163
DeleteOld()
Определения storage_upload.php:72
Part($data, $part_no, $obBucket=null)
Определения storage_upload.php:215
getPartSize()
Определения storage_upload.php:355
hasRetries()
Определения storage_upload.php:372
__construct($filePath)
Определения storage_upload.php:24
getTempFileName()
Определения storage_upload.php:381
$arFields
Определения dblapprove.php:5
$data['IS_AVAILABLE']
Определения .description.php:13
if($ajaxMode) $ID
Определения get_user.php:27
global $DB
Определения cron_frame.php:29
ExecuteModuleEventEx($arEvent, $arParams=[])
Определения tools.php:5214
GetModuleEvents($MODULE_ID, $MESSAGE_ID, $bReturnArray=false)
Определения tools.php:5177
IncludeModuleLangFile($filepath, $lang=false, $bReturnArray=false)
Определения tools.php:3778
GetMessage($name, $aReplace=null)
Определения tools.php:3397
$service
Определения payment.php:18
$ar
Определения options.php:199
$error
Определения subscription_card_product.php:20
$rs
Определения action.php:82