protected function processActionDiscard()
 {
     $this->checkRequiredPostParams(array('editSessionId'));
     if ($this->errorCollection->hasErrors()) {
         $this->sendJsonErrorResponse();
     }
     $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();
     }
     if (!$this->deleteEditSession($currentSession)) {
         $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_DOC_CONTROLLER_ERROR_COULD_NOT_FIND_DELETE_SESSION'), self::ERROR_COULD_NOT_FIND_DELETE_SESSION)));
         $this->sendJsonErrorResponse();
     }
     $fileData = new FileData();
     $fileData->setId($currentSession->getServiceFileId());
     if ($currentSession->isExclusive()) {
         $this->deleteFile($currentSession, $fileData);
     } else {
         $this->initializeFile($currentSession->getObjectId());
         if ($this->isLastEditSessionForFile()) {
             $this->deleteFile($currentSession, $fileData);
         }
     }
     $this->sendJsonSuccessResponse();
 }
 /**
  * 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;
 }
 /**
  * Create new blank file in cloud service.
  * It is not necessary set shared rights on file.
  * @param FileData $fileData
  * @return FileData|null
  */
 public function createBlankFile(FileData $fileData)
 {
     if (!$this->checkRequiredInputParams($fileData->toArray(), array('name'))) {
         return null;
     }
     $accessToken = $this->getAccessToken();
     $googleMimeType = $this->getInternalMimeTypeByExtension(getFileExtension($fileData->getName()));
     $fileName = getFileNameWithoutExtension($fileData->getName());
     $fileName = $this->convertToUtf8($fileName);
     if (!$googleMimeType) {
         $this->errorCollection->add(array(new Error("Unsupported file format with name {$fileData->getName()}", self::ERROR_UNSUPPORTED_FILE_FORMAT)));
         return null;
     }
     $http = new HttpClient(array('socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1));
     $http->setHeader('Content-Type', 'application/json; charset=UTF-8');
     $http->setHeader('Authorization', "Bearer {$accessToken}");
     $postFields = "{\"title\":\"{$fileName}\",\"mimeType\":\"{$googleMimeType}\"}";
     if ($http->post(self::API_URL_V2 . '/files', $postFields) === false) {
         $errorString = implode('; ', array_keys($http->getError()));
         $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_CREATE_BLANK)));
         return null;
     }
     if (!$this->checkHttpResponse($http)) {
         return null;
     }
     $finalOutput = Json::decode($http->getResult());
     if ($finalOutput === null) {
         $this->errorCollection->add(array(new Error('Could not decode response as json', self::ERROR_BAD_JSON)));
         return null;
     }
     if (empty($finalOutput['id']) || empty($finalOutput['alternateLink'])) {
         $this->errorCollection->add(array(new Error('Could not find id or alternateLink in response from Google.', self::ERROR_COULD_NOT_FIND_ID)));
         return null;
     }
     $fileData->setLinkInService($finalOutput['alternateLink']);
     $fileData->setId($finalOutput['id']);
     //last signed user must delete file from google drive
     $this->insertPermission($fileData);
     return $fileData;
 }
 protected function createFileInternal(FileData $fileData)
 {
     if (!$this->checkRequiredInputParams($fileData->toArray(), array('name', 'src'))) {
         return null;
     }
     $accessToken = $this->getAccessToken();
     $fileName = $fileData->getName();
     $fileName = $this->convertToUtf8($fileName);
     $http = new HttpClient(array('redirect' => false, 'socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1));
     $fileName = urlencode($fileName);
     $accessToken = urlencode($accessToken);
     if ($http->query('PUT', "https://apis.live.net/v5.0/me/skydrive/files/{$fileName}?access_token={$accessToken}", IO\File::getFileContents(IO\Path::convertPhysicalToLogical($fileData->getSrc()))) === false) {
         $errorString = implode('; ', array_keys($http->getError()));
         $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_FILE_INTERNAL)));
         return null;
     }
     if (!$this->checkHttpResponse($http)) {
         return null;
     }
     $finalOutput = Json::decode($http->getResult());
     if ($finalOutput === null) {
         $this->errorCollection->add(array(new Error('Could not decode response as json', self::ERROR_BAD_JSON)));
         return null;
     }
     $fileData->setId($finalOutput['id']);
     $props = $this->getFileData($fileData);
     if ($props === null) {
         return null;
     }
     $fileData->setLinkInService($props['link']);
     return $fileData;
 }