• Модуль: disk
  • Путь к файлу: ~/bitrix/modules/disk/lib/document/myofficehandler.php
  • Класс: BitrixDiskDocumentMyOfficeHandler
  • Вызов: MyOfficeHandler::createFile
public function createFile(FileData $fileData)
{
	if(!$this->checkRequiredInputParams($fileData->toArray(), array(
		'name', 'src',
	)))
	{
		return null;
	}

	$fileName = $fileData->getName();
	$fileName = $this->convertToUtf8($fileName);
	$file = new IOFile(IOPath::convertPhysicalToLogical($fileData->getSrc()));

	$http = new HttpClient(array(
		'redirect' => false,
		'socketTimeout' => 10,
		'streamTimeout' => 30,
		'version' => HttpClient::HTTP_1_1,
	));
	$http->setHeader('X-co-auth-token', $this->getAccessToken());
	$http->setHeader('Content-type', $fileData->getMimeType());


	if(
		$http->post(
			$this->getApiUrlRoot() . "/files/upload",
			array(
				'file' => array(
					'filename' => $fileName,
					'resource' => $file->open('r'),
					'contentType' => TypeFile::getMimeTypeByFilename($fileName),
				),
				'conflictStrategy' => 'keep_both'
			),
			true
		) === false
	)
	{
		$errorString = implode('; ', array_keys($http->getError()));
		$this->errorCollection->add(array(
			new Error($errorString, self::ERROR_HTTP_FILE_INTERNAL)
		));
		return null;
	}

	if(!$this->checkHttpResponse($http))
	{
		return null;
	}

	$finalOutput = Json::decode($http->getResult());
	if($finalOutput === null)
	{
		$this->errorCollection->add(array(
			new Error('Could not decode response as json', self::ERROR_BAD_JSON)
		));
		return null;
	}
	$fileData
		->setId($finalOutput['file']['id'])
		->setLinkInService($this->getEditLink($finalOutput['links']))
		->setMetaData($this->normalizeMetadata($finalOutput))
	;

	return $fileData;
}