33 $error = \CFile::checkFile(
42 $commitOptions->isForceRandom(),
43 $commitOptions->isSkipExtension()
48 return $this->handleUploadError(
49 $uploadResult->addError(
new UploaderError(
'CHECK_FILE_FAILED', $error)),
56 if (!$validationResult->isSuccess())
58 return $this->handleUploadError($uploadResult->addErrors($validationResult->getErrors()),
$controller);
61 [
'width' => $width,
'height' => $height] = $validationResult->getData();
66 $uploadRequest->setWidth($chunk->
getWidth());
67 $uploadRequest->setHeight($chunk->
getHeight());
71 $canUploadResult = call_user_func([
$controller,
'canUpload'], $uploadRequest);
72 if (($canUploadResult instanceof CanUploadResult) && !$canUploadResult->isSuccess())
74 return $this->handleUploadError($uploadResult->addErrors($canUploadResult->getErrors()),
$controller);
76 else if (!is_bool($canUploadResult) || $canUploadResult ===
false)
78 return $this->handleUploadError(
85 if (!$createResult->isSuccess())
87 return $this->handleUploadError($uploadResult->addErrors($createResult->getErrors()),
$controller);
91 $tempFile = $createResult->getData()[
'tempFile'];
92 $uploadResult->setTempFile($tempFile);
96 if (!$uploadResult->isSuccess())
98 return $this->handleUploadError($uploadResult,
$controller);
105 return $this->handleUploadError(
111 $guid = $this->getGuidFromToken($token);
114 return $this->handleUploadError(
120 $tempFile = TempFileTable::getList([
123 '=UPLOADED' =>
false,
129 return $this->handleUploadError(
135 $uploadResult->setTempFile($tempFile);
136 $uploadResult->setToken($token);
138 $appendResult = $tempFile->append($chunk);
139 if (!$appendResult->isSuccess())
141 return $this->handleUploadError($uploadResult->addErrors($appendResult->getErrors()),
$controller);
145 if ($uploadResult->isSuccess() && $chunk->
isLast())
148 if (!$commitResult->isSuccess())
150 return $this->handleUploadError($uploadResult->addErrors($commitResult->getErrors()),
$controller);
153 $fileInfo = $this->createFileInfo($uploadResult->getToken());
154 $uploadResult->setFileInfo($fileInfo);
155 $uploadResult->setDone(
true);
158 if (!$uploadResult->isSuccess())
160 return $this->handleUploadError($uploadResult,
$controller);
164 return $uploadResult;
167 private function handleUploadError(UploadResult $uploadResult, UploaderController
$controller): UploadResult
171 if (!$uploadResult->isSuccess())
173 $tempFile = $uploadResult->getTempFile();
174 if ($tempFile !==
null && $tempFile->state !== State::DELETED)
180 return $uploadResult;
185 $guid = $tempFile->getGuid();
186 $salt = $this->getTokenSalt([$guid]);
189 return $signer->sign($guid, $salt);
192 private function getGuidFromToken(
string $token): ?string
194 $parts = explode(
'.', $token, 2);
195 if (count($parts) !== 2)
200 [$guid, $signature] = $parts;
201 if (empty($guid) || empty($signature))
206 $salt = $this->getTokenSalt([$guid]);
207 $signer =
new Signer();
209 if (!$signer->validate($guid, $signature, $salt))
217 private function getTokenSalt($params = []): string
226 : (string)\bitrix_sessid()
229 return md5(serialize(
250 [$bfileIds, $tempFileIds] = $this->splitIds($ids);
254 if ($fileOwnerships->count() > 0)
257 if ($controller->canView())
262 foreach ($fileOwnerships as $fileOwnership)
264 if ($fileOwnership->isOwn())
266 $loadResult = $this->loadFile($fileOwnership->getId());
270 $loadResult =
new LoadResult($fileOwnership->getId());
274 $results->add($loadResult);
279 if (count($tempFileIds) > 0)
281 foreach ($tempFileIds as $tempFileId)
283 $loadResult = $this->loadTempFile($tempFileId);
284 $results->add($loadResult);
300 [$bfileIds, $tempFileIds] = $this->splitIds($ids);
303 if (count($bfileIds) > 0)
311 foreach ($fileOwnerships as $fileOwnership)
313 $removeResult =
new RemoveResult($fileOwnership->getId());
314 if ($fileOwnership->isOwn())
323 $results->add($removeResult);
328 if (count($tempFileIds) > 0)
330 foreach ($tempFileIds as $tempFileId)
333 $results->add($removeResult);
335 $guid = $this->getGuidFromToken($tempFileId);
342 $tempFile = TempFileTable::getList([
361 foreach ($tempFileIds as $tempFileId)
363 if (!is_string($tempFileId) || empty($tempFileId))
369 $pendingFiles->add($pendingFile);
371 $guid = $this->getGuidFromToken($tempFileId);
379 $tempFile = TempFileTable::getList([
393 $pendingFile->setTempFile($tempFile);
396 return $pendingFiles;
399 private function loadFile(
int $fileId):
LoadResult
407 $fileInfo = $this->createFileInfo($fileId);
410 $result->setFile($fileInfo);
420 private function loadTempFile(
string $tempFileId): LoadResult
422 $result =
new LoadResult($tempFileId);
423 $guid = $this->getGuidFromToken($tempFileId);
429 $tempFile = TempFileTable::getList([
441 $fileInfo = $this->createFileInfo($tempFileId);
444 $result->setFile($fileInfo);
454 private function createFileInfo($fileId): ?FileInfo
460 $fileInfo->setDownloadUrl($downloadUrl);
461 if ($fileInfo->isImage())
464 if ($config->shouldTreatOversizeImageAsFile())
466 $treatImageAsFile = $config->shouldTreatImageAsFile($fileInfo);
467 $fileInfo->setTreatImageAsFile($treatImageAsFile);
470 if (!$fileInfo->shouldTreatImageAsFile())
474 $fileInfo->setPreviewUrl($previewUrl, $rectangle->getWidth(), $rectangle->getHeight());
482 private function splitIds(array $ids): array
486 foreach ($ids as $id)
490 $fileIds[] = (int)$id;
494 $tempFileIds[] = (string)$id;
498 return [$fileIds, $tempFileIds];