• Модуль: disk
  • Путь к файлу: ~/bitrix/modules/disk/lib/bitrix24disk/legacy/storagecontroller.php
  • Класс: BitrixDiskBitrix24DiskLegacyStorageController
  • Вызов: StorageController::processActionUpdate
protected function processActionUpdate()
{
	$this->checkRequiredPostParams(array('storageExtra', 'storageId', 'name'));
	if($this->errorCollection->hasErrors())
	{
		$this->sendJsonErrorResponse();
	}

	$tmpFile = $parentFolderId = $targetSectionId = $elementId = null;
	$storage = $this->getStorageObject($this->request->getPost('storageExtra'), $this->request->getPost('storageId'));
	$filename = $this->request->getPost('name');
	$token = $this->request->getPost('token');
	$inRoot = $this->request->getPost('inRoot') == 'true';
	$isUpdate = $this->request->getPost('update') == 'true';

	if($token)
	{
		$tmpFileManager = new Bitrix24DiskUploadFileManager();
		$tmpFileManager
			->setToken($token)
			->setUser($this->getUser())
		;
		$tmpFile = $tmpFileManager->findUserSpecificTmpFileByToken();
		if(!$tmpFile)
		{
			throw new SystemException('Not found file by token');
		}
		$tmpFile->registerDelayedDeleteOnShutdown();
	}

	if(!$storage->isCorrectName($filename, $msg))
	{
		$tmpFile && ($tmpFile->delete());
		$this->sendJsonResponse(array(
			'status' => static::STATUS_DENIED,
			'message' => $msg,
		));
	}

	if($inRoot)
	{
		$storageExtra = $storage->getStorageExtra();
		$targetSectionId = $storageExtra['sectionId'];
		$parentFolderId = $storageExtra['sectionId'];
	}
	else
	{
		$this->checkRequiredPostParams(array('parentExtra'));
		if($this->errorCollection->hasErrors())
		{
			$this->sendJsonErrorResponse();
		}

		$parentFolderExtra = $storage->parseElementExtra($this->request->getPost('parentExtra'));
		$parentFolderId = $parentFolderExtra['id'];

		$folder = Folder::loadById($parentFolderId);
		if($folder && $folder->isLink())
		{
			$parentFolderId = $folder->getRealObjectId();
		}
	}

	if($isUpdate)
	{
		$this->checkRequiredPostParams(array('id', 'version'));
		if($this->errorCollection->hasErrors())
		{
			$this->sendJsonErrorResponse();
		}

		$version = $this->request->getPost('version');
		$fileExtra = $storage->parseElementExtra($this->request->getPost('extra'));
		$elementId = $fileExtra['id'];

		$file = $storage->getFile($this->request->getPost('id'), $fileExtra);
		if(empty($file))
		{
			$this->sendJsonResponse(array(
				'status' => static::STATUS_NOT_FOUND,
			));
		}
		if($storage->compareVersion($file['version'], $version) > 0)
		{
			$file['status'] = static::STATUS_OLD_VERSION;
			$this->sendJsonResponse($file);
		}

		//todo simple check for move/rename
		if($filename != $file['extra']['name'] || $parentFolderId != $file['extra']['sectionId'])
		{
			if(!$storage->isUnique($filename, $parentFolderId, $opponentId))
			{
				$opponentFile = array();
				if($opponentId)
				{
					$opponentFile = $storage->getFile(null, array('id' => $opponentId), true);
				}
				$opponentFile['status'] = static::STATUS_NON_UNIQUE_NAME;

				$this->sendJsonResponse($opponentFile);
			}

			if($filename != $file['extra']['name'])
			{
				$file = $storage->renameFile($filename, $elementId, $parentFolderId);
				if(!$file)
				{
					$this->errorCollection->addOne(new Error('Error in rename (update) file'));
					$this->sendJsonErrorResponse();
				}
			}

			if($parentFolderId != $file['extra']['sectionId'])
			{
				$file = $storage->moveFile($filename, $elementId, $parentFolderId);
			}

			if(!$file)
			{
				$this->errorCollection->addOne(new Error('Error in move/rename (update) file'));
				$this->sendJsonErrorResponse();
			}

			if(!$tmpFile)
			{
				$this->sendJsonSuccessResponse($file);
			}
		}

		if($tmpFile) //update content
		{
			$file = $storage->updateFile($filename, $elementId, $tmpFile, array('originalTimestamp' => $this->request->getPost('originalTimestamp')));
			if($file)
			{
				$event = new Event(Driver::INTERNAL_MODULE_ID, self::EVENT_ON_AFTER_DISK_FILE_UPDATE, array($file['extra']['id'], $file));
				$event->send();

				$this->sendJsonSuccessResponse($file);
			}

			$this->errorCollection->add($storage->getErrors());
			if($this->errorCollection->getErrorByCode($storage::ERROR_CREATE_FORK_FILE))
			{
				$urlManager = Driver::getInstance()->getUrlManager();
				/** @var Error $error */
				$error = $this->errorCollection->getErrorByCode($storage::ERROR_CREATE_FORK_FILE);
				/** @var File $forkedFile */
				$forkedFile = $error->getData();
				$this->errorCollection->clear();

				$this->errorCollection[] = new Error(
					$urlManager->getUrlFocusController('showObjectInGrid', array('objectId' => $forkedFile->getId())),
					self::ERROR_CREATE_FORK_FILE
				);

				$this->sendJsonErrorResponse();
			}

			$this->sendJsonResponse(array(
				'status' => static::STATUS_DENIED,
				'message'=> 'Error in updateFile',
			));
		}

		$this->sendJsonSuccessResponse($file);
	}
	elseif($tmpFile)
	{
		if(!$storage->isUnique($filename, $parentFolderId, $opponentId))
		{
			$opponentFile = array();
			$fixedBadFile = false;
			if ($opponentId)
			{
				$fixedBadFile = $this->tryToFixInvalidDeletedFile($opponentId);
			}

			if (!$fixedBadFile)
			{
				if($opponentId)
				{
					$opponentFile = $storage->getFile(null, array('id' => $opponentId), true);
				}
				$opponentFile['status'] = static::STATUS_OLD_VERSION;

				$this->sendJsonResponse($opponentFile);
			}
		}

		$newFile = $storage->addFile($filename, $parentFolderId, $tmpFile, array('originalTimestamp' => $this->request->getPost('originalTimestamp')));

		if($newFile)
		{
			$event = new Event(Driver::INTERNAL_MODULE_ID, self::EVENT_ON_AFTER_DISK_FILE_ADD, array($newFile['extra']['id'], $newFile));
			$event->send();

			$this->sendJsonSuccessResponse($newFile);
		}
		//else denied
	}

	$this->sendJsonResponse(array(
		'status' => static::STATUS_DENIED,
		'message'=> 'Error in add/update file',
	));
}