@@ -65,369 +65,369 @@ |
||
| 65 | 65 | * @package OCA\Files\Controller |
| 66 | 66 | */ |
| 67 | 67 | class ViewController extends Controller { |
| 68 | - /** @var string */ |
|
| 69 | - protected $appName; |
|
| 70 | - /** @var IRequest */ |
|
| 71 | - protected $request; |
|
| 72 | - /** @var IURLGenerator */ |
|
| 73 | - protected $urlGenerator; |
|
| 74 | - /** @var IL10N */ |
|
| 75 | - protected $l10n; |
|
| 76 | - /** @var IConfig */ |
|
| 77 | - protected $config; |
|
| 78 | - /** @var IEventDispatcher */ |
|
| 79 | - protected $eventDispatcher; |
|
| 80 | - /** @var IUserSession */ |
|
| 81 | - protected $userSession; |
|
| 82 | - /** @var IAppManager */ |
|
| 83 | - protected $appManager; |
|
| 84 | - /** @var IRootFolder */ |
|
| 85 | - protected $rootFolder; |
|
| 86 | - /** @var Helper */ |
|
| 87 | - protected $activityHelper; |
|
| 88 | - /** @var IInitialState */ |
|
| 89 | - private $initialState; |
|
| 90 | - /** @var ITemplateManager */ |
|
| 91 | - private $templateManager; |
|
| 92 | - /** @var IManager */ |
|
| 93 | - private $shareManager; |
|
| 94 | - |
|
| 95 | - public function __construct(string $appName, |
|
| 96 | - IRequest $request, |
|
| 97 | - IURLGenerator $urlGenerator, |
|
| 98 | - IL10N $l10n, |
|
| 99 | - IConfig $config, |
|
| 100 | - IEventDispatcher $eventDispatcher, |
|
| 101 | - IUserSession $userSession, |
|
| 102 | - IAppManager $appManager, |
|
| 103 | - IRootFolder $rootFolder, |
|
| 104 | - Helper $activityHelper, |
|
| 105 | - IInitialState $initialState, |
|
| 106 | - ITemplateManager $templateManager, |
|
| 107 | - IManager $shareManager |
|
| 108 | - ) { |
|
| 109 | - parent::__construct($appName, $request); |
|
| 110 | - $this->appName = $appName; |
|
| 111 | - $this->request = $request; |
|
| 112 | - $this->urlGenerator = $urlGenerator; |
|
| 113 | - $this->l10n = $l10n; |
|
| 114 | - $this->config = $config; |
|
| 115 | - $this->eventDispatcher = $eventDispatcher; |
|
| 116 | - $this->userSession = $userSession; |
|
| 117 | - $this->appManager = $appManager; |
|
| 118 | - $this->rootFolder = $rootFolder; |
|
| 119 | - $this->activityHelper = $activityHelper; |
|
| 120 | - $this->initialState = $initialState; |
|
| 121 | - $this->templateManager = $templateManager; |
|
| 122 | - $this->shareManager = $shareManager; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * @param string $appName |
|
| 127 | - * @param string $scriptName |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - protected function renderScript($appName, $scriptName) { |
|
| 131 | - $content = ''; |
|
| 132 | - $appPath = \OC_App::getAppPath($appName); |
|
| 133 | - $scriptPath = $appPath . '/' . $scriptName; |
|
| 134 | - if (file_exists($scriptPath)) { |
|
| 135 | - // TODO: sanitize path / script name ? |
|
| 136 | - ob_start(); |
|
| 137 | - include $scriptPath; |
|
| 138 | - $content = ob_get_contents(); |
|
| 139 | - @ob_end_clean(); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $content; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * FIXME: Replace with non static code |
|
| 147 | - * |
|
| 148 | - * @return array |
|
| 149 | - * @throws \OCP\Files\NotFoundException |
|
| 150 | - */ |
|
| 151 | - protected function getStorageInfo() { |
|
| 152 | - \OC_Util::setupFS(); |
|
| 153 | - $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false); |
|
| 154 | - |
|
| 155 | - return \OC_Helper::getStorageInfo('/', $dirInfo); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @NoCSRFRequired |
|
| 160 | - * @NoAdminRequired |
|
| 161 | - * |
|
| 162 | - * @param string $fileid |
|
| 163 | - * @return TemplateResponse|RedirectResponse |
|
| 164 | - * @throws NotFoundException |
|
| 165 | - */ |
|
| 166 | - public function showFile(string $fileid = null, int $openfile = 1): Response { |
|
| 167 | - // This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server. |
|
| 168 | - try { |
|
| 169 | - return $this->redirectToFile($fileid, $openfile !== 0); |
|
| 170 | - } catch (NotFoundException $e) { |
|
| 171 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @NoCSRFRequired |
|
| 177 | - * @NoAdminRequired |
|
| 178 | - * |
|
| 179 | - * @param string $dir |
|
| 180 | - * @param string $view |
|
| 181 | - * @param string $fileid |
|
| 182 | - * @param bool $fileNotFound |
|
| 183 | - * @param string $openfile - the openfile URL parameter if it was present in the initial request |
|
| 184 | - * @return TemplateResponse|RedirectResponse |
|
| 185 | - * @throws NotFoundException |
|
| 186 | - */ |
|
| 187 | - public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false, $openfile = null) { |
|
| 188 | - if ($fileid !== null && $dir === '') { |
|
| 189 | - try { |
|
| 190 | - return $this->redirectToFile($fileid); |
|
| 191 | - } catch (NotFoundException $e) { |
|
| 192 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); |
|
| 193 | - } |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - $nav = new \OCP\Template('files', 'appnavigation', ''); |
|
| 197 | - |
|
| 198 | - // Load the files we need |
|
| 199 | - \OCP\Util::addStyle('files', 'merged'); |
|
| 200 | - \OCP\Util::addScript('files', 'merged-index', 'files'); |
|
| 201 | - \OCP\Util::addScript('files', 'main'); |
|
| 202 | - |
|
| 203 | - // mostly for the home storage's free space |
|
| 204 | - // FIXME: Make non static |
|
| 205 | - $storageInfo = $this->getStorageInfo(); |
|
| 206 | - |
|
| 207 | - $user = $this->userSession->getUser()->getUID(); |
|
| 208 | - |
|
| 209 | - // Get all the user favorites to create a submenu |
|
| 210 | - try { |
|
| 211 | - $favElements = $this->activityHelper->getFavoriteFilePaths($this->userSession->getUser()->getUID()); |
|
| 212 | - } catch (\RuntimeException $e) { |
|
| 213 | - $favElements['folders'] = []; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - $collapseClasses = ''; |
|
| 217 | - if (count($favElements['folders']) > 0) { |
|
| 218 | - $collapseClasses = 'collapsible'; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - $favoritesSublistArray = []; |
|
| 222 | - |
|
| 223 | - $navBarPositionPosition = 6; |
|
| 224 | - $currentCount = 0; |
|
| 225 | - foreach ($favElements['folders'] as $favElement) { |
|
| 226 | - $link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $favElement, 'view' => 'files']); |
|
| 227 | - $sortingValue = ++$currentCount; |
|
| 228 | - $element = [ |
|
| 229 | - 'id' => str_replace('/', '-', $favElement), |
|
| 230 | - 'view' => 'files', |
|
| 231 | - 'href' => $link, |
|
| 232 | - 'dir' => $favElement, |
|
| 233 | - 'order' => $navBarPositionPosition, |
|
| 234 | - 'folderPosition' => $sortingValue, |
|
| 235 | - 'name' => basename($favElement), |
|
| 236 | - 'icon' => 'files', |
|
| 237 | - 'quickaccesselement' => 'true' |
|
| 238 | - ]; |
|
| 239 | - |
|
| 240 | - array_push($favoritesSublistArray, $element); |
|
| 241 | - $navBarPositionPosition++; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - $navItems = \OCA\Files\App::getNavigationManager()->getAll(); |
|
| 245 | - |
|
| 246 | - // add the favorites entry in menu |
|
| 247 | - $navItems['favorites']['sublist'] = $favoritesSublistArray; |
|
| 248 | - $navItems['favorites']['classes'] = $collapseClasses; |
|
| 249 | - |
|
| 250 | - // parse every menu and add the expandedState user value |
|
| 251 | - foreach ($navItems as $key => $item) { |
|
| 252 | - if (isset($item['expandedState'])) { |
|
| 253 | - $navItems[$key]['defaultExpandedState'] = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', $item['expandedState'], '0') === '1'; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - $nav->assign('navigationItems', $navItems); |
|
| 258 | - |
|
| 259 | - $nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used'])); |
|
| 260 | - if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 261 | - $totalSpace = $this->l10n->t('Unlimited'); |
|
| 262 | - } else { |
|
| 263 | - $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']); |
|
| 264 | - } |
|
| 265 | - $nav->assign('total_space', $totalSpace); |
|
| 266 | - $nav->assign('quota', $storageInfo['quota']); |
|
| 267 | - $nav->assign('usage_relative', $storageInfo['relative']); |
|
| 268 | - |
|
| 269 | - $nav->assign('webdav_url', \OCP\Util::linkToRemote('dav/files/' . $user)); |
|
| 270 | - |
|
| 271 | - $contentItems = []; |
|
| 272 | - |
|
| 273 | - // render the container content for every navigation item |
|
| 274 | - foreach ($navItems as $item) { |
|
| 275 | - $content = ''; |
|
| 276 | - if (isset($item['script'])) { |
|
| 277 | - $content = $this->renderScript($item['appname'], $item['script']); |
|
| 278 | - } |
|
| 279 | - // parse submenus |
|
| 280 | - if (isset($item['sublist'])) { |
|
| 281 | - foreach ($item['sublist'] as $subitem) { |
|
| 282 | - $subcontent = ''; |
|
| 283 | - if (isset($subitem['script'])) { |
|
| 284 | - $subcontent = $this->renderScript($subitem['appname'], $subitem['script']); |
|
| 285 | - } |
|
| 286 | - $contentItems[$subitem['id']] = [ |
|
| 287 | - 'id' => $subitem['id'], |
|
| 288 | - 'content' => $subcontent |
|
| 289 | - ]; |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - $contentItems[$item['id']] = [ |
|
| 293 | - 'id' => $item['id'], |
|
| 294 | - 'content' => $content |
|
| 295 | - ]; |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - $this->eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent()); |
|
| 299 | - $event = new LoadAdditionalScriptsEvent(); |
|
| 300 | - $this->eventDispatcher->dispatchTyped($event); |
|
| 301 | - $this->eventDispatcher->dispatchTyped(new LoadSidebar()); |
|
| 302 | - // Load Viewer scripts |
|
| 303 | - if (class_exists(LoadViewer::class)) { |
|
| 304 | - $this->eventDispatcher->dispatchTyped(new LoadViewer()); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - $this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false); |
|
| 308 | - $this->initialState->provideInitialState('templates', $this->templateManager->listCreators()); |
|
| 309 | - |
|
| 310 | - $params = []; |
|
| 311 | - $params['usedSpacePercent'] = (int) $storageInfo['relative']; |
|
| 312 | - $params['owner'] = $storageInfo['owner'] ?? ''; |
|
| 313 | - $params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? ''; |
|
| 314 | - $params['isPublic'] = false; |
|
| 315 | - $params['allowShareWithLink'] = $this->shareManager->shareApiAllowLinks() ? 'yes' : 'no'; |
|
| 316 | - $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name'); |
|
| 317 | - $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc'); |
|
| 318 | - $params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false); |
|
| 319 | - $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false); |
|
| 320 | - $params['showHiddenFiles'] = $showHidden ? 1 : 0; |
|
| 321 | - $cropImagePreviews = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', true); |
|
| 322 | - $params['cropImagePreviews'] = $cropImagePreviews ? 1 : 0; |
|
| 323 | - $params['fileNotFound'] = $fileNotFound ? 1 : 0; |
|
| 324 | - $params['appNavigation'] = $nav; |
|
| 325 | - $params['appContents'] = $contentItems; |
|
| 326 | - $params['hiddenFields'] = $event->getHiddenFields(); |
|
| 327 | - |
|
| 328 | - $response = new TemplateResponse( |
|
| 329 | - $this->appName, |
|
| 330 | - 'index', |
|
| 331 | - $params |
|
| 332 | - ); |
|
| 333 | - $policy = new ContentSecurityPolicy(); |
|
| 334 | - $policy->addAllowedFrameDomain('\'self\''); |
|
| 335 | - $response->setContentSecurityPolicy($policy); |
|
| 336 | - |
|
| 337 | - $this->provideInitialState($dir, $openfile); |
|
| 338 | - |
|
| 339 | - return $response; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Add openFileInfo in initialState if $openfile is set. |
|
| 344 | - * @param string $dir - the ?dir= URL param |
|
| 345 | - * @param string $openfile - the ?openfile= URL param |
|
| 346 | - * @return void |
|
| 347 | - */ |
|
| 348 | - private function provideInitialState(string $dir, ?string $openfile): void { |
|
| 349 | - if ($openfile === null) { |
|
| 350 | - return; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - $user = $this->userSession->getUser(); |
|
| 354 | - |
|
| 355 | - if ($user === null) { |
|
| 356 | - return; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - $uid = $user->getUID(); |
|
| 360 | - $userFolder = $this->rootFolder->getUserFolder($uid); |
|
| 361 | - $nodes = $userFolder->getById((int) $openfile); |
|
| 362 | - $node = array_shift($nodes); |
|
| 363 | - |
|
| 364 | - if ($node === null) { |
|
| 365 | - return; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - // properly format full path and make sure |
|
| 369 | - // we're relative to the user home folder |
|
| 370 | - $isRoot = $node === $userFolder; |
|
| 371 | - $path = $userFolder->getRelativePath($node->getPath()); |
|
| 372 | - $directory = $userFolder->getRelativePath($node->getParent()->getPath()); |
|
| 373 | - |
|
| 374 | - // Prevent opening a file from another folder. |
|
| 375 | - if ($dir !== $directory) { |
|
| 376 | - return; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - $this->initialState->provideInitialState( |
|
| 380 | - 'openFileInfo', [ |
|
| 381 | - 'id' => $node->getId(), |
|
| 382 | - 'name' => $isRoot ? '' : $node->getName(), |
|
| 383 | - 'path' => $path, |
|
| 384 | - 'directory' => $directory, |
|
| 385 | - 'mime' => $node->getMimetype(), |
|
| 386 | - 'type' => $node->getType(), |
|
| 387 | - 'permissions' => $node->getPermissions(), |
|
| 388 | - ] |
|
| 389 | - ); |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Redirects to the file list and highlight the given file id |
|
| 394 | - * |
|
| 395 | - * @param string $fileId file id to show |
|
| 396 | - * @param bool $setOpenfile - whether or not to set the openfile URL parameter |
|
| 397 | - * @return RedirectResponse redirect response or not found response |
|
| 398 | - * @throws \OCP\Files\NotFoundException |
|
| 399 | - */ |
|
| 400 | - private function redirectToFile($fileId, bool $setOpenfile = false) { |
|
| 401 | - $uid = $this->userSession->getUser()->getUID(); |
|
| 402 | - $baseFolder = $this->rootFolder->getUserFolder($uid); |
|
| 403 | - $files = $baseFolder->getById($fileId); |
|
| 404 | - $params = []; |
|
| 405 | - |
|
| 406 | - if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) { |
|
| 407 | - $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); |
|
| 408 | - $files = $baseFolder->getById($fileId); |
|
| 409 | - $params['view'] = 'trashbin'; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - if (!empty($files)) { |
|
| 413 | - $file = current($files); |
|
| 414 | - if ($file instanceof Folder) { |
|
| 415 | - // set the full path to enter the folder |
|
| 416 | - $params['dir'] = $baseFolder->getRelativePath($file->getPath()); |
|
| 417 | - } else { |
|
| 418 | - // set parent path as dir |
|
| 419 | - $params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath()); |
|
| 420 | - // and scroll to the entry |
|
| 421 | - $params['scrollto'] = $file->getName(); |
|
| 422 | - |
|
| 423 | - if ($setOpenfile) { |
|
| 424 | - // forward the openfile URL parameter. |
|
| 425 | - $params['openfile'] = $fileId; |
|
| 426 | - } |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params)); |
|
| 430 | - } |
|
| 431 | - throw new \OCP\Files\NotFoundException(); |
|
| 432 | - } |
|
| 68 | + /** @var string */ |
|
| 69 | + protected $appName; |
|
| 70 | + /** @var IRequest */ |
|
| 71 | + protected $request; |
|
| 72 | + /** @var IURLGenerator */ |
|
| 73 | + protected $urlGenerator; |
|
| 74 | + /** @var IL10N */ |
|
| 75 | + protected $l10n; |
|
| 76 | + /** @var IConfig */ |
|
| 77 | + protected $config; |
|
| 78 | + /** @var IEventDispatcher */ |
|
| 79 | + protected $eventDispatcher; |
|
| 80 | + /** @var IUserSession */ |
|
| 81 | + protected $userSession; |
|
| 82 | + /** @var IAppManager */ |
|
| 83 | + protected $appManager; |
|
| 84 | + /** @var IRootFolder */ |
|
| 85 | + protected $rootFolder; |
|
| 86 | + /** @var Helper */ |
|
| 87 | + protected $activityHelper; |
|
| 88 | + /** @var IInitialState */ |
|
| 89 | + private $initialState; |
|
| 90 | + /** @var ITemplateManager */ |
|
| 91 | + private $templateManager; |
|
| 92 | + /** @var IManager */ |
|
| 93 | + private $shareManager; |
|
| 94 | + |
|
| 95 | + public function __construct(string $appName, |
|
| 96 | + IRequest $request, |
|
| 97 | + IURLGenerator $urlGenerator, |
|
| 98 | + IL10N $l10n, |
|
| 99 | + IConfig $config, |
|
| 100 | + IEventDispatcher $eventDispatcher, |
|
| 101 | + IUserSession $userSession, |
|
| 102 | + IAppManager $appManager, |
|
| 103 | + IRootFolder $rootFolder, |
|
| 104 | + Helper $activityHelper, |
|
| 105 | + IInitialState $initialState, |
|
| 106 | + ITemplateManager $templateManager, |
|
| 107 | + IManager $shareManager |
|
| 108 | + ) { |
|
| 109 | + parent::__construct($appName, $request); |
|
| 110 | + $this->appName = $appName; |
|
| 111 | + $this->request = $request; |
|
| 112 | + $this->urlGenerator = $urlGenerator; |
|
| 113 | + $this->l10n = $l10n; |
|
| 114 | + $this->config = $config; |
|
| 115 | + $this->eventDispatcher = $eventDispatcher; |
|
| 116 | + $this->userSession = $userSession; |
|
| 117 | + $this->appManager = $appManager; |
|
| 118 | + $this->rootFolder = $rootFolder; |
|
| 119 | + $this->activityHelper = $activityHelper; |
|
| 120 | + $this->initialState = $initialState; |
|
| 121 | + $this->templateManager = $templateManager; |
|
| 122 | + $this->shareManager = $shareManager; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * @param string $appName |
|
| 127 | + * @param string $scriptName |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + protected function renderScript($appName, $scriptName) { |
|
| 131 | + $content = ''; |
|
| 132 | + $appPath = \OC_App::getAppPath($appName); |
|
| 133 | + $scriptPath = $appPath . '/' . $scriptName; |
|
| 134 | + if (file_exists($scriptPath)) { |
|
| 135 | + // TODO: sanitize path / script name ? |
|
| 136 | + ob_start(); |
|
| 137 | + include $scriptPath; |
|
| 138 | + $content = ob_get_contents(); |
|
| 139 | + @ob_end_clean(); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return $content; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * FIXME: Replace with non static code |
|
| 147 | + * |
|
| 148 | + * @return array |
|
| 149 | + * @throws \OCP\Files\NotFoundException |
|
| 150 | + */ |
|
| 151 | + protected function getStorageInfo() { |
|
| 152 | + \OC_Util::setupFS(); |
|
| 153 | + $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false); |
|
| 154 | + |
|
| 155 | + return \OC_Helper::getStorageInfo('/', $dirInfo); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @NoCSRFRequired |
|
| 160 | + * @NoAdminRequired |
|
| 161 | + * |
|
| 162 | + * @param string $fileid |
|
| 163 | + * @return TemplateResponse|RedirectResponse |
|
| 164 | + * @throws NotFoundException |
|
| 165 | + */ |
|
| 166 | + public function showFile(string $fileid = null, int $openfile = 1): Response { |
|
| 167 | + // This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server. |
|
| 168 | + try { |
|
| 169 | + return $this->redirectToFile($fileid, $openfile !== 0); |
|
| 170 | + } catch (NotFoundException $e) { |
|
| 171 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @NoCSRFRequired |
|
| 177 | + * @NoAdminRequired |
|
| 178 | + * |
|
| 179 | + * @param string $dir |
|
| 180 | + * @param string $view |
|
| 181 | + * @param string $fileid |
|
| 182 | + * @param bool $fileNotFound |
|
| 183 | + * @param string $openfile - the openfile URL parameter if it was present in the initial request |
|
| 184 | + * @return TemplateResponse|RedirectResponse |
|
| 185 | + * @throws NotFoundException |
|
| 186 | + */ |
|
| 187 | + public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false, $openfile = null) { |
|
| 188 | + if ($fileid !== null && $dir === '') { |
|
| 189 | + try { |
|
| 190 | + return $this->redirectToFile($fileid); |
|
| 191 | + } catch (NotFoundException $e) { |
|
| 192 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); |
|
| 193 | + } |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + $nav = new \OCP\Template('files', 'appnavigation', ''); |
|
| 197 | + |
|
| 198 | + // Load the files we need |
|
| 199 | + \OCP\Util::addStyle('files', 'merged'); |
|
| 200 | + \OCP\Util::addScript('files', 'merged-index', 'files'); |
|
| 201 | + \OCP\Util::addScript('files', 'main'); |
|
| 202 | + |
|
| 203 | + // mostly for the home storage's free space |
|
| 204 | + // FIXME: Make non static |
|
| 205 | + $storageInfo = $this->getStorageInfo(); |
|
| 206 | + |
|
| 207 | + $user = $this->userSession->getUser()->getUID(); |
|
| 208 | + |
|
| 209 | + // Get all the user favorites to create a submenu |
|
| 210 | + try { |
|
| 211 | + $favElements = $this->activityHelper->getFavoriteFilePaths($this->userSession->getUser()->getUID()); |
|
| 212 | + } catch (\RuntimeException $e) { |
|
| 213 | + $favElements['folders'] = []; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + $collapseClasses = ''; |
|
| 217 | + if (count($favElements['folders']) > 0) { |
|
| 218 | + $collapseClasses = 'collapsible'; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + $favoritesSublistArray = []; |
|
| 222 | + |
|
| 223 | + $navBarPositionPosition = 6; |
|
| 224 | + $currentCount = 0; |
|
| 225 | + foreach ($favElements['folders'] as $favElement) { |
|
| 226 | + $link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $favElement, 'view' => 'files']); |
|
| 227 | + $sortingValue = ++$currentCount; |
|
| 228 | + $element = [ |
|
| 229 | + 'id' => str_replace('/', '-', $favElement), |
|
| 230 | + 'view' => 'files', |
|
| 231 | + 'href' => $link, |
|
| 232 | + 'dir' => $favElement, |
|
| 233 | + 'order' => $navBarPositionPosition, |
|
| 234 | + 'folderPosition' => $sortingValue, |
|
| 235 | + 'name' => basename($favElement), |
|
| 236 | + 'icon' => 'files', |
|
| 237 | + 'quickaccesselement' => 'true' |
|
| 238 | + ]; |
|
| 239 | + |
|
| 240 | + array_push($favoritesSublistArray, $element); |
|
| 241 | + $navBarPositionPosition++; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + $navItems = \OCA\Files\App::getNavigationManager()->getAll(); |
|
| 245 | + |
|
| 246 | + // add the favorites entry in menu |
|
| 247 | + $navItems['favorites']['sublist'] = $favoritesSublistArray; |
|
| 248 | + $navItems['favorites']['classes'] = $collapseClasses; |
|
| 249 | + |
|
| 250 | + // parse every menu and add the expandedState user value |
|
| 251 | + foreach ($navItems as $key => $item) { |
|
| 252 | + if (isset($item['expandedState'])) { |
|
| 253 | + $navItems[$key]['defaultExpandedState'] = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', $item['expandedState'], '0') === '1'; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + $nav->assign('navigationItems', $navItems); |
|
| 258 | + |
|
| 259 | + $nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used'])); |
|
| 260 | + if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 261 | + $totalSpace = $this->l10n->t('Unlimited'); |
|
| 262 | + } else { |
|
| 263 | + $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']); |
|
| 264 | + } |
|
| 265 | + $nav->assign('total_space', $totalSpace); |
|
| 266 | + $nav->assign('quota', $storageInfo['quota']); |
|
| 267 | + $nav->assign('usage_relative', $storageInfo['relative']); |
|
| 268 | + |
|
| 269 | + $nav->assign('webdav_url', \OCP\Util::linkToRemote('dav/files/' . $user)); |
|
| 270 | + |
|
| 271 | + $contentItems = []; |
|
| 272 | + |
|
| 273 | + // render the container content for every navigation item |
|
| 274 | + foreach ($navItems as $item) { |
|
| 275 | + $content = ''; |
|
| 276 | + if (isset($item['script'])) { |
|
| 277 | + $content = $this->renderScript($item['appname'], $item['script']); |
|
| 278 | + } |
|
| 279 | + // parse submenus |
|
| 280 | + if (isset($item['sublist'])) { |
|
| 281 | + foreach ($item['sublist'] as $subitem) { |
|
| 282 | + $subcontent = ''; |
|
| 283 | + if (isset($subitem['script'])) { |
|
| 284 | + $subcontent = $this->renderScript($subitem['appname'], $subitem['script']); |
|
| 285 | + } |
|
| 286 | + $contentItems[$subitem['id']] = [ |
|
| 287 | + 'id' => $subitem['id'], |
|
| 288 | + 'content' => $subcontent |
|
| 289 | + ]; |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + $contentItems[$item['id']] = [ |
|
| 293 | + 'id' => $item['id'], |
|
| 294 | + 'content' => $content |
|
| 295 | + ]; |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + $this->eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent()); |
|
| 299 | + $event = new LoadAdditionalScriptsEvent(); |
|
| 300 | + $this->eventDispatcher->dispatchTyped($event); |
|
| 301 | + $this->eventDispatcher->dispatchTyped(new LoadSidebar()); |
|
| 302 | + // Load Viewer scripts |
|
| 303 | + if (class_exists(LoadViewer::class)) { |
|
| 304 | + $this->eventDispatcher->dispatchTyped(new LoadViewer()); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + $this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false); |
|
| 308 | + $this->initialState->provideInitialState('templates', $this->templateManager->listCreators()); |
|
| 309 | + |
|
| 310 | + $params = []; |
|
| 311 | + $params['usedSpacePercent'] = (int) $storageInfo['relative']; |
|
| 312 | + $params['owner'] = $storageInfo['owner'] ?? ''; |
|
| 313 | + $params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? ''; |
|
| 314 | + $params['isPublic'] = false; |
|
| 315 | + $params['allowShareWithLink'] = $this->shareManager->shareApiAllowLinks() ? 'yes' : 'no'; |
|
| 316 | + $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name'); |
|
| 317 | + $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc'); |
|
| 318 | + $params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false); |
|
| 319 | + $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false); |
|
| 320 | + $params['showHiddenFiles'] = $showHidden ? 1 : 0; |
|
| 321 | + $cropImagePreviews = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', true); |
|
| 322 | + $params['cropImagePreviews'] = $cropImagePreviews ? 1 : 0; |
|
| 323 | + $params['fileNotFound'] = $fileNotFound ? 1 : 0; |
|
| 324 | + $params['appNavigation'] = $nav; |
|
| 325 | + $params['appContents'] = $contentItems; |
|
| 326 | + $params['hiddenFields'] = $event->getHiddenFields(); |
|
| 327 | + |
|
| 328 | + $response = new TemplateResponse( |
|
| 329 | + $this->appName, |
|
| 330 | + 'index', |
|
| 331 | + $params |
|
| 332 | + ); |
|
| 333 | + $policy = new ContentSecurityPolicy(); |
|
| 334 | + $policy->addAllowedFrameDomain('\'self\''); |
|
| 335 | + $response->setContentSecurityPolicy($policy); |
|
| 336 | + |
|
| 337 | + $this->provideInitialState($dir, $openfile); |
|
| 338 | + |
|
| 339 | + return $response; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Add openFileInfo in initialState if $openfile is set. |
|
| 344 | + * @param string $dir - the ?dir= URL param |
|
| 345 | + * @param string $openfile - the ?openfile= URL param |
|
| 346 | + * @return void |
|
| 347 | + */ |
|
| 348 | + private function provideInitialState(string $dir, ?string $openfile): void { |
|
| 349 | + if ($openfile === null) { |
|
| 350 | + return; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + $user = $this->userSession->getUser(); |
|
| 354 | + |
|
| 355 | + if ($user === null) { |
|
| 356 | + return; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + $uid = $user->getUID(); |
|
| 360 | + $userFolder = $this->rootFolder->getUserFolder($uid); |
|
| 361 | + $nodes = $userFolder->getById((int) $openfile); |
|
| 362 | + $node = array_shift($nodes); |
|
| 363 | + |
|
| 364 | + if ($node === null) { |
|
| 365 | + return; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + // properly format full path and make sure |
|
| 369 | + // we're relative to the user home folder |
|
| 370 | + $isRoot = $node === $userFolder; |
|
| 371 | + $path = $userFolder->getRelativePath($node->getPath()); |
|
| 372 | + $directory = $userFolder->getRelativePath($node->getParent()->getPath()); |
|
| 373 | + |
|
| 374 | + // Prevent opening a file from another folder. |
|
| 375 | + if ($dir !== $directory) { |
|
| 376 | + return; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + $this->initialState->provideInitialState( |
|
| 380 | + 'openFileInfo', [ |
|
| 381 | + 'id' => $node->getId(), |
|
| 382 | + 'name' => $isRoot ? '' : $node->getName(), |
|
| 383 | + 'path' => $path, |
|
| 384 | + 'directory' => $directory, |
|
| 385 | + 'mime' => $node->getMimetype(), |
|
| 386 | + 'type' => $node->getType(), |
|
| 387 | + 'permissions' => $node->getPermissions(), |
|
| 388 | + ] |
|
| 389 | + ); |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Redirects to the file list and highlight the given file id |
|
| 394 | + * |
|
| 395 | + * @param string $fileId file id to show |
|
| 396 | + * @param bool $setOpenfile - whether or not to set the openfile URL parameter |
|
| 397 | + * @return RedirectResponse redirect response or not found response |
|
| 398 | + * @throws \OCP\Files\NotFoundException |
|
| 399 | + */ |
|
| 400 | + private function redirectToFile($fileId, bool $setOpenfile = false) { |
|
| 401 | + $uid = $this->userSession->getUser()->getUID(); |
|
| 402 | + $baseFolder = $this->rootFolder->getUserFolder($uid); |
|
| 403 | + $files = $baseFolder->getById($fileId); |
|
| 404 | + $params = []; |
|
| 405 | + |
|
| 406 | + if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) { |
|
| 407 | + $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); |
|
| 408 | + $files = $baseFolder->getById($fileId); |
|
| 409 | + $params['view'] = 'trashbin'; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + if (!empty($files)) { |
|
| 413 | + $file = current($files); |
|
| 414 | + if ($file instanceof Folder) { |
|
| 415 | + // set the full path to enter the folder |
|
| 416 | + $params['dir'] = $baseFolder->getRelativePath($file->getPath()); |
|
| 417 | + } else { |
|
| 418 | + // set parent path as dir |
|
| 419 | + $params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath()); |
|
| 420 | + // and scroll to the entry |
|
| 421 | + $params['scrollto'] = $file->getName(); |
|
| 422 | + |
|
| 423 | + if ($setOpenfile) { |
|
| 424 | + // forward the openfile URL parameter. |
|
| 425 | + $params['openfile'] = $fileId; |
|
| 426 | + } |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params)); |
|
| 430 | + } |
|
| 431 | + throw new \OCP\Files\NotFoundException(); |
|
| 432 | + } |
|
| 433 | 433 | } |