• Модуль: disk
  • Путь к файлу: ~/bitrix/modules/disk/lib/bitrix24disk/uploadfilemanager.php
  • Класс: BitrixDiskBitrix24DiskUploadFileManager
  • Вызов: UploadFileManager::upload
public function upload($filename, array $fileData)
{
	$tmpFileClass = $this->tmpFileClass;
	list($startRange, $endRange) = $this->getContentRange();
	$fileSize = $this->getFileSize();

	if(
		$startRange === null ||
		(
			$this->isValidChunkSize($startRange, $endRange, $fileData) &&
			$startRange == 0 &&
			($endRange - $startRange + 1) == $fileSize
		)
	)
	{
		$fileData['name'] = $filename;
		list($fileData['width'], $fileData['height']) = CFile::getImageSize($fileData['tmp_name']);

		$tmpFile = $tmpFileClass::createFromFileArray(
			$fileData,
			array(
				'CREATED_BY' => $this->getUserId(),
				'SIZE' => $fileSize,
			),
			$this->errorCollection
		);
		if(!$tmpFile)
		{
			$this->errorCollection->addOne(
				new Error(
					"Could not create tmpFile model (simple mode)",
					self::ERROR_CREATE_TMP_FILE_NON_BUCKET
				)
			);
			return false;
		}
		$this->token = $tmpFile->getToken();

		return true;
	}

	if($this->isInvalidChunkSize($startRange, $endRange, $fileData))
	{
		$this->errorCollection->addOne(
			new Error(
				'Size of file: ' . $fileData['size'] . ' not equals size of chunk: ' . ($endRange - $startRange + 1),
				self::ERROR_CHUNK_ERROR
			)
		);
		return false;
	}

	if($this->isFirstChunk($startRange))
	{
		$fileData['name'] = $filename;
		list($fileData['width'], $fileData['height']) = CFile::getImageSize($fileData['tmp_name']);

		//attempt to decide: cloud? not cloud?
		$bucket = $this->findBucketForFile(array(
			'name' => $filename,
			'fileSize' => $fileSize,
		));
		if($bucket)
		{
			/** @var TmpFile $tmpFile */
			$tmpFile = $tmpFileClass::createInBucketFirstPartFromFileArray(
				$fileData,
				array(
					'CREATED_BY' => $this->getUserId(),
					'SIZE' => $fileSize,
				),
				$bucket,
				compact('startRange', 'endRange', 'fileSize'),
				$this->errorCollection
			);
			if(!$tmpFile)
			{
				$this->errorCollection->addOne(
					new Error(
						"Could not create tmpFile model (cloud bucket {$bucket->ID})",
						self::ERROR_CREATE_TMP_FILE_BUCKET
					)
				);
				return false;
			}
		}
		else
		{
			$tmpFile = $tmpFileClass::createFromFileArray($fileData, array(
				'CREATED_BY' => $this->getUserId(),
				'SIZE' => $fileSize,
			), $this->errorCollection);
			if(!$tmpFile)
			{
				$this->errorCollection->addOne(
					new Error(
						"Could not create tmpFile model (simple mode)",
						self::ERROR_CREATE_TMP_FILE_NON_BUCKET
					)
				);
				return false;
			}
		}
		$this->token = $tmpFile->getToken();
		return true;
	}

	//if run resumable upload we needed token.
	if(!$this->hasToken())
	{
		$this->errorCollection->addOne(
			new Error(
				"Could not append content to file. Have to set token parameter.",
				self::ERROR_EMPTY_TOKEN
			)
		);
		return false;
	}
	$tmpFile = $this->findUserSpecificTmpFileByToken();
	if(!$tmpFile)
	{
		$this->errorCollection->addOne(
			new Error(
				"Could not find file by token",
				self::ERROR_UNKNOWN_TOKEN
			)
		);
		return false;
	}

	$success = $tmpFile->append(IOFile::getFileContents($fileData['tmp_name']), compact(
		'startRange', 'endRange', 'fileSize'
	));
	if(!$success)
	{
		$this->errorCollection->add($tmpFile->getErrors());
		return false;
	}

	$this->token = $tmpFile->getToken();
	return true;
}