From 9bb400714cf8297ba1e40f4014730f0f40a4a43a Mon Sep 17 00:00:00 2001 From: Pranav Kant Date: Thu, 11 May 2017 21:01:40 +0530 Subject: [PATCH] Fix file revisions --- js/documents.js | 3 +- lib/Controller/WopiController.php | 64 +++++++++++++------------------ lib/TokenManager.php | 8 +--- lib/db/wopi.php | 7 +--- lib/helper.php | 27 +++++++++++++ 5 files changed, 58 insertions(+), 51 deletions(-) diff --git a/js/documents.js b/js/documents.js index f4c5c28e..7098c072 100644 --- a/js/documents.js +++ b/js/documents.js @@ -118,7 +118,7 @@ var documentsMain = { } // WOPISrc - URL that loolwsd will access (ie. pointing to ownCloud) - var wopiurl = window.location.protocol + '//' + window.location.host + OC.generateUrl('apps/richdocuments/wopi/files/{file_id}_{instanceId}', {file_id: fileId, instanceId: instanceId}); + var wopiurl = window.location.protocol + '//' + window.location.host + OC.generateUrl('apps/richdocuments/wopi/files/{file_id}', {file_id: fileId}); var wopisrc = encodeURIComponent(wopiurl); // urlsrc - the URL from discovery xml that we access for the particular @@ -162,7 +162,6 @@ var documentsMain = { if (version === 0) { formattedTimestamp = t('richdocuments', 'Latest revision'); downloadUrl = OC.generateUrl('apps/files/download'+ documentPath); - fileId = fileId.replace(/_.*/, ''); } else { downloadUrl = OC.generateUrl('apps/files_versions/download.php?file={file}&revision={revision}', {file: documentPath, revision: version}); diff --git a/lib/Controller/WopiController.php b/lib/Controller/WopiController.php index c1e3eb86..3c810ca6 100644 --- a/lib/Controller/WopiController.php +++ b/lib/Controller/WopiController.php @@ -21,7 +21,9 @@ namespace OCA\Richdocuments\Controller; +use OC\Files\View; use OCA\Richdocuments\Db\Wopi; +use OCA\Richdocuments\Helper; use OCA\Richdocuments\WOPI\Parser; use OCP\AppFramework\Controller; use OCP\AppFramework\Http; @@ -50,29 +52,6 @@ class WopiController extends Controller { $this->rootFolder = $rootFolder; } - /** - * @param string $fileId - * @return array - * @throws \Exception - */ - private function parseFileId($fileId) { - $arr = explode('_', $fileId, 2); - if (count($arr) === 2) { - list($fileId, $instanceId) = $arr; - $version = '0'; - } else if (count($arr) === 3) { - list($fileId, $instanceId, $version) = $arr; - } else { - throw new \Exception('$fileId has not the expected format'); - } - - return [ - $fileId, - $instanceId, - $version, - ]; - } - /** * Returns general info about a file. * @@ -86,12 +65,9 @@ class WopiController extends Controller { public function checkFileInfo($fileId) { $token = $this->request->getParam('access_token'); - list($fileId, , $version) = $this->parseFileId($fileId); - - $row = new Wopi(); - $row->loadBy('token', $token); - - $res = $row->getPathForToken($fileId, $version, $token); + list($fileId, , $version) = Helper::parseFileId($fileId); + $db = new Wopi(); + $res = $db->getPathForToken($fileId, $token); if ($res === false) { return new JSONResponse([], Http::STATUS_FORBIDDEN); } @@ -136,18 +112,32 @@ class WopiController extends Controller { */ public function getFile($fileId, $access_token) { - list($fileId, , $version) = $this->parseFileId($fileId); - + list($fileId, , $version) = Helper::parseFileId($fileId); $row = new Wopi(); $row->loadBy('token', $access_token); - - $res = $row->getPathForToken($fileId, $version, $access_token); - + $res = $row->getPathForToken($fileId, $access_token); try { /** @var File $file */ $userFolder = $this->rootFolder->getUserFolder($res['owner']); $file = $userFolder->getById($fileId)[0]; - $response = new StreamResponse($file->fopen('rb')); + + if ($version !== '0') + { + $view = new View('/' . $res['owner'] . '/files'); + $relPath = $view->getRelativePath($file->getPath()); + $versionPath = '/files_versions/' . $relPath . '.v' . $version; + $view = new View('/' . $res['owner']); + if ($view->file_exists($versionPath)){ + $response = new StreamResponse($view->fopen($versionPath, 'rb')); + } + else { + $response->setStatus(Http::STATUS_NOT_FOUND); + } + } + else + { + $response = new StreamResponse($file->fopen('rb')); + } $response->addHeader('Content-Disposition', 'attachment'); $response->addHeader('Content-Type', 'application/octet-stream'); return $response; @@ -169,12 +159,12 @@ class WopiController extends Controller { */ public function putFile($fileId, $access_token) { - list($fileId, , $version) = $this->parseFileId($fileId); + list($fileId, , $version) = Helper::parseFileId($fileId); $row = new Wopi(); $row->loadBy('token', $access_token); - $res = $row->getPathForToken($fileId, $version, $access_token); + $res = $row->getPathForToken($fileId, $access_token); if (!$res['canwrite']) { return new JSONResponse([], Http::STATUS_FORBIDDEN); } diff --git a/lib/TokenManager.php b/lib/TokenManager.php index d212b4e3..c8ac1f5c 100644 --- a/lib/TokenManager.php +++ b/lib/TokenManager.php @@ -22,6 +22,7 @@ namespace OCA\Richdocuments; use OC\Share\Constants; +use OCA\Richdocuments\Helper; use OCA\Richdocuments\Db\Wopi; use OCA\Richdocuments\WOPI\Parser; use OCP\Files\File; @@ -64,12 +65,7 @@ class TokenManager { * @throws \Exception */ public function getToken($fileId, $shareToken = null) { - $arr = explode('_', $fileId, 2); - $version = '0'; - if (count($arr) === 2) { - list($fileId, $version) = $arr; - } - + list($fileId,, $version) = Helper::parseFileId($fileId); // if the user is not logged-in do use the sharers storage if($shareToken !== null) { /** @var File $file */ diff --git a/lib/db/wopi.php b/lib/db/wopi.php index 0eded59d..45152f8e 100644 --- a/lib/db/wopi.php +++ b/lib/db/wopi.php @@ -56,7 +56,7 @@ class Wopi extends \OCA\Richdocuments\Db{ * constructs and validates the path. * Returns the path, if valid, else false. */ - public function getPathForToken($fileId, $version, $token){ + public function getPathForToken($fileId, $token){ $wopi = new Wopi(); $row = $wopi->loadBy('token', $token)->getData(); @@ -75,11 +75,6 @@ class Wopi extends \OCA\Richdocuments\Db{ //$wopi->deleteBy('id', $row['id']); //return false; } - if ($row['fileid'] !== $fileId || $row['version'] !== $version){ - // File unknown / user unauthorized (for the requested file). - http_response_code(404); - return false; - } return array( 'owner' => $row['owner_uid'], diff --git a/lib/helper.php b/lib/helper.php index b5d6cda9..55e6f410 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -14,6 +14,33 @@ namespace OCA\Richdocuments; class Helper { const APP_ID = 'richdocuments'; + /** + * @param string $fileId + * @return array + * @throws \Exception + */ + public static function parseFileId($fileId) { + $arr = explode('_', $fileId); + if (count($arr) === 1) { + $fileId = $arr[0]; + $version = '0'; + } else if (count($arr) === 2) { + list($fileId, $instanceId) = $arr; + $version = '0'; + } else if (count($arr) === 3) { + list($fileId, $instanceId, $version) = $arr; + } else { + throw new \Exception('$fileId has not the expected format'); + } + + return [ + $fileId, + $instanceId, + $version, + ]; + } + + public static function getNewFileName($view, $path, $prepend = ' '){ $fileNum = 1;