public function uploadVersion(Entry $entry) { if (!$entry->getTmpFile()) { $this->errorCollection->addOne(new Error('Could not find cloud import', self::ERROR_COULD_NOT_FIND_CLOUD_IMPORT)); return null; } if ($entry->getContentSize() != $entry->getDownloadedContentSize()) { $this->errorCollection->addOne(new Error('Content size != downloaded content size')); return null; } /** @var File $file */ $file = $entry->getObject(); if (!$file) { $this->errorCollection->addOne(new Error('Could not get file from cloud import record')); return null; } $tmpFile = $entry->getTmpFile(); $fileArray = \CFile::makeFileArray($tmpFile->getAbsolutePath()); $version = $file->uploadVersion($fileArray, $this->documentHandler->getUserId()); if (!$version) { $tmpFile->delete(); $this->errorCollection->add($file->getErrors()); return null; } $entry->linkVersion($version); return $version; }
private function loadData() { if (empty($this->arParams['PARAMS']['arUserField'])) { return array(); } $userId = $this->getUser()->getId(); $values = $this->arParams['PARAMS']['arUserField']['VALUE']; if (!is_array($this->arParams['PARAMS']['arUserField']['VALUE'])) { $values = array($values); } $urlManager = \Bitrix\Disk\Driver::getInstance()->getUrlManager(); $versions = array(); foreach ($values as $value) { $attachedObjectId = (int) $value; if ($attachedObjectId <= 0) { continue; } /** @var \Bitrix\Disk\AttachedObject $attachedModel */ $attachedModel = \Bitrix\Disk\AttachedObject::loadById($attachedObjectId, array('VERSION.OBJECT')); if (!$attachedModel) { continue; } $version = $attachedModel->getVersion(); if (!$version) { continue; } $extension = $version->getExtension(); $versions[] = array('ID' => $attachedModel->getId(), 'NAME' => $version->getName(), 'CONVERT_EXTENSION' => DocumentHandler::isNeedConvertExtension($extension), 'EDITABLE' => DocumentHandler::isEditable($extension), 'CAN_UPDATE' => $attachedModel->canUpdate($userId), 'FROM_EXTERNAL_SYSTEM' => $version->getObject()->getContentProvider() && $version->getObject()->getCreatedBy() == $userId, 'EXTENSION' => $extension, 'SIZE' => \CFile::formatSize($version->getSize()), 'HISTORY_URL' => $urlManager->getUrlUfController('history', array('attachedId' => $attachedModel->getId())), 'DOWNLOAD_URL' => $urlManager->getUrlUfController('download', array('attachedId' => $attachedModel->getId())), 'COPY_TO_ME_URL' => $urlManager->getUrlUfController('copyTome', array('attachedId' => $attachedModel->getId())), 'VIEW_URL' => $urlManager->getUrlToShowAttachedFileByService($attachedModel->getId(), 'gvdrive'), 'EDIT_URL' => $urlManager->getUrlToStartEditUfFileByService($attachedModel->getId(), 'gdrive'), 'GLOBAL_CONTENT_VERSION' => $version->getGlobalContentVersion(), 'ATTRIBUTES_FOR_VIEWER' => Ui\Viewer::getAttributesByAttachedObject($attachedModel, array('version' => $version->getGlobalContentVersion(), 'canUpdate' => $attachedModel->canUpdate($userId), 'showStorage' => false, 'externalId' => false, 'relativePath' => false))); } unset($value); return $versions; }
protected function sendNeedAuth() { $this->sendJsonResponse(array('status' => self::STATUS_NEED_AUTH, 'authUrl' => $this->documentHandler->getUrlForAuthorizeInTokenService())); }
protected function checkHttpResponse(HttpClient $http) { $status = (int) $http->getStatus(); if ($status === 401) { $this->errorCollection->add(array(new Error('Invalid credentials (401)', self::ERROR_CODE_INVALID_CREDENTIALS))); } elseif ($status === 403) { $headers = $http->getHeaders(); $response = $http->getResult(); $errorMessage = ''; if ($response && is_string($response)) { $jsonResponse = Json::decode($response); if (isset($jsonResponse['error']['message'])) { $errorMessage = $jsonResponse['error']['message']; } unset($jsonResponse, $response); } $headerAuthenticate = $headers->get('WWW-Authenticate'); if (is_string($headerAuthenticate) && strpos($headerAuthenticate, 'insufficient') !== false) { $this->errorCollection->add(array(new Error('Insufficient scope (403)', self::ERROR_CODE_INSUFFICIENT_SCOPE))); return false; } elseif (strpos($errorMessage, 'The authenticated user has not installed the app with client') !== false) { $this->errorCollection->add(array(new Error('The authenticated user has not installed the app (403)', self::ERROR_CODE_NOT_INSTALLED_APP))); } elseif (strpos($errorMessage, 'The authenticated user has not granted the app') !== false) { $this->errorCollection->add(array(new Error('The authenticated user has not granted the app (403)', self::ERROR_CODE_NOT_GRANTED_APP))); } elseif (strpos($errorMessage, 'Invalid accessLevel') !== false) { $this->errorCollection->add(array(new Error('Invalid accessLevel (403)', self::ERROR_CODE_INVALID_ACCESS_LEVEL))); } elseif (strpos($errorMessage, 'is not properly configured as a Google Drive app') !== false) { $this->errorCollection->add(array(new Error('The app does not exist or is not properly configured as a Google Drive app (403)', self::ERROR_CODE_APP_NOT_CONFIGURED))); } elseif (strpos($errorMessage, 'is blacklisted') !== false) { $this->errorCollection->add(array(new Error('The app is blacklisted as a Google Drive app. (403)', self::ERROR_CODE_APP_IN_BLACKLIST))); } elseif ($errorMessage) { $this->errorCollection->add(array(new Error($errorMessage, self::ERROR_CODE_UNKNOWN))); } } if ($this->errorCollection->hasErrors()) { return false; } return parent::checkHttpResponse($http); }
private function loadFilesData() { if (empty($this->arParams['PARAMS']['arUserField'])) { return array(); } $userId = $this->getUser()->getId(); $values = $this->arParams['PARAMS']['arUserField']['VALUE']; if (!is_array($this->arParams['PARAMS']['arUserField']['VALUE'])) { $values = array($values); } $files = array(); $driver = \Bitrix\Disk\Driver::getInstance(); $urlManager = $driver->getUrlManager(); $userFieldManager = $driver->getUserFieldManager(); $userFieldManager->loadBatchAttachedObject($values); foreach ($values as $id) { $attachedModel = null; list($type, $realValue) = FileUserType::detectType($id); if ($realValue <= 0) { continue; } elseif ($type == FileUserType::TYPE_NEW_OBJECT) { /** @var File $fileModel */ $fileModel = File::loadById($realValue); if (!$fileModel || !$fileModel->canRead($fileModel->getStorage()->getCurrentUserSecurityContext())) { continue; } } else { /** @var \Bitrix\Disk\AttachedObject $attachedModel */ $attachedModel = $userFieldManager->getAttachedObjectById($realValue); if (!$attachedModel) { continue; } if (!$this->editMode) { $attachedModel->setOperableEntity(array('ENTITY_ID' => $this->arParams['PARAMS']['arUserField']['ENTITY_ID'], 'ENTITY_VALUE_ID' => $this->arParams['PARAMS']['arUserField']['ENTITY_VALUE_ID'])); } /** @var File $fileModel */ $fileModel = $attachedModel->getFile(); } $name = $fileModel->getName(); $data = array('ID' => $id, 'NAME' => $name, 'CONVERT_EXTENSION' => DocumentHandler::isNeedConvertExtension($fileModel->getExtension()), 'EDITABLE' => DocumentHandler::isEditable($fileModel->getExtension()), 'CAN_UPDATE' => $attachedModel ? $attachedModel->canUpdate($userId) : $fileModel->canUpdate($fileModel->getStorage()->getCurrentUserSecurityContext()), 'FROM_EXTERNAL_SYSTEM' => $fileModel->getContentProvider() && $fileModel->getCreatedBy() == $userId, 'EXTENSION' => $fileModel->getExtension(), 'SIZE' => \CFile::formatSize($fileModel->getSize()), 'XML_ID' => $fileModel->getXmlId(), 'FILE_ID' => $fileModel->getId(), 'VIEW_URL' => $urlManager->getUrlToShowAttachedFileByService($id, 'gvdrive'), 'EDIT_URL' => $urlManager->getUrlToStartEditUfFileByService($id, 'gdrive'), 'DOWNLOAD_URL' => $urlManager->getUrlUfController('download', array('attachedId' => $id)), 'COPY_TO_ME_URL' => $urlManager->getUrlUfController('copyToMe', array('attachedId' => $id)), 'DELETE_URL' => ""); if (\Bitrix\Disk\TypeFile::isImage($fileModel)) { $this->arParams['PARAMS']['THUMB_SIZE'] = $this->arParams['PARAMS']['THUMB_SIZE'] > 0 ? $this->arParams['PARAMS']['THUMB_SIZE'] : 100; $data["PREVIEW_URL"] = $attachedModel === null ? $urlManager->getUrlForShowFile($fileModel) : $urlManager->getUrlUfController('show', array('attachedId' => $id)); $data["IMAGE"] = $fileModel->getFile(); } if ($this->editMode) { $data['STORAGE'] = $fileModel->getStorage()->getProxyType()->getTitleForCurrentUser() . ' / ' . $fileModel->getParent()->getName(); } else { $data['ATTRIBUTES_FOR_VIEWER'] = Ui\Viewer::getAttributesByAttachedObject($attachedModel, array('canUpdate' => $data['CAN_UPDATE'], 'canFakeUpdate' => true, 'showStorage' => false, 'externalId' => false, 'relativePath' => false)); } $files[] = $data; } unset($id); return $files; }
/** * Gets data attributes by attached object to viewer. * @param AttachedObject $attachedObject Target attached object. * @param array $additionalParams Additional parameters 'relativePath', 'externalId', 'canUpdate', 'canFakeUpdate', 'showStorage', 'version'. * @return string */ public static function getAttributesByAttachedObject(AttachedObject $attachedObject, array $additionalParams = array()) { $urlManager = Driver::getInstance()->getUrlManager(); $version = $object = null; if ($attachedObject->isSpecificVersion()) { $version = $attachedObject->getVersion(); if (!$version) { return ''; } $name = $version->getName(); $extension = $version->getExtension(); $size = $version->getSize(); $updateTime = $version->getCreateTime(); } else { $object = $attachedObject->getObject(); if (!$object) { return ''; } $name = $object->getName(); $extension = $object->getExtension(); $size = $object->getSize(); $updateTime = $object->getUpdateTime(); } if (DocumentHandler::isEditable($extension)) { $dataAttributesForViewer = 'data-bx-viewer="iframe" ' . 'data-bx-title="' . htmlspecialcharsbx($name) . '" ' . 'data-bx-src="' . $urlManager->getUrlToShowAttachedFileByService($attachedObject->getId(), 'gvdrive') . '" ' . 'data-bx-isFromUserLib="" ' . 'data-bx-askConvert="' . (DocumentHandler::isNeedConvertExtension($extension) ? '1' : '') . '" ' . 'data-bx-download="' . $urlManager->getUrlUfController('download', array('attachedId' => $attachedObject->getId())) . '" ' . 'data-bx-size="' . htmlspecialcharsbx(CFile::formatSize($size)) . '" '; } elseif (Viewer::isViewable($extension)) { $dataAttributesForViewer = 'data-bx-viewer="iframe" ' . 'data-bx-title="' . htmlspecialcharsbx($name) . '" ' . 'data-bx-src="' . $urlManager->getUrlToShowAttachedFileByService($attachedObject->getId(), 'gvdrive') . '" ' . 'data-bx-isFromUserLib="" ' . 'data-bx-askConvert="0" ' . 'data-bx-download="' . $urlManager->getUrlUfController('download', array('attachedId' => $attachedObject->getId())) . '" ' . 'data-bx-size="' . htmlspecialcharsbx(CFile::formatSize($size)) . '" ' . 'data-bx-dateModify="' . htmlspecialcharsbx($updateTime) . '" '; } elseif (TypeFile::isImage($name)) { $dataAttributesForViewer = 'data-bx-viewer="image" ' . 'data-bx-title="' . htmlspecialcharsbx($name) . '" ' . 'data-bx-src="' . $urlManager->getUrlUfController('download', array('attachedId' => $attachedObject->getId())) . '" ' . 'data-bx-isFromUserLib="" ' . 'data-bx-download="' . $urlManager->getUrlUfController('download', array('attachedId' => $attachedObject->getId())) . '" '; } else { $user = $version ? $version->getCreateUser() : $object->getCreateUser(); $formattedName = $user ? $user->getFormattedName() : ''; $dataAttributesForViewer = 'data-bx-viewer="unknown" ' . 'data-bx-src="' . $urlManager->getUrlUfController('download', array('attachedId' => $attachedObject->getId())) . '" ' . 'data-bx-isFromUserLib="" ' . 'data-bx-download="' . $urlManager->getUrlUfController('download', array('attachedId' => $attachedObject->getId())) . '" ' . 'data-bx-title="' . htmlspecialcharsbx($name) . '" ' . 'data-bx-owner="' . htmlspecialcharsbx($formattedName) . '" ' . 'data-bx-dateModify="' . htmlspecialcharsbx($updateTime) . '" ' . 'data-bx-size="' . htmlspecialcharsbx(CFile::formatSize($size)) . '" '; } $dataAttributesForViewer .= " data-bx-history=\"\"" . " data-bx-historyPage=\"\""; if ($object) { $dataAttributesForViewer .= " bx-attach-file-id=\"{$object->getId()}\""; } if (!empty($additionalParams['relativePath'])) { $dataAttributesForViewer .= ' data-bx-relativePath="' . htmlspecialcharsbx($additionalParams['relativePath'] . '/' . $name) . '" '; } if (!empty($additionalParams['externalId'])) { $dataAttributesForViewer .= ' data-bx-externalId="' . htmlspecialcharsbx($additionalParams['externalId']) . '" '; } if (!empty($additionalParams['canUpdate'])) { $dataAttributesForViewer .= ' data-bx-edit="' . $urlManager->getUrlToStartEditUfFileByService($attachedObject->getId(), 'gdrive') . '" '; } if (!empty($additionalParams['canFakeUpdate'])) { $dataAttributesForViewer .= ' data-bx-fakeEdit="' . $urlManager->getUrlToStartEditUfFileByService($attachedObject->getId(), 'gdrive') . '" '; } if (!empty($additionalParams['showStorage']) && $object) { $dataAttributesForViewer .= ' data-bx-storage="' . htmlspecialcharsbx($object->getParent()->getName()) . '" '; } if (!empty($additionalParams['version'])) { $dataAttributesForViewer .= ' data-bx-version="' . htmlspecialcharsbx($additionalParams['version']) . '" '; } return $dataAttributesForViewer; }
public function isReady(DocumentHandler $documentHandler) { if (!$documentHandler->checkAccessibleTokenService()) { $this->errorCollection->add($documentHandler->getErrors()); return false; } return $documentHandler->queryAccessToken()->hasAccessToken() && !$documentHandler->isRequiredAuthorization(); }
if ($version['EDITABLE'] && $version['CAN_UPDATE']) { ?> {text : BX.message('JS_CORE_VIEWER_EDIT'), className : "bx-viewer-popup-item item-edit", href : "#", onclick: function(e){ BX.fireEvent(BX.findPreviousSibling(BX(this.bindElement), function(node){ return BX.type.isElementNode(node) && (node.getAttribute('data-bx-viewer'));}, true), 'click');top.BX.CViewer.objNowInShow.runActionByCurrentElement('forceEdit', {obElementViewer: top.BX.CViewer.objNowInShow}); BX.PopupMenu.currentItem.popupWindow.close(); return BX.PreventDefault(e); }}, <?php } ?> {text : BX.message('JS_CORE_VIEWER_SAVE_TO_OWN_FILES'), className : "bx-viewer-popup-item item-b24", href : "#", onclick: function(e){ top.BX.CViewer.getWindowCopyToDisk({link: "<?php echo CUtil::JSUrlEscape($version['COPY_TO_ME_URL']); ?> ", selfViewer: false, showEdit: <?php echo \Bitrix\Disk\Document\DocumentHandler::isEditable($version['NAME']) ? 'true' : 'false'; ?> , title: "<?php echo CUtil::JSEscape($version["NAME"]); ?> "}); BX.PopupMenu.currentItem.popupWindow.close(); return BX.PreventDefault(e); }}, <?php if ($version['FROM_EXTERNAL_SYSTEM'] && $version['CAN_UPDATE']) { ?> {text : '<?php echo GetMessageJS('DISK_UF_FILE_RUN_FILE_IMPORT'); ?> ', className : "bx-viewer-popup-item item-toload", href : "#", onclick: function(e){
protected function listItemsCloud(DocumentHandler $documentHandler, $path = '/') { $urlManager = Driver::getInstance()->getUrlManager(); $items = $documentHandler->listFolder($path, $this->request->getQuery('folderId')); if ($items === null) { $this->errorCollection->add($documentHandler->getErrors()); return null; } $response = array(); foreach ($items as $item) { $item['link'] = $urlManager->getUrlUfController('loadItems', array('folderId' => $item['id'], 'service' => $documentHandler->getCode())); $response[$item['id']] = $item; } unset($item); return $response; }