urlGenerator->linkToRoute('files.view.index')); } // This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server. try { return $this->redirectToFile((int)$fileid); } catch (NotFoundException $e) { return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); } } /** * @param string $dir * @param string $view * @param string $fileid * @param bool $fileNotFound * @return TemplateResponse|RedirectResponse */ #[NoAdminRequired] #[NoCSRFRequired] public function indexView($dir = '', $view = '', $fileid = null, $fileNotFound = false) { return $this->index($dir, $view, $fileid, $fileNotFound); } /** * @param string $dir * @param string $view * @param string $fileid * @param bool $fileNotFound * @return TemplateResponse|RedirectResponse */ #[NoAdminRequired] #[NoCSRFRequired] public function indexViewFileid($dir = '', $view = '', $fileid = null, $fileNotFound = false) { return $this->index($dir, $view, $fileid, $fileNotFound); } /** * @param string $dir * @param string $view * @param string $fileid * @param bool $fileNotFound * @return TemplateResponse|RedirectResponse */ #[NoAdminRequired] #[NoCSRFRequired] public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false) { if ($fileid !== null && $view !== 'trashbin') { try { return $this->redirectToFileIfInTrashbin((int)$fileid); } catch (NotFoundException $e) { } } // Load the files we need Util::addInitScript('files', 'init'); Util::addScript('files', 'main'); $userId = $this->userSession->getUser()->getUID(); // If the file doesn't exists in the folder and // exists in only one occurrence, redirect to that file // in the correct folder if ($fileid && $dir !== '') { $baseFolder = $this->rootFolder->getUserFolder($userId); $nodes = $baseFolder->getById((int)$fileid); if (!empty($nodes)) { $nodePath = $baseFolder->getRelativePath($nodes[0]->getPath()); $relativePath = $nodePath ? dirname($nodePath) : ''; // If the requested path does not contain the file id // or if the requested path is not the file id itself if (count($nodes) === 1 && $relativePath !== $dir && $nodePath !== $dir) { return $this->redirectToFile((int)$fileid); } } else { // fileid does not exist anywhere $fileNotFound = true; } } try { // If view is files, we use the directory, otherwise we use the root storage $storageInfo = $this->getStorageInfo(($view === 'files' && $dir) ? $dir : '/'); } catch (\Exception $e) { $storageInfo = $this->getStorageInfo(); } $this->initialState->provideInitialState('storageStats', $storageInfo); $this->initialState->provideInitialState('config', $this->userConfig->getConfigs()); $this->initialState->provideInitialState('viewConfigs', $this->viewConfig->getConfigs()); // File sorting user config $filesSortingConfig = json_decode($this->config->getUserValue($userId, 'files', 'files_sorting_configs', '{}'), true); $this->initialState->provideInitialState('filesSortingConfig', $filesSortingConfig); // Forbidden file characters (deprecated use capabilities) // TODO: Remove with next release of `@nextcloud/files` $forbiddenCharacters = $this->filenameValidator->getForbiddenCharacters(); $this->initialState->provideInitialState('forbiddenCharacters', $forbiddenCharacters); $event = new LoadAdditionalScriptsEvent(); $this->eventDispatcher->dispatchTyped($event); $this->eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent()); $this->eventDispatcher->dispatchTyped(new LoadSidebar()); $this->eventDispatcher->dispatchTyped(new LoadSearchPlugins()); // Load Viewer scripts if (class_exists(LoadViewer::class)) { $this->eventDispatcher->dispatchTyped(new LoadViewer()); } $this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false); $this->initialState->provideInitialState('templates', $this->templateManager->listCreators()); $response = new TemplateResponse( Application::APP_ID, 'index', ); $policy = new ContentSecurityPolicy(); $policy->addAllowedFrameDomain('\'self\''); // Allow preview service worker $policy->addAllowedWorkerSrcDomain('\'self\''); $response->setContentSecurityPolicy($policy); return $response; } /** * Redirects to the trashbin file list and highlight the given file id * * @param int $fileId file id to show * @return RedirectResponse redirect response or not found response * @throws NotFoundException */ private function redirectToFileIfInTrashbin($fileId): RedirectResponse { $uid = $this->userSession->getUser()->getUID(); $baseFolder = $this->rootFolder->getUserFolder($uid); $node = $baseFolder->getFirstNodeById($fileId); $params = []; if (!$node && $this->appManager->isEnabledForUser('files_trashbin')) { /** @var Folder */ $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); $node = $baseFolder->getFirstNodeById($fileId); $params['view'] = 'trashbin'; if ($node) { $params['fileid'] = $fileId; if ($node instanceof Folder) { // set the full path to enter the folder $params['dir'] = $baseFolder->getRelativePath($node->getPath()); } else { // set parent path as dir $params['dir'] = $baseFolder->getRelativePath($node->getParent()->getPath()); } return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.indexViewFileid', $params)); } } throw new NotFoundException(); } /** * Redirects to the file list and highlight the given file id * * @param int $fileId file id to show * @return RedirectResponse redirect response or not found response * @throws NotFoundException */ private function redirectToFile(int $fileId) { $uid = $this->userSession->getUser()->getUID(); $baseFolder = $this->rootFolder->getUserFolder($uid); $node = $baseFolder->getFirstNodeById($fileId); $params = ['view' => 'files']; try { $this->redirectToFileIfInTrashbin($fileId); } catch (NotFoundException $e) { } if ($node) { $params['fileid'] = $fileId; if ($node instanceof Folder) { // set the full path to enter the folder $params['dir'] = $baseFolder->getRelativePath($node->getPath()); } else { // set parent path as dir $params['dir'] = $baseFolder->getRelativePath($node->getParent()->getPath()); // open the file by default (opening the viewer) $params['openfile'] = 'true'; } return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.indexViewFileid', $params)); } throw new NotFoundException(); } }