From 093e9dd4225e6681140523c75bfc20809cd6d651 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Wed, 4 May 2016 10:28:06 +0200 Subject: Add route to resolve fileid to files app URL The following routes will redirect to the files app and display the matching folder. If the fileid is a file, it will scroll to it. - http://localhost/owncloud/index.php/f/$fileid - http://localhost/owncloud/index.php/files/?dir=somedir&fileid=$fileid --- apps/files/controller/viewcontroller.php | 48 ++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) (limited to 'apps/files/controller/viewcontroller.php') diff --git a/apps/files/controller/viewcontroller.php b/apps/files/controller/viewcontroller.php index ded6fd555df..6ee924e2f0b 100644 --- a/apps/files/controller/viewcontroller.php +++ b/apps/files/controller/viewcontroller.php @@ -3,6 +3,7 @@ * @author Christoph Wurst * @author Lukas Reschke * @author Thomas Müller + * @author Vincent Petry * * @copyright Copyright (c) 2016, ownCloud, Inc. * @license AGPL-3.0 @@ -26,6 +27,7 @@ namespace OCA\Files\Controller; use OC\AppFramework\Http\Request; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\ContentSecurityPolicy; +use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; use OCP\IConfig; @@ -35,6 +37,8 @@ use OCP\IRequest; use OCP\IURLGenerator; use OCP\IUserSession; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use OCP\AppFramework\Http\NotFoundResponse; +use OCP\Files\Folder; /** * Class ViewController @@ -58,6 +62,8 @@ class ViewController extends Controller { protected $eventDispatcher; /** @var IUserSession */ protected $userSession; + /** @var \OCP\Files\Folder */ + protected $userFolder; /** * @param string $appName @@ -68,6 +74,7 @@ class ViewController extends Controller { * @param IConfig $config * @param EventDispatcherInterface $eventDispatcherInterface * @param IUserSession $userSession + * @param Folder $userFolder */ public function __construct($appName, IRequest $request, @@ -76,7 +83,9 @@ class ViewController extends Controller { IL10N $l10n, IConfig $config, EventDispatcherInterface $eventDispatcherInterface, - IUserSession $userSession) { + IUserSession $userSession, + Folder $userFolder + ) { parent::__construct($appName, $request); $this->appName = $appName; $this->request = $request; @@ -86,6 +95,7 @@ class ViewController extends Controller { $this->config = $config; $this->eventDispatcher = $eventDispatcherInterface; $this->userSession = $userSession; + $this->userFolder = $userFolder; } /** @@ -124,10 +134,15 @@ class ViewController extends Controller { * * @param string $dir * @param string $view + * @param string $fileid * @return TemplateResponse * @throws \OCP\Files\NotFoundException */ - public function index($dir = '', $view = '') { + public function index($dir = '', $view = '', $fileid = null) { + if ($fileid !== null) { + return $this->showFile($fileid); + } + $nav = new \OCP\Template('files', 'appnavigation', ''); // Load the files we need @@ -239,4 +254,33 @@ class ViewController extends Controller { return $response; } + + /** + * Redirects to the file list and highlight the given file id + * + * @param string $fileId file id to show + * @return Response redirect response or not found response + * + * @NoCSRFRequired + * @NoAdminRequired + */ + public function showFile($fileId) { + $files = $this->userFolder->getById($fileId); + $params = []; + + if (!empty($files)) { + $file = current($files); + if ($file instanceof Folder) { + // set the full path to enter the folder + $params['dir'] = $this->userFolder->getRelativePath($file->getPath()); + } else { + // set parent path as dir + $params['dir'] = $this->userFolder->getRelativePath($file->getParent()->getPath()); + // and scroll to the entry + $params['scrollto'] = $file->getName(); + } + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params)); + } + return new NotFoundResponse(); + } } -- cgit v1.2.3