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

	$accessToken = $this->getAccessToken();

	$googleMimeType = static::getInternalMimeTypeByExtension(getFileExtension($fileData->getName()));
	$fileName = getFileNameWithoutExtension($fileData->getName());
	$fileName = $this->convertToUtf8($fileName);

	if(!$googleMimeType)
	{
		$this->errorCollection[] = new Error(
			"Unsupported file format with name {$fileData->getName()}", self::ERROR_UNSUPPORTED_FILE_FORMAT
		);
		return null;
	}

	$http = new HttpClient(array(
		'socketTimeout' => 10,
		'streamTimeout' => 30,
		'version' => HttpClient::HTTP_1_1,
	));
	$http->setHeader('Content-Type', 'application/json; charset=UTF-8');
	$http->setHeader('Authorization', "Bearer {$accessToken}");

	$postFields = "{"name":"{$fileName}","mimeType":"{$googleMimeType}"}";
	if($http->post(self::API_URL_V3 . '/files?fields=id,webViewLink', $postFields) === false)
	{
		$errorString = implode('; ', array_keys($http->getError()));
		$this->errorCollection[] = new Error(
			$errorString, self::ERROR_HTTP_CREATE_BLANK
		);
		return null;
	}

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

	$fileMetadata = Json::decode($http->getResult());
	if($fileMetadata === null)
	{
		$this->errorCollection[] = new Error(
			'Could not decode response as json', self::ERROR_BAD_JSON
		);
		return null;
	}

	if(empty($fileMetadata['id']) || empty($fileMetadata['webViewLink']))
	{
		$this->errorCollection[] = new Error(
			'Could not find id or webViewLink in response from Google.', self::ERROR_COULD_NOT_FIND_ID
		);
		return null;
	}

	$fileData->setLinkInService($fileMetadata['webViewLink']);
	$fileData->setId($fileMetadata['id']);

	//last signed user must delete file from google drive
	$this->insertPermission($fileData);

	return $fileData;
}