diff --git a/js/documents.js b/js/documents.js index d902d182..7098c072 100644 --- a/js/documents.js +++ b/js/documents.js @@ -72,7 +72,6 @@ var documentsMain = { loadErrorHint : '', renderComplete: false, // false till page is rendered with all required data about the document(s) toolbar : '
', - returnToDir : null, // directory where we started from in the 'Files' app UI : { /* Editor wrapper HTML */ @@ -119,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 @@ -163,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}); @@ -268,12 +266,8 @@ var documentsMain = { documentsMain.UI.notify(t('richdocuments', 'Failed to revert the document to older version')); } - // generate file id with returnToDir information in it, if any - var fileid = e.currentTarget.parentElement.dataset.fileid.replace(/_.*/, '') + - (documentsMain.returnToDir ? '_' + documentsMain.returnToDir : ''); - // load the file again, it should get reverted now - window.location = OC.generateUrl('apps/richdocuments/index#{fileid}', {fileid: fileid}); + window.location = OC.generateUrl('apps/richdocuments/index#{fileid}', {fileid: e.currentTarget.parentElement.dataset.fileid}); window.location.reload(); documentsMain.overlay.documentOverlay('hide'); } @@ -426,11 +420,6 @@ var documentsMain = { // Does anything indicate that we need to autostart a session? fileId = getURLParameter('fileid').replace(/^\W*/, ''); - if (fileId.indexOf('_') >= 0) { - documentsMain.returnToDir = unescape(fileId.replace(/^[^_]*_/, '')); - fileId = fileId.replace(/_.*/, ''); - } - documentsMain.show(fileId); if (fileId) { diff --git a/js/viewer/viewer.js b/js/viewer/viewer.js index cdf5bc72..f7237749 100644 --- a/js/viewer/viewer.js +++ b/js/viewer/viewer.js @@ -77,7 +77,7 @@ var odfViewer = { ); } else { viewer = OC.generateUrl( - 'apps/richdocuments/index?fileId={fileId}_{dir}&requesttoken={requesttoken}', + 'apps/richdocuments/index?fileId={fileId}&requesttoken={requesttoken}', { fileId: fileId, dir: fileDir, 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;