Skip to content

OPDS Plugin: Ensure the default download filename is consistent across different platforms.#13709

Merged
Frenzie merged 5 commits into
koreader:masterfrom
spfenwick:master
Aug 16, 2025
Merged

OPDS Plugin: Ensure the default download filename is consistent across different platforms.#13709
Frenzie merged 5 commits into
koreader:masterfrom
spfenwick:master

Conversation

@spfenwick

@spfenwick spfenwick commented May 2, 2025

Copy link
Copy Markdown
Contributor

The OPDS plugin uses util.getSafeFilename() to remove any special characters from the filename for downloads. Tthis function works differently on different platforms, with some characters that are common in book titles (like : and ?) being allowed on some platforms but replaced with _ on others. In particular this breaks Progress Sync if it is configured to rely on filenames and the book title contains any of those special characters.

If the user knows what the problem is it's easy enough to work around by changing the filename, but the default behaviour of silently failing to sync is not a good user experience.

This change gives a better default behaviour by ensuring the default filename when the file is downloaded via OPDS is the same across different platforms, while still letting the user change the filename to include special characters if the platform supports them.


This change is Reviewable

spfenwick added 3 commits May 2, 2025 17:03
…latforms.

The OPDS plugin correctly uses util.getSafeFilename() to remove any special characters from the filename for downloads. However this function works differently on different platforms, with some common characters like : and ? being allowed on some platforms but not others. In particular this may break Process Sync if it is configured to rely on file names. This change gives a better default behaviour by ensuring the default filename is the same across different platforms, while still letting the user change the filename to include special characters if the platform supports them.
Comment thread plugins/opds.koplugin/opdsbrowser.lua Outdated
end
-- Ensure the default download filename is consistent across different platforms. In particular
-- this is needed for Progress Sync to work correctly if it is configured to rely on file names.
filename = filename:gsub('[\\,%/,:,%*,%?,%",%<,%>,%|]','_')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Given the explanation in the PR I was expecting something more along the lines of exporting the replaceAllInvalidChars function and updating wherever util.getSafeFilename() is used to use that instead. Because now you're first doing this and still doing util.getSafeFilename() again unnecessarily later. Or just to add an extra boolean to util.getSafeFilename() for example.

koreader/frontend/util.lua

Lines 949 to 960 in acbde33

--- Replaces characters that are invalid filenames.
--
-- Replaces the characters <code>\/:*?"<>|</code> with an <code>_</code>.
-- These characters are problematic on Windows filesystems. On Linux only
-- <code>/</code> poses a problem.
---- @string str filename
---- @treturn string sanitized filename
local function replaceAllInvalidChars(str)
if str then
return str:gsub('[\\,%/,:,%*,%?,%",%<,%>,%|]','_')
end
end

But that aside, I suppose this same concern would apply just about anywhere the function is used, wouldn't it?

@spfenwick

Copy link
Copy Markdown
Contributor Author

My first thought was to just have util.getSafeFilename() always call replaceAllInvalidChars() regardless of the platform or filesystem. That would be simplest and most consistent. I didn't go with that because it would mean characters like : and ? can never be used in filenames even on platforms were they don't cause a problem. It's not unusual to have those characters in book titles so not being able to use them at all in file names could annoy people.

The line I added is prior to the Save File dialog box and ensures the default filename is consistent. The later call toutil.getSafeFilename() happens after the user has possibly changed the filename, so isn't strictly unnecessary.

I don't think the inconsistency is a concern when the user is actually choosing a filename. It's really just where KOReader chooses a default filename - certainly here in the OPDS plugin, possibly in Cloud Storage and Wallabag but I haven't looked at those. There may be other places too.

Thinking about this more it may make most sense to go back to my first idea and just make the filename rules the same across all platforms. I'm open to suggestions on that.

@Frenzie

Frenzie commented May 3, 2025

Copy link
Copy Markdown
Member

I didn't go with that because it would mean characters like : and ? can never be used in filenames even on platforms were they don't cause a problem.

That's why it is the way it is, yes. I'd rather keep it that way, but I don't think I'd considered this scenario.

For the moment can you update it to use util.replaceAllInvalidChars()? (No longer local.) I think that'd be the proper version of this PR as it's currently written, rather than copy pasting it.

@spfenwick

Copy link
Copy Markdown
Contributor Author

Good suggestion. I've just pushed that change.

@Frenzie Frenzie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fine by me, but I'm waiting to merge for a bit to allow for more comments (if any).

@Frenzie Frenzie added this to the 2025.05 milestone May 3, 2025
@Frenzie Frenzie added the Plugin label May 3, 2025
@Frenzie

Frenzie commented Aug 11, 2025

Copy link
Copy Markdown
Member

@spfenwick Sorry, I forgot to merge. Would you mind double-checking it's correctly reconciled with #13946?

@Frenzie Frenzie merged commit c00df15 into koreader:master Aug 16, 2025
4 checks passed
0xstillb pushed a commit to 0xstillb/koreader-thai that referenced this pull request May 9, 2026
…s different platforms (koreader#13709)

* Ensure the default download filename is consistent across different platforms.
The OPDS plugin correctly uses util.getSafeFilename() to remove any special characters from the filename for downloads. However this function works differently on different platforms, with some common characters like : and ? being allowed on some platforms but not others. In particular this may break Process Sync if it is configured to rely on file names. This change gives a better default behaviour by ensuring the default filename is the same across different platforms, while still letting the user change the filename to include special characters if the platform supports them.

* Make util.replaceAllInvalidChars() global and call it rather than copying code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants