From 30a22ef7e76799e6dc4fcff955c3cb83c4be054b Mon Sep 17 00:00:00 2001 From: Ashod Nakashian Date: Tue, 8 Mar 2016 21:16:19 -0500 Subject: [PATCH] WOPI GetFile and PutFile added and document loader updated --- appinfo/routes.php | 4 ++ controller/documentcontroller.php | 64 +++++++++++++++++++++++++++++++ js/documents.js | 53 +++++++++++++------------ 3 files changed, 97 insertions(+), 24 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index fbcba020..e80c48b0 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -36,6 +36,10 @@ $application->registerRoutes($this, [ ['name' => 'document#localLoad', 'url' => 'load/{fileId}', 'verb' => 'POST'], ['name' => 'document#localSave', 'url' => 'save/{fileId}', 'verb' => 'POST'], ['name' => 'document#localClose', 'url' => 'close/{fileId}', 'verb' => 'POST'], + //documents - for WOPI access + ['name' => 'document#wopiGetToken', 'url' => 'wopi/token/{fileId}', 'verb' => 'GET'], + ['name' => 'document#wopiGetFile', 'url' => 'wopi/files/{fileId}/contents', 'verb' => 'GET'], + ['name' => 'document#wopiPutFile', 'url' => 'wopi/files/{fileId}/contents', 'verb' => 'POST'], //settings ['name' => 'settings#savePersonal', 'url' => 'ajax/personal.php', 'verb' => 'POST'], ['name' => 'settings#setUnstable', 'url' => 'ajax/config/unstable', 'verb' => 'POST'], diff --git a/controller/documentcontroller.php b/controller/documentcontroller.php index 2033457a..9d33fe72 100644 --- a/controller/documentcontroller.php +++ b/controller/documentcontroller.php @@ -265,6 +265,70 @@ class DocumentController extends Controller{ ); } + /** + * Generates and returns an access token for a given fileId. + * Only for authenticated users! + */ + public function wopiGetToken($fileId){ + \OC::$server->getLogger()->debug('Generating WOPI Token for file {fileId}.', [ 'app' => $this->appName, 'fileId' => $fileId ]); + + $row = new Db\Wopi(); + $token = $row->generateFileToken($fileId); + + // Return the token. + return array( + 'status' => 'success', + 'token' => $token + ); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @PublicPage + * Given an access token and a fileId, returns the contents of the file. + * Expects a valid token in access_token parameter. + */ + public function wopiGetFile($fileId){ + $token = $this->request->getParam('access_token'); + + \OC::$server->getLogger()->debug('Getting contents of file {fileId} by token {token}.', [ 'app' => $this->appName, 'fileId' => $fileId, 'token' => $token ]); + + $row = new Db\Wopi(); + $row->loadBy('token', $token); + + //TODO: Support X-WOPIMaxExpectedSize header. + $res = $row->getPathForToken($fileId, $token); + return new DownloadResponse($this->request, $res['user'], $res['path']); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @PublicPage + * Given an access token and a fileId, replaces the files with the request body. + * Expects a valid token in access_token parameter. + */ + public function wopiPutFile($fileId){ + $token = $this->request->getParam('access_token'); + + \OC::$server->getLogger()->debug('Putting contents of file {fileId} by token {token}.', [ 'app' => $this->appName, 'fileId' => $fileId, 'token' => $token ]); + + $row = new Db\Wopi(); + $row->loadBy('token', $token); + + $res = $row->getPathForToken($token); + $view = new \OC\Files\View('/' . $res['user'] . '/'); + + // Read the contents of the file from the POST body and store. + $content = file_get_contents('php://input'); + $view->file_put_contents($res['path'], $content); + + return array( + 'status' => 'success' + ); + } + /** * @NoAdminRequired * @PublicPage diff --git a/js/documents.js b/js/documents.js index 5b8d16eb..a9ea2ebd 100644 --- a/js/documents.js +++ b/js/documents.js @@ -180,27 +180,38 @@ var documentsMain = { $('title').text(title + ' - ' + documentsMain.UI.mainTitle); - // TODO. wopiurl = get from discovery xml - var wopiurl = $('#wopi-url').val() + '/loleaflet/dist/loleaflet.html'; - var wopisrc = documentsMain.url; - var action = wopiurl + '?' + wopisrc; - var token = oc_requesttoken; - - var form = '
' + - '
'; - var frame = '