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

Fix opening PDF files with special characters in their name #291

Merged

Conversation

danxuliu
Copy link
Member

Before the move to the viewer the URL of the PDF file to load was got (indirectly) using Files.getDownloadUrl(), which encodes each section of the path as a URI component. The full URL, in turn, was again encoded as a URI component to set the source of the iframe in which the PDF viewer is loaded. When the PDF viewer parsed the URL it decoded it once, so each section of the path was still encoded as a URI component.

After the move to the viewer the URL of the PDF file to load was got from the davPath property set by the viewer, which uses the filename as is. The full URL was then encoded as a URI component, so when the PDF viewer parsed and decoded the URL the raw filename was used. In most cases it worked fine, but if the filename included some special character, like ?, the file failed to load.

Now, instead of using the davPath property set by the viewer, each section of the dav path is encoded as a URI component. The encoding tries to not make any assumption on the format used by the viewer to avoid being coupled too much.

I do not know if this should be something to be fixed in the viewer instead (although images with "?" in their name load fine 🤷). However, even in that case, this should fix the PDF viewer until the changes in the viewer are ready.

How to test

  • Open the Files app
  • Upload a PDF file
  • Rename it to test?.pdf
  • Open the PDF file

Result with this pull request

The file is opened in the viewer.

Result without this pull request

The viewer opens, but the file fails to open.

Before the move to the viewer the URL of the PDF file to load was got
using "Files.getDownloadUrl()", which encodes each section of the path
as a URI component. The full URL, in turn, was again encoded as a URI
component to set the source of the iframe in which the PDF viewer is
loaded. When the PDF viewer parsed the URL it decoded it once, so each
section of the path was still encoded as a URI component.

After the move to the viewer the URL of the PDF file to load was got
from the "davPath" property set by the viewer, which uses the filename
as is. The full URL was then encoded as a URI component, so when the PDF
viewer parsed and decoded the URL the raw filename was used. In most
cases it worked fine, but if the filename included some special
character, like "?", the file failed to load.

Now, instead of using the "davPath" property set by the viewer, each
section of the dav path is encoded as a URI component. The encoding
tries to not make any assumption on the format used by the viewer to
avoid being coupled too much.

Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
@danxuliu
Copy link
Member Author

/backport to stable20

Comment on lines +40 to +61
encodedDavPath() {
const hasScheme = this.davPath.indexOf('://') !== -1

const pathSections = this.davPath.split('/')

// Ignore scheme and domain in the loop (note that the scheme
// delimiter, "//", creates an empty section when split by "/").
const initialSection = hasScheme ? 3 : 0

let encodedDavPath = ''
for (let i = initialSection; i < pathSections.length; i++) {
if (pathSections[i] !== '') {
encodedDavPath += '/' + encodeURIComponent(pathSections[i])
}
}

if (hasScheme) {
encodedDavPath = pathSections[0] + '//' + pathSections[2] + encodedDavPath
}

return encodedDavPath
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is: shouldn't this be on Viewer? we have the same issue for other files

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that question is in the pull request description...

Copy link
Member

@MorrisJobke MorrisJobke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and works 👍

@rullzer rullzer mentioned this pull request Jan 22, 2021
19 tasks
@rullzer
Copy link
Member

rullzer commented Jan 28, 2021

SO go or no go?

@danxuliu
Copy link
Member Author

I would say merge now and then adjust later if it is fixed in the viewer instead of the PDF viewer. Otherwise Nextcloud 21 could end shipping with the issue.

@danxuliu danxuliu merged commit 6d896a3 into master Jan 28, 2021
@delete-merged-branch delete-merged-branch bot deleted the fix-opening-pdf-files-with-special-characters-in-their-name branch January 28, 2021 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants