• Модуль: disk
  • Путь к файлу: ~/bitrix/modules/disk/lib/bitrix24disk/tmpfile.php
  • Класс: BitrixDiskBitrix24DiskTmpFile
  • Вызов: TmpFile::appendContentCloud
private function appendContentCloud($fileContent, $startRange, $endRange, $fileSize, string $contentType = null)
{
	$isLastChunk = ($endRange + 1) == $fileSize;
	$bucket = $this->getBucket();

	if(!$bucket)
	{
		return false;
	}

	if
	(
		($endRange - $startRange + 1) < $bucket->getService()->getMinUploadPartSize() && !$isLastChunk
	)
	{
		$this->errorCollection->addOne(
			new Error(
				"Could not append content. Size of chunk must be more than {$bucket->getService()->getMinUploadPartSize()} (if chunk is not last)",
				static::ERROR_CLOUD_APPEND_INVALID_CHUNK_SIZE
			)
		);

		return false;
	}

	$upload = new CCloudStorageUpload($this->path);
	if(!$upload->isStarted())
	{
		$contentType = $contentType ?? 'application/octet-stream';
		if(!$upload->start($bucket->ID, $fileSize, $contentType))
		{
			$this->errorCollection->addOne(
				new Error(
					"Could not start cloud upload",
					static::ERROR_CLOUD_START_UPLOAD
				)
			);

			return false;
		}
	}
	$success = false;
	if($fileContent === false)
	{
		$this->errorCollection->addOne(
			new Error(
				'Could not get file contents',
				static::ERROR_GET_FILE_CONTENTS
			)
		);

		return false;
	}

	if ($upload->getPos() == doubleval($endRange+1))
	{
		//we already have this portion of content. Seems to be like that
		if ($isLastChunk && !$upload->finish())
		{
			$this->errorCollection[] = new Error('Could not finish resumable upload',static::ERROR_CLOUD_FINISH_UPLOAD);

			return false;
		}

		return true;
	}

	$fails = 0;
	while ($upload->hasRetries())
	{
		if ($upload->next($fileContent))
		{
			$success = true;
			break;
		}
		$fails++;
	}
	if (!$success)
	{
		$this->errorCollection->addOne(
			new Error(
				"Could not upload part of file for {$fails} times",
				static::ERROR_CLOUD_UPLOAD_PART
			)
		);

		return false;
	}
	elseif($success && $isLastChunk)
	{
		if($upload->finish())
		{
			//we don't  inc and don't dec
			//$bucket->incFileCounter($fileSize);
		}
		else
		{
			$this->errorCollection->addOne(
				new Error(
					'Could not finish resumable upload',
					static::ERROR_CLOUD_FINISH_UPLOAD
				)
			);

			return false;
		}
		return true;
	}

	return $success;
}