Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trigger the default action when openfile URL param is set #27102

Merged
merged 1 commit into from
Jun 10, 2021
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
1 change: 1 addition & 0 deletions apps/files/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
fileActions: fileActions,
allowLegacyActions: true,
scrollTo: urlParams.scrollto,
openFile: urlParams.openfile,
filesClient: OC.Files.getClient(),
multiSelectMenu: [
{
Expand Down
6 changes: 3 additions & 3 deletions apps/files/js/dist/sidebar.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/files/js/dist/sidebar.js.map

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions apps/files/js/fileactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,19 @@
permissions: OC.PERMISSION_READ,
icon: '',
actionHandler: function (filename, context) {
var dir = context.$file.attr('data-path') || context.fileList.getCurrentDirectory();
let dir, id
if (context.$file) {
dir = context.$file.attr('data-path')
id = context.$file.attr('data-id')
} else {
dir = context.fileList.getCurrentDirectory()
id = context.fileId
}
if (OCA.Files.App && OCA.Files.App.getActiveView() !== 'files') {
OCA.Files.App.setActiveView('files', {silent: true});
OCA.Files.App.fileList.changeDirectory(OC.joinPaths(dir, filename), true, true);
} else {
context.fileList.changeDirectory(OC.joinPaths(dir, filename), true, false, parseInt(context.$file.attr('data-id'), 10));
context.fileList.changeDirectory(OC.joinPaths(dir, filename), true, false, parseInt(id, 10));
}
},
displayName: t('files', 'Open')
Expand Down
50 changes: 25 additions & 25 deletions apps/files/js/filelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,31 @@
});
}

if(options.openFile) {
// Wait for some initialisation process to be over before triggering the default action.
_.defer(() => {
try {
var fileInfo = JSON.parse(atob($('#initial-state-files-openFileInfo').val()))
var spec = this.fileActions.getDefaultFileAction(fileInfo.mime, fileInfo.type, fileInfo.permissions)
if (spec && spec.action) {
spec.action(fileInfo.name, {
fileId: fileInfo.id,
fileList: this,
fileActions: this.fileActions,
dir: fileInfo.directory
});
} else {
var url = this.getDownloadUrl(fileInfo.name, fileInfo.dir, true);
OCA.Files.Files.handleDownload(url);
}

OCA.Files.Sidebar.open(fileInfo.path);
} catch (error) {
console.error(`Failed to trigger default action on the file for URL: ${location.href}`, error)
}
})
}

this._operationProgressBar = new OCA.Files.OperationProgressBar();
this._operationProgressBar.render();
this.$el.find('#uploadprogresswrapper').replaceWith(this._operationProgressBar.$el);
Expand Down Expand Up @@ -1320,31 +1345,6 @@
}, 0);
}

if(!this.triedActionOnce) {
var id = OC.Util.History.parseUrlQuery().openfile;
if (id) {
var $tr = this.$fileList.children().filterAttr('data-id', '' + id);
var filename = $tr.attr('data-file');
this.fileActions.currentFile = $tr.find('td');
var dir = $tr.attr('data-path') || this.getCurrentDirectory();
var spec = this.fileActions.getCurrentDefaultFileAction();
if (spec && spec.action) {
spec.action(filename, {
$file: $tr,
fileList: this,
fileActions: this.fileActions,
dir: dir
});

}
else {
var url = this.getDownloadUrl(filename, dir, true);
OCA.Files.Files.handleDownload(url);
}
}
this.triedActionOnce = true;
}

return newTrs;
},

Expand Down
55 changes: 54 additions & 1 deletion apps/files/lib/Controller/ViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ public function showFile(string $fileid = null): Response {
* @param string $view
* @param string $fileid
* @param bool $fileNotFound
* @param string $openfile
* @return TemplateResponse|RedirectResponse
* @throws NotFoundException
*/
public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false) {
public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false, $openfile = null) {
if ($fileid !== null) {
try {
return $this->redirectToFile($fileid);
Expand Down Expand Up @@ -331,9 +332,61 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
$policy->addAllowedFrameDomain('\'self\'');
$response->setContentSecurityPolicy($policy);

$this->provideInitialState($dir, $openfile);

return $response;
}

/**
* Add openFileInfo in initialState if $openfile is set.
* @param string $dir - the ?dir= URL param
* @param string $openfile - the ?openfile= URL param
* @return void
*/
private function provideInitialState(string $dir, ?string $openfile): void {
if ($openfile === null) {
return;
}

$user = $this->userSession->getUser();

if ($user === null) {
return;
}

$uid = $user->getUID();
$userFolder = $this->rootFolder->getUserFolder($uid);
$nodes = $userFolder->getById((int) $openfile);
$node = array_shift($nodes);

if ($node === null) {
return;
}

// properly format full path and make sure
// we're relative to the user home folder
$isRoot = $node === $userFolder;
$path = $userFolder->getRelativePath($node->getPath());
$directory = $userFolder->getRelativePath($node->getParent()->getPath());

// Prevent opening a file from another folder.
if ($dir !== $directory) {
return;
}

$this->initialState->provideInitialState(
'openFileInfo', [
'id' => $node->getId(),
'name' => $isRoot ? '' : $node->getName(),
'path' => $path,
'directory' => $directory,
'mime' => $node->getMimetype(),
'type' => $node->getType(),
'permissions' => $node->getPermissions(),
]
);
}

/**
* Redirects to the file list and highlight the given file id
*
Expand Down
1 change: 1 addition & 0 deletions apps/files/lib/Search/FilesSearchProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public function search(IUser $user, ISearchQuery $query): SearchResult {
[
'dir' => dirname($path),
'scrollto' => $result->getName(),
'openfile' => $result->getId(),
]
);

Expand Down
1 change: 1 addition & 0 deletions apps/files/src/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ window.addEventListener('DOMContentLoaded', function() {
AppSidebar.$mount('#app-sidebar')
window.OCA.Files.Sidebar.open = AppSidebar.open
window.OCA.Files.Sidebar.close = AppSidebar.close
window.OCA.Files.Sidebar.setFullScreenMode = AppSidebar.setFullScreenMode
})
23 changes: 21 additions & 2 deletions apps/files/src/views/Sidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export default {
loading: true,
fileInfo: null,
starLoading: false,
isFullScreen: false,
}
},

Expand Down Expand Up @@ -202,7 +203,10 @@ export default {
'star-loading': this.starLoading,
active: this.activeTab,
background: this.background,
class: { 'has-preview': this.fileInfo.hasPreview },
class: {
'app-sidebar--has-preview': this.fileInfo.hasPreview,
'app-sidebar--full': this.isFullScreen,
artonge marked this conversation as resolved.
Show resolved Hide resolved
},
compact: !this.fileInfo.hasPreview,
loading: this.loading,
starred: this.fileInfo.isFavourited,
Expand Down Expand Up @@ -428,6 +432,14 @@ export default {
this.resetData()
},

/**
* Allow to set the Sidebar as fullscreen from OCA.Files.Sidebar
* @param {boolean} isFullScreen - Wether or not to render the Sidebar in fullscreen.
*/
setFullScreenMode(isFullScreen) {
this.isFullScreen = isFullScreen
},

/**
* Emit SideBar events.
*/
Expand All @@ -448,7 +460,7 @@ export default {
</script>
<style lang="scss" scoped>
.app-sidebar {
&.has-preview::v-deep {
&--has-preview::v-deep {
.app-sidebar-header__figure {
background-size: cover;
}
Expand All @@ -460,5 +472,12 @@ export default {
}
}
}

&--full {
position: fixed !important;
z-index: 2025 !important;
top: 0 !important;
height: 100% !important;
}
}
</style>