"NotFound", 1 => "Editing", 2 => "MustSave", 3 => "Corrupted", 4 => "Closed" ); /** * @param string $AppName application name * @param IRequest $request request object * @param IRootFolder $root root folder * @param IUserSession $userSession user session * @param IUserManager $userManager user manager * @param IL10N $l10n l10n service * @param OCA\Onlyoffice\Crypt $crypt hash generator */ public function __construct($AppName, IRequest $request, IRootFolder $root, IUserSession $userSession, IUserManager $userManager, IL10N $trans, AppConfig $config, Crypt $crypt ) { parent::__construct($AppName, $request); $this->root = $root; $this->userSession = $userSession; $this->userManager = $userManager; $this->trans = $trans; $this->config = $config; $this->crypt = $crypt; } /** * Downloading file by the document service * * @param string $doc - verification token with the file identifier * * @return OCA\Onlyoffice\DownloadResponse * * @NoAdminRequired * @NoCSRFRequired * @PublicPage * @CORS */ public function download($doc) { $hashData = $this->crypt->ReadHash($doc); if ($hashData === NULL) { return new ErrorResponse(Http::STATUS_FORBIDDEN, $this->trans->t("Access deny")); } if ($hashData->action !== "download") { return new ErrorResponse(Http::STATUS_BAD_REQUEST, $this->trans->t("Invalid request")); } $fileId = $hashData->fileId; $ownerId = $hashData->ownerId; $files = $this->root->getUserFolder($ownerId)->getById($fileId); if(empty($files)) { return new ErrorResponse(Http::STATUS_NOT_FOUND, $this->trans->t("Files not found")); } $file = $files[0]; if (! $file instanceof File) { return new ErrorResponse(Http::STATUS_NOT_FOUND, $this->trans->t("File not found")); } try { return new DownloadResponse($file); } catch(\OCP\Files\NotPermittedException $e) { return new ErrorResponse(Http::STATUS_FORBIDDEN, $this->trans->t("Not permitted")); } return new ErrorResponse(Http::STATUS_INTERNAL_SERVER_ERROR, $this->trans->t("Download failed")); } /** * Handle request from the document server with the document status information * * @param string $doc - verification token with the file identifier * @param array $users - the list of the identifiers of the users * @param string $key - the edited document identifier * @param string $url - the link to the edited document to be saved * * @return array * * @NoAdminRequired * @NoCSRFRequired * @PublicPage * @CORS */ public function track($doc, $users, $key, $status, $url) { $hashData = $this->crypt->ReadHash($doc); if ($hashData === NULL) { return ["message" => $this->trans->t("Access deny")]; } if ($hashData->action !== "track") { return ["message" => $this->trans->t("Invalid request")]; } $trackerStatus = $this->_trackerStatus[$status]; $error = 1; switch ($trackerStatus) { case "MustSave": case "Corrupted": $fileId = $hashData->fileId; $ownerId = $hashData->ownerId; $files = $this->root->getUserFolder($ownerId)->getById($fileId); if(empty($files)) { return ["message" => $this->trans->t("Files not found")]; } $file = $files[0]; if (! $file instanceof File) { return ["message" => $this->trans->t("File not found")]; } $fileName = $file->getName(); $curExt = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); $downloadExt = strtolower(pathinfo($url, PATHINFO_EXTENSION)); if ($downloadExt !== $curExt) { $documentService = new DocumentService($this->trans, $this->config); $key = DocumentService::GenerateRevisionId($fileId . $url); try { $newFileUri; $documentService->GetConvertedUri($url, $downloadExt, $curExt, $key, FALSE, $newFileUri); $url = $newFileUri; } catch (\Exception $e) { return ["message" => $e->getMessage()]; } } if (($newData = file_get_contents($url))) { $this->userSession->setUser($this->userManager->get($users[0])); $file->putContent($newData); $error = 0; } break; case "Editing": $error = 0; break; } return ["error" => $error]; } }