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

	$accessToken = $this->getAccessToken();
	$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}");

	if(
		$http->get(
			self::API_URL_V2 . '/files/' . $fileData->getId() .
			'?' . http_build_query(array('fields' => 'embedLink'))
		) === false)
	{
		$errorString = implode('; ', array_keys($http->getError()));
		$this->errorCollection[] = new Error(
			$errorString, self::ERROR_HTTP_GET_METADATA
		);
		return null;
	}

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

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

	if(empty($file['embedLink']))
	{
		if($fileData->getMimeType() === 'application/pdf')
		{
			return "https://drive.google.com/file/d/{$fileData->getId()}/preview";
		}

		$this->errorCollection[] = new Error(
			'Could not find {embedLink} in response', self::ERROR_EMBED_FILE_LINK
		);
		return null;
	}

	return $file['embedLink'];
}