protected function getLocationForResumableUpload(FileData $fileData) { if (!$this->checkRequiredInputParams($fileData->toArray(), array('name', 'mimeType', 'size'))) { 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)); $http->setHeader('Content-Type', 'application/json; charset=UTF-8'); $http->setHeader('Authorization', "Bearer {$accessToken}"); $http->setHeader('X-Upload-Content-Type', $fileData->getMimeType()); $http->setHeader('X-Upload-Content-Length', $fileData->getSize()); $postFields = "{\"title\":\"{$fileName}\"}"; if ($http->post('https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&convert=' . ($fileData->isNeedConvert() ? 'true' : 'false'), $postFields) === false) { $errorString = implode('; ', array_keys($http->getError())); $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_GET_LOCATION_FOR_UPLOAD))); return null; } if (!$this->checkHttpResponse($http)) { return null; } return $http->getHeaders()->get('Location'); }
protected function processActionPublish() { $onlineSession = $this->getOnlineEditSessionForFile(); if ($onlineSession) { $forkSession = $onlineSession; if ($onlineSession->getOwnerId() != $this->getUser()->getId()) { $forkSession = $this->forkEditSessionForCurrentUser($onlineSession); } $this->sendJsonSuccessResponse(array('editSessionId' => $forkSession->getId(), 'id' => $onlineSession->getServiceFileId(), 'link' => $onlineSession->getServiceFileLink())); } $src = $this->getFileSrcToPublish(); if (!$src) { $this->errorCollection->add(array(new Error(Loc::getMessage('DISK_DOC_CONTROLLER_ERROR_COULD_NOT_GET_FILE'), self::ERROR_COULD_NOT_GET_FILE))); $this->sendJsonErrorResponse(); } $fileData = new FileData(); $fileData->setName($this->file->getName()); $fileData->setMimeType(TypeFile::getMimeTypeByFilename($this->file->getName())); $fileData->setSrc($src); if (!$fileData->getSize()) { $fileData->setSize(filesize($fileData->getSrc())); } if ($fileData->getSize() === 0) { $fileData = $this->documentHandler->createBlankFile($fileData); } else { $fileData = $this->documentHandler->createFile($fileData); } if (!$fileData) { if ($this->documentHandler->isRequiredAuthorization()) { $this->sendNeedAuth(); } $this->errorCollection->add($this->documentHandler->getErrors()); $this->sendJsonErrorResponse(); } //if somebody publish to google similar document $onlineSession = $this->getOnlineEditSessionForFile(); if ($onlineSession) { $this->documentHandler->deleteFile($fileData); $forkSession = $onlineSession; if ($onlineSession->getOwnerId() != $this->getUser()->getId()) { $forkSession = $this->forkEditSessionForCurrentUser($onlineSession); } $this->sendJsonSuccessResponse(array('editSessionId' => $forkSession->getId(), 'id' => $onlineSession->getServiceFileId(), 'link' => $onlineSession->getServiceFileLink())); } $session = $this->addFileEditSessionByCurrentUser($fileData); $this->sendJsonSuccessResponse(array('editSessionId' => $session->getId(), 'id' => $fileData->getId(), 'link' => $fileData->getLinkInService())); }