• Модуль: disk
  • Путь к файлу: ~/bitrix/modules/disk/lib/document/documentcontroller.php
  • Класс: BitrixDiskDocumentDocumentController
  • Вызов: DocumentController::processActionCommit
protected function processActionCommit()
{
	$this->checkRequiredPostParams(array(
		'editSessionId'
	));
	if($this->errorCollection->hasErrors())
	{
		$this->sendJsonErrorResponse();
	}
	$this->checkUpdatePermissions();

	$currentSession = $this->getEditSessionByCurrentUser((int)$this->request->getPost('editSessionId'));
	if(!$currentSession)
	{
		$this->errorCollection->add(array(new Error(Loc::getMessage('DISK_DOC_CONTROLLER_ERROR_COULD_NOT_FIND_EDIT_SESSION'), self::ERROR_COULD_NOT_FIND_EDIT_SESSION)));
		$this->sendJsonErrorResponse();
	}

	$tmpFile = CTempFile::getFileName(uniqid('_wd'));
	checkDirPath($tmpFile);

	$fileData = new FileData();
	$fileData->setId($currentSession->getServiceFileId());
	$fileData->setSrc($tmpFile);

	$newNameFileAfterConvert = null;
	if($this->documentHandler->isNeedConvertExtension($this->file->getExtension()))
	{
		$newNameFileAfterConvert = getFileNameWithoutExtension($this->file->getName()) . '.' . $this->documentHandler->getConvertExtension($this->file->getExtension());
		$fileData->setMimeType(TypeFile::getMimeTypeByFilename($newNameFileAfterConvert));
	}
	else
	{
		$fileData->setMimeType(TypeFile::getMimeTypeByFilename($this->file->getName()));
	}

	$fileData = $this->downloadFile($currentSession, $fileData);
	if(!$fileData)
	{
		if($this->documentHandler->isRequiredAuthorization())
		{
			$this->sendNeedAuth();
		}
		$this->errorCollection->add($this->documentHandler->getErrors());
		$this->sendJsonErrorResponse();
	}
	$this->deleteEditSession($currentSession);

	$oldName = $this->file->getName();
	//rename in cloud service
	$renameInCloud = $fileData->getName() && $fileData->getName() != $this->file->getName();
	if ($newNameFileAfterConvert && $renameInCloud)
	{
		$newNameFileAfterConvert = getFileNameWithoutExtension($fileData->getName())
			. '.'
			. getFileExtension($newNameFileAfterConvert);
	}

	if ($newNameFileAfterConvert)
	{
		$this->file->rename($newNameFileAfterConvert);
	}
	elseif ($renameInCloud)
	{
		$this->file->rename($fileData->getName());
	}

	$fileArray = CFile::makeFileArray($tmpFile);
	$fileArray['name'] = $this->file->getName();
	$fileArray['type'] = $fileData->getMimeType();
	$fileArray['MODULE_ID'] = Driver::INTERNAL_MODULE_ID;

	$fileId = CFile::saveFile($fileArray, Driver::INTERNAL_MODULE_ID, true, true);

	if(!$fileId)
	{
		$this->errorCollection->add(array(new Error(Loc::getMessage('DISK_DOC_CONTROLLER_ERROR_COULD_NOT_SAVE_FILE'), self::ERROR_COULD_NOT_SAVE_FILE)));
		$this->sendJsonErrorResponse();
	}

	$versionModel = $this->file->addVersion(array(
		'ID' => $fileId,
		'FILE_SIZE' => $fileArray['size'],
	), $this->getUser()->getId(), true);

	if(!$versionModel)
	{
		if(Configuration::isEnabledObjectLock() && $this->file->getErrorByCode(File::ERROR_EXCLUSIVE_LOCK))
		{
			$this->processWithLockedFile($fileId, $fileArray);
		}

		CFile::delete($fileId);
		$this->errorCollection->add(array(new Error(Loc::getMessage('DISK_DOC_CONTROLLER_ERROR_COULD_NOT_ADD_VERSION'), self::ERROR_COULD_NOT_ADD_VERSION)));
		$this->errorCollection->add($this->file->getErrors());
		$this->sendJsonErrorResponse();
	}

	$defaultHandlerForView = Driver::getInstance()->getDocumentHandlersManager()->getDefaultHandlerForView();
	if($this->documentHandler instanceof GoogleHandler && $defaultHandlerForView instanceof GoogleViewerHandler)
	{
		$fileDataNew = $this->documentHandler->repackDocument($fileData);
		if($fileDataNew)
		{
			$fileArray = CFile::makeFileArray($fileDataNew->getSrc());
			$fileArray['name'] = $this->file->getName();
			$fileArray['type'] = $fileData->getMimeType();
			$fileArray['MODULE_ID'] = Driver::INTERNAL_MODULE_ID;

			$fileId = CFile::saveFile($fileArray, Driver::INTERNAL_MODULE_ID, true, true);
			if($fileId && !$this->file->addVersion(array(
					'ID' => $fileId,
					'FILE_SIZE' => $fileArray['size'],
				), $this->getUser()->getId())
			)
			{
				CFile::delete($fileId);
			}
		}
	}

	if($this->isLastEditSessionForFile())
	{
		$this->deleteFile($currentSession, $fileData);
	}

	$this->sendJsonSuccessResponse([
		'object' => [
			'id' => $this->file->getId(),
		],
		'objectId' => $this->file->getId(),
		'newName' => $this->file->getName(),
		'oldName' => $oldName,
	]);
}