diff options
Diffstat (limited to 'controller/callbackcontroller.php')
-rw-r--r-- | controller/callbackcontroller.php | 74 |
1 files changed, 48 insertions, 26 deletions
diff --git a/controller/callbackcontroller.php b/controller/callbackcontroller.php index 0eeeb3c..f69901f 100644 --- a/controller/callbackcontroller.php +++ b/controller/callbackcontroller.php @@ -22,15 +22,18 @@ * in every copy of the program you distribute. * Pursuant to Section 7 ยง 3(e) we decline to grant you any rights under trademark law for use of our trademarks. * -*/ + */ namespace OCA\Onlyoffice\Controller; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataDownloadResponse; +use OCP\AppFramework\Http\JSONResponse; use OCP\Files\File; use OCP\Files\IRootFolder; use OCP\IL10N; +use OCP\ILogger; use OCP\IRequest; use OCP\IUserManager; use OCP\IUserSession; @@ -38,8 +41,6 @@ use OCP\IUserSession; use OCA\Onlyoffice\AppConfig; use OCA\Onlyoffice\Crypt; use OCA\Onlyoffice\DocumentService; -use OCA\Onlyoffice\DownloadResponse; -use OCA\Onlyoffice\ErrorResponse; /** * Callback handler for the document server. @@ -77,6 +78,13 @@ class CallbackController extends Controller { private $trans; /** + * Logger + * + * @var OCP\ILogger + */ + private $logger; + + /** * Application configuration * * @var OCA\Onlyoffice\AppConfig @@ -104,13 +112,15 @@ class CallbackController extends Controller { ); /** - * @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 + * @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 $trans - l10n service + * @param ILogger $logger - logger + * @param OCA\Onlyoffice\AppConfig $config - application configuration + * @param OCA\Onlyoffice\Crypt $crypt - hash generator */ public function __construct($AppName, IRequest $request, @@ -118,6 +128,7 @@ class CallbackController extends Controller { IUserSession $userSession, IUserManager $userManager, IL10N $trans, + ILogger $logger, AppConfig $config, Crypt $crypt ) { @@ -127,6 +138,7 @@ class CallbackController extends Controller { $this->userSession = $userSession; $this->userManager = $userManager; $this->trans = $trans; + $this->logger = $logger; $this->config = $config; $this->crypt = $crypt; } @@ -137,7 +149,7 @@ class CallbackController extends Controller { * * @param string $doc - verification token with the file identifier * - * @return OCA\Onlyoffice\DownloadResponse + * @return DataDownloadResponse * * @NoAdminRequired * @NoCSRFRequired @@ -148,31 +160,36 @@ class CallbackController extends Controller { $hashData = $this->crypt->ReadHash($doc); if ($hashData === NULL) { - return new ErrorResponse(Http::STATUS_FORBIDDEN, $this->trans->t("Access deny")); + $this->logger->info("Download with empty or not correct hash", array("app" => $this->appName)); + return new JSONResponse(["message" => $this->trans->t("Access deny")], Http::STATUS_FORBIDDEN); } if ($hashData->action !== "download") { - return new ErrorResponse(Http::STATUS_BAD_REQUEST, $this->trans->t("Invalid request")); + $this->logger->info("Download with other action", array("app" => $this->appName)); + return new JSONResponse(["message" => $this->trans->t("Invalid request")], Http::STATUS_BAD_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")); + if (empty($files)) { + $this->logger->info("Files for download not found: " . $fileId, array("app" => $this->appName)); + return new JSONResponse(["message" => $this->trans->t("Files not found")], Http::STATUS_NOT_FOUND); } $file = $files[0]; if (! $file instanceof File) { - return new ErrorResponse(Http::STATUS_NOT_FOUND, $this->trans->t("File not found")); + $this->logger->info("File for download not found: " . $fileId, array("app" => $this->appName)); + return new JSONResponse(["message" => $this->trans->t("File not found")], Http::STATUS_NOT_FOUND); } try { - return new DownloadResponse($file); + return new DataDownloadResponse($file->getContent(), $file->getName(), $file->getMimeType()); } catch(\OCP\Files\NotPermittedException $e) { - return new ErrorResponse(Http::STATUS_FORBIDDEN, $this->trans->t("Not permitted")); + $this->logger->info("Download Not permitted: " . $fileId . " " . $e->getMessage(), array("app" => $this->appName)); + return new JSONResponse(["message" => $this->trans->t("Not permitted")], Http::STATUS_FORBIDDEN); } - return new ErrorResponse(Http::STATUS_INTERNAL_SERVER_ERROR, $this->trans->t("Download failed")); + return new JSONResponse(["message" => $this->trans->t("Download failed")], Http::STATUS_INTERNAL_SERVER_ERROR); } /** @@ -194,10 +211,12 @@ class CallbackController extends Controller { $hashData = $this->crypt->ReadHash($doc); if ($hashData === NULL) { - return ["message" => $this->trans->t("Access deny")]; + $this->logger->info("Track with empty or not correct hash", array("app" => $this->appName)); + return new JSONResponse(["message" => $this->trans->t("Access deny")], Http::STATUS_FORBIDDEN); } if ($hashData->action !== "track") { - return ["message" => $this->trans->t("Invalid request")]; + $this->logger->info("Track with other action", array("app" => $this->appName)); + return new JSONResponse(["message" => $this->trans->t("Invalid request")], Http::STATUS_BAD_REQUEST); } $trackerStatus = $this->_trackerStatus[$status]; @@ -211,13 +230,15 @@ class CallbackController extends Controller { $ownerId = $hashData->ownerId; $files = $this->root->getUserFolder($ownerId)->getById($fileId); - if(empty($files)) { - return ["message" => $this->trans->t("Files not found")]; + if (empty($files)) { + $this->logger->info("Files for track not found: " . $fileId, array("app" => $this->appName)); + return new JSONResponse(["message" => $this->trans->t("Files not found")], Http::STATUS_NOT_FOUND); } $file = $files[0]; if (! $file instanceof File) { - return ["message" => $this->trans->t("File not found")]; + $this->logger->info("File for track not found: " . $fileId, array("app" => $this->appName)); + return new JSONResponse(["message" => $this->trans->t("File not found")], Http::STATUS_NOT_FOUND); } $fileName = $file->getName(); @@ -233,7 +254,8 @@ class CallbackController extends Controller { $documentService->GetConvertedUri($url, $downloadExt, $curExt, $key, FALSE, $newFileUri); $url = $newFileUri; } catch (\Exception $e) { - return ["message" => $e->getMessage()]; + $this->logger->error("GetConvertedUri in track: " . $url . " " . $e->getMessage(), array("app" => $this->appName)); + return new JSONResponse(["message" => $e->getMessage()], Http::STATUS_INTERNAL_SERVER_ERROR); } } @@ -251,6 +273,6 @@ class CallbackController extends Controller { break; } - return ["error" => $error]; + return new JSONResponse(["error" => $error], ($error === 0 ? Http::STATUS_OK : Http::STATUS_BAD_REQUEST)); } }
\ No newline at end of file |