Repository navigation
fix: Use POST instead of GET for multi-file downloads #56543
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| import type { IFileAction, INode, IView } from '@nextcloud/files' | ||
|
|
||
| import ArrowDownSvg from '@mdi/svg/svg/arrow-down.svg?raw' | ||
| import { getRequestToken } from '@nextcloud/auth' | ||
| import axios from '@nextcloud/axios' | ||
| import { showError } from '@nextcloud/dialogs' | ||
| import { emit } from '@nextcloud/event-bus' | ||
|
|
@@ -142,7 +143,41 @@ async function downloadNodes(nodes: INode[]) { | |
| // The URL contains the path encoded so we need to decode as the query.append will re-encode it | ||
| const filenames = nodes.map((node) => decodeURIComponent(node.encodedSource.slice(url.href.length + 1))) | ||
| url.searchParams.append('accept', 'zip') | ||
| url.searchParams.append('files', JSON.stringify(filenames)) | ||
|
|
||
| if (url.pathname.at(-1) !== '/') { | ||
| url.pathname = `${url.pathname}/` | ||
| } | ||
|
|
||
| // try to see if the resource is still available | ||
| await axios.head(url.href) | ||
|
|
||
| // Send the file list in the request body to avoid too long URLs. | ||
| // A native form submission lets the browser stream the archive to disk. | ||
| const form = document.createElement('form') | ||
| form.method = 'POST' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tested locally: zipping a folder that contains a view-only share returns a 403 here. With a form submit, wouldn't the browser end up on that error page instead of staying in Files?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit tricky, I will try to figure out a solution |
||
| // Pass the parameters in the body as POST requests with query parameters are not supported by all DAV plugins | ||
| url.search = '' | ||
| form.action = url.href | ||
| form.style.display = 'none' | ||
| const fields = { | ||
| accept: 'zip', | ||
| files: JSON.stringify(filenames), | ||
| requesttoken: getRequestToken() ?? '', | ||
| } | ||
| for (const [name, value] of Object.entries(fields)) { | ||
| const input = document.createElement('input') | ||
| input.type = 'hidden' | ||
| input.name = name | ||
| input.value = value | ||
| form.appendChild(input) | ||
| } | ||
| document.body.appendChild(form) | ||
| try { | ||
| form.submit() | ||
|
CarlSchwan marked this conversation as resolved.
|
||
| } finally { | ||
| form.remove() | ||
| } | ||
| return | ||
| } | ||
|
|
||
| if (url.pathname.at(-1) !== '/') { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.