• Модуль: clouds
  • Путь к файлу: ~/bitrix/modules/clouds/lib/filehash.php
  • Класс: BitrixCloudsFileHashTable
  • Вызов: FileHashTable::prepareDuplicates
static function prepareDuplicates($bucketId, &$fileIds)
{
	$originalId = false;
	if ($fileIds)
	{
		//Exclude any id that is already a duplicate
		$duplicates = BitrixMainFileInternalFileDuplicateTable::getList([
			'select' => ['DUPLICATE_ID'],
			'filter' => [
				'=DUPLICATE_ID' => $fileIds,
			],
		]);
		while ($duplicate = $duplicates->fetch())
		{
			//Others will be excluded from the process
			$p = array_search($duplicate['DUPLICATE_ID'], $fileIds);
			if ($p !== false)
			{
				unset($fileIds[$p]);
			}
		}
	}

	if ($fileIds)
	{
		//Search throught file id for any existing originals
		$originals = BitrixMainFileInternalFileDuplicateTable::getList([
			'select' => ['ORIGINAL_ID'],
			'filter' => [
				'=ORIGINAL_ID' => $fileIds,
			],
			'order' => ['ORIGINAL_ID' => 'ASC'],
		]);
		while ($original = $originals->fetch())
		{
			//First will be used for future deduplication
			if ($originalId === false)
			{
				$originalId = $original['ORIGINAL_ID'];
			}

			//Others will be excluded from the process
			$p = array_search($original['ORIGINAL_ID'], $fileIds);
			if ($p !== false)
			{
				unset($fileIds[$p]);
			}
		}
		//None originals exists yet
		if ($originalId === false)
		{
			$originalId = array_shift($fileIds);
		}
	}

	return $originalId;
}