Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions apps/files/lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\Util;

/**
* @package OCA\Files\Controller
Expand Down Expand Up @@ -80,16 +81,18 @@ protected function getStorageInfo(string $dir = '/') {
*/
#[NoAdminRequired]
#[NoCSRFRequired]
public function showFile(?string $fileid = null): Response {
public function showFile(?string $fileid = null, ?string $openfile = null): Response {
if (!$fileid) {
return new RedirectResponse($this->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);
return $this->redirectToFile((int)$fileid, $openfile);
} catch (NotFoundException $e) {
return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true]));
// Keep the fileid even if not found, it will be used
// to detect the file could not be found and warn the user
return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.indexViewFileid', ['fileid' => $fileid, 'view' => 'files']));
}
}

Expand All @@ -98,49 +101,46 @@ public function showFile(?string $fileid = null): Response {
* @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);
public function indexView($dir = '', $view = '', $fileid = null) {
return $this->index($dir, $view, $fileid);
}

/**
* @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);
public function indexViewFileid($dir = '', $view = '', $fileid = null) {
return $this->index($dir, $view, $fileid);
}

/**
* @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) {
public function index($dir = '', $view = '', $fileid = null) {
if ($fileid !== null && $view !== 'trashbin') {
try {
return $this->redirectToFileIfInTrashbin((int) $fileid);
return $this->redirectToFileIfInTrashbin((int)$fileid);
} catch (NotFoundException $e) {
}
}

// Load the files we need
\OCP\Util::addInitScript('files', 'init');
\OCP\Util::addStyle('files', 'merged');
\OCP\Util::addScript('files', 'main');
Util::addInitScript('files', 'init');
Util::addStyle('files', 'merged');
Util::addScript('files', 'main');

$userId = $this->userSession->getUser()->getUID();

Expand All @@ -149,24 +149,22 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
// in the correct folder
if ($fileid && $dir !== '') {
$baseFolder = $this->rootFolder->getUserFolder($userId);
$nodes = $baseFolder->getById((int) $fileid);
$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);
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) {
} catch (\Exception $e) {
$storageInfo = $this->getStorageInfo();
}

Expand Down Expand Up @@ -247,10 +245,11 @@ private function redirectToFileIfInTrashbin($fileId): RedirectResponse {
* Redirects to the file list and highlight the given file id
*
* @param int $fileId file id to show
* @param string|null $openFile open file parameter
* @return RedirectResponse redirect response or not found response
* @throws NotFoundException
*/
private function redirectToFile(int $fileId) {
private function redirectToFile(int $fileId, ?string $openFile = null): RedirectResponse {
$uid = $this->userSession->getUser()->getUID();
$baseFolder = $this->rootFolder->getUserFolder($uid);
$node = $baseFolder->getFirstNodeById($fileId);
Expand All @@ -272,6 +271,13 @@ private function redirectToFile(int $fileId) {
// open the file by default (opening the viewer)
$params['openfile'] = 'true';
}

// Forward openfile parameters if any.
// It will be evaluated as truthy
if ($openFile !== null) {
$params['openfile'] = $openFile !== 'false' ? 'true' : 'false';
}

return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.indexViewFileid', $params));
}

Expand Down
3 changes: 3 additions & 0 deletions apps/files/src/components/FilesListVirtual.vue
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,10 @@ export default defineComponent({
if (node && sidebarAction?.enabled?.([node], this.currentView)) {
logger.debug('Opening sidebar on file ' + node.path, { node })
sidebarAction.exec(node, this.currentView, this.currentFolder.path)
return
}

logger.error(`Failed to open sidebar on file ${fileId}, file isn't cached yet !`, { fileId, node })
}
},

Expand Down
16 changes: 13 additions & 3 deletions apps/files/src/views/FilesList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -493,14 +493,24 @@ export default defineComponent({
},
},

mounted() {
this.fetchContent()

async mounted() {
subscribe('files:node:deleted', this.onNodeDeleted)
subscribe('files:node:updated', this.onUpdatedNode)

// reload on settings change
subscribe('files:config:updated', this.fetchContent)

// Finally, fetch the current directory contents
await this.fetchContent()
if (this.fileId) {
// If we have a fileId, let's check if the file exists
const node = this.dirContents.find(node => node.fileid.toString() === this.fileId.toString())
// If the file isn't in the current directory nor if
// the current directory is the file, we show an error
if (!node && this.currentFolder.fileid.toString() !== this.fileId.toString()) {
showError(t('files', 'The file could not be found'))
}
}
},

unmounted() {
Expand Down
Loading
Loading