/** * Saves file in destination folder from entry of cloud import. * @param Entry $entry Cloud import entry. * @param Folder $folder Destination folder. * @return \Bitrix\Disk\File|null New file object. */ public function saveFile(Entry $entry, Folder $folder) { 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; } $fileData = new Document\FileData(); $fileData->setId($entry->getServiceObjectId()); $fileMetadata = $this->documentHandler->getFileMetadata($fileData); if (!$fileMetadata || empty($fileMetadata['name'])) { $this->errorCollection->add($this->documentHandler->getErrors()); return null; } $name = $fileMetadata['name']; if (!getFileExtension($name)) { $name = $this->recoverExtensionInName($name, $fileMetadata['mimeType']); } $tmpFile = $entry->getTmpFile(); $fileArray = \CFile::makeFileArray($tmpFile->getAbsolutePath()); $file = $folder->uploadFile($fileArray, array('NAME' => $name, 'CREATED_BY' => $this->documentHandler->getUserId(), 'CONTENT_PROVIDER' => $this->documentHandler->getCode()), array(), true); if (!$file) { $tmpFile->delete(); $this->errorCollection->add($folder->getErrors()); return null; } $entry->linkObject($file); return $file; }
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->documentHandler->downloadFile($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) { if ($newNameFileAfterConvert && $renameInCloud) { $newNameFileAfterConvert = getFileNameWithoutExtension($fileData->getName()) . '.' . getFileExtension($newNameFileAfterConvert); } $this->file->rename($newNameFileAfterConvert); } $fileArray = \CFile::makeFileArray($tmpFile); $fileArray['name'] = $this->file->getName(); $fileArray['type'] = $fileData->getMimeType(); $fileArray['MODULE_ID'] = Driver::INTERNAL_MODULE_ID; /** @noinspection PhpDynamicAsStaticMethodCallInspection */ $fileId = \CFile::saveFile($fileArray, Driver::INTERNAL_MODULE_ID); if (!$fileId) { \CFile::delete($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) { \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(); } if ($this->isLastEditSessionForFile()) { $this->deleteFile($currentSession, $fileData); } $this->sendJsonSuccessResponse(array('objectId' => $this->file->getId(), 'newName' => $this->file->getName(), 'oldName' => $oldName)); }
public function isReady(DocumentHandler $documentHandler) { if (!$documentHandler->checkAccessibleTokenService()) { $this->errorCollection->add($documentHandler->getErrors()); return false; } return $documentHandler->queryAccessToken()->hasAccessToken() && !$documentHandler->isRequiredAuthorization(); }
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; }