• Модуль: ui
  • Путь к файлу: ~/bitrix/modules/ui/lib/FileUploader/Chunk.php
  • Класс: BitrixUIFileUploaderChunk
  • Вызов: Chunk::createFromRequest
static function createFromRequest(HttpRequest $request): Result
{
	$result = new Result();

	$fileMimeType = (string)$request->getHeader('Content-Type');
	if (!preg_match('~w+/[-+.w]+~', $fileMimeType))
	{
		return $result->addError(new UploaderError(UploaderError::INVALID_CONTENT_TYPE));
	}

	$contentLength = $request->getHeader('Content-Length');
	if ($contentLength === null)
	{
		return $result->addError(new UploaderError(UploaderError::INVALID_CONTENT_LENGTH));
	}

	$contentLength = (int)$contentLength;
	$filename = static::normalizeFilename((string)$request->getHeader('X-Upload-Content-Name'));
	if (empty($filename))
	{
		return $result->addError(new UploaderError(UploaderError::INVALID_CONTENT_NAME));
	}

	if (!static::isValidFilename($filename))
	{
		return $result->addError(new UploaderError(UploaderError::INVALID_FILENAME));
	}

	$contentRangeResult = static::getContentRange($request);
	if (!$contentRangeResult->isSuccess())
	{
		return $result->addErrors($contentRangeResult->getErrors());
	}

	$file = static::getFileFromHttpInput();
	$contentRange = $contentRangeResult->getData();
	$rangeChunkSize = empty($contentRange) ? 0 : ($contentRange['endRange'] - $contentRange['startRange'] + 1);

	if ($rangeChunkSize && $contentLength !== $rangeChunkSize)
	{
		return $result->addError(new UploaderError(
			UploaderError::INVALID_RANGE_SIZE,
			[
				'rangeChunkSize' => $rangeChunkSize,
				'contentLength' => $contentLength,
			]
		));
	}

	$chunk = new Chunk($file);
	if ($chunk->getSize() !== $contentLength)
	{
		return $result->addError(new UploaderError(
			UploaderError::INVALID_CHUNK_SIZE,
			[
				'chunkSize' => $chunk->getSize(),
				'contentLength' => $contentLength,
			]
		));
	}

	$chunk->setName($filename);
	$chunk->setType($fileMimeType);

	if (!empty($contentRange))
	{
		$chunk->setStartRange($contentRange['startRange']);
		$chunk->setEndRange($contentRange['endRange']);
		$chunk->setFileSize($contentRange['fileSize']);
	}

	$result->setData(['chunk' => $chunk]);

	return $result;
}