• Модуль: clouds
  • Путь к файлу: ~/bitrix/modules/clouds/classes/general/storage.php
  • Класс: CCloudStorage
  • Вызов: CCloudStorage::ResizeImageFileCheck
static function ResizeImageFileCheck($obBucket, $path)
{
	global $DB;
	$io = CBXVirtualIo::GetInstance();

	$path = preg_replace("/^https?:/i", "", $path);
	$path = CCloudUtil::URLEncode($path, "UTF-8", true);
	$q = $DB->Query("
		select
			ID
			,ERROR_CODE
			,TO_PATH
			,FROM_PATH
			,PARAMS
			,".$DB->DateToCharFunction("TIMESTAMP_X", "FULL")." TIMESTAMP_X
		from b_clouds_file_resize
		where TO_PATH = '".$DB->ForSql($path)."'
	");
	$task = $q->Fetch();
	if (!$task)
		return false;

	//File in the Sky with Diamonds
	if ($task["ERROR_CODE"] == 9)
	{
		return true;
	}

	//Fatal error
	if ($task["ERROR_CODE"] >= 20)
	{
		return false;
	}

	//Recoverable error
	if ($task["ERROR_CODE"] >= 10 && $task["ERROR_CODE"] < 20)
	{
		if ((MakeTimeStamp($task["TIMESTAMP_X"]) + 300/*5min*/) > (time() + CTimeZone::GetOffset()))
			return false;
	}

	$DB->Query("
		UPDATE b_clouds_file_resize
		SET ERROR_CODE = '11'
		WHERE ID = ".$task["ID"]."
	");

	$tmpFile = CFile::MakeFileArray($task["FROM_PATH"]);
	// if (!is_array($tmpFile) || !file_exists($tmpFile["tmp_name"]))
	// {
	// 	$tmpFile = CFile::MakeFileArray(BitrixMainWebUri::urnEncode($task["FROM_PATH"], "UTF-8"));
	// }

	if (!is_array($tmpFile) || !file_exists($tmpFile["tmp_name"]))
	{
		$DB->Query("
			UPDATE b_clouds_file_resize
			SET ERROR_CODE = '22'
			WHERE ID = ".$task["ID"]."
		");
		return false;
	}

	$arResizeParams = unserialize($task["PARAMS"], ['allowed_classes' => false]);
	if (!is_array($arResizeParams))
	{
		$DB->Query("
			UPDATE b_clouds_file_resize
			SET ERROR_CODE = '23'
			WHERE ID = ".$task["ID"]."
		");
		return false;
	}

	$DB->Query("
		UPDATE b_clouds_file_resize
		SET ERROR_CODE = '14'
		WHERE ID = ".$task["ID"]."
	");

	$arSize = $arResizeParams[0];
	$resizeType = $arResizeParams[1];
	$arWaterMark = $arResizeParams[2];
	$jpgQuality = $arResizeParams[3];
	$arFilters = $arResizeParams[4];

	$from_path = $io->GetLogicalName($tmpFile["tmp_name"]);
	$to_path = BitrixMainTextEncoding::convertEncoding(rawurldecode($task["TO_PATH"]), "UTF-8", LANG_CHARSET);
	$to_path = CFile::GetTempName('', bx_basename($to_path));

	if (!CFile::ResizeImageFile($from_path, $to_path, $arSize, $resizeType, $arWaterMark, $jpgQuality, $arFilters))
	{
		$DB->Query("
			UPDATE b_clouds_file_resize
			SET ERROR_CODE = '25'
			WHERE ID = ".$task["ID"]."
		");
		return false;
	}

	$DB->Query("
		UPDATE b_clouds_file_resize
		SET ERROR_CODE = '16'
		WHERE ID = ".$task["ID"]."
	");

	$fileToStore = CFile::MakeFileArray($io->GetPhysicalName($to_path));
	if ($arResizeParams["type"] && !preg_match("/^image\//", $fileToStore["type"]))
	{
		$fileToStore["type"] = $arResizeParams["type"];
	}

	$baseURL = preg_replace("/^https?:/i", "", $obBucket->GetFileSRC("/"));
	$pathToStore = mb_substr($task["TO_PATH"], mb_strlen($baseURL) - 1);
	$pathToStore = BitrixMainTextEncoding::convertEncoding(rawurldecode($pathToStore), "UTF-8", LANG_CHARSET);
	if (!$obBucket->SaveFile($pathToStore, $fileToStore))
	{
		$DB->Query("
			UPDATE b_clouds_file_resize
			SET ERROR_CODE = '27'
			WHERE ID = ".$task["ID"]."
		");
		return false;
	}
	$obBucket->IncFileCounter($fileToStore["size"]);
	$DB->Query("
		UPDATE b_clouds_file_resize
		SET ERROR_CODE = '9'
		WHERE ID = ".$task["ID"]."
	");
	return true;
}