-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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: decode media names before inserting into the database #18019
base: main
Are you sure you want to change the base?
fix: decode media names before inserting into the database #18019
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A description of the issue and how this fix it would be appreciated. You also said that you had thought about other ways to fix it. What are they?
@@ -2575,7 +2575,8 @@ class NoteEditor : | |||
|
|||
private fun updateField(field: FieldEditText?): Boolean { | |||
val fieldContent = field!!.text?.toString() ?: "" | |||
val correctedFieldContent = NoteService.convertToHtmlNewline(fieldContent, shouldReplaceNewlines()) | |||
// Decode the file name when preparing data for saveNote() to handle special characters correctly |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the //
should follow the surrounding indent
@@ -2575,7 +2575,8 @@ class NoteEditor : | |||
|
|||
private fun updateField(field: FieldEditText?): Boolean { | |||
val fieldContent = field!!.text?.toString() ?: "" | |||
val correctedFieldContent = NoteService.convertToHtmlNewline(fieldContent, shouldReplaceNewlines()) | |||
// Decode the file name when preparing data for saveNote() to handle special characters correctly | |||
val correctedFieldContent = Uri.decode(NoteService.convertToHtmlNewline(fieldContent, shouldReplaceNewlines())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this affect any kind of encoded content the user has added?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not aware of any instances where users would intentionally add encoded content, but this is something to consider. If necessary, we might need to ensure that only media filenames are decoded rather than applying decoding to all fields.
Do you know of any cases where users might intentionally add encoded content?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From memory, no. But in Anki there are always someone doing stuff like that
I have updated the PR description for better clarity. Regarding the second question, modifying the editorNote parameter anywhere before passing it to addNote() would effectively resolve the issue. Initially, I tried to fix it by updating the fields parameter in NoteEditor.kt at
But, this introduces an extra iteration, which is unnecessary. If needed, we can update editorNote at any suitable point before calling addNote() to optimize the fix. |
Issue:
Check Media shows incorrect values when media filenames contain special characters (spaces, %, &, =, etc.) on Android, while the desktop version works as expected.
Cause:
According to the [Anki manual](https://docs.ankiweb.net/media.html#media), filenames with spaces or special characters appear differently in the HTML editor compared to how they are stored on disk. For example,
hello 100%.jpg
is displayed ashello%20100%25.jpg
in the editor.HTML encoding is likely applied because certain characters have special meanings in URLs and HTML syntax. Without proper encoding, the card rendering engine may fail to locate or display media files with special characters in their names.
For instance, if a file named
test,.jpg
is referenced in a card, it gets encoded astest%2C.jpg
in the editor for proper rendering, while the actual file on disk remainstest,.jpg
.Observations:
collection.anki2
database (notes
table).Scrrenshot of the notes table :
This discrepancy causes the
check_media
function to return incorrect results for media files added from Android when their names include special characters.Approach
Now, while adding card in a deck
addNote()
function is calledhttps://github.com/Sahil06012002/Anki-Android/blob/fb3eec394463e19f0b3db3fdaa5b6b1eb156879a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt#L1198
that stores the note in the database.
Decoding the media filenames before storing them in the database resolves the issue.
Before calling
addNote(editorNote!!, deckId)
, thefields
parameter ineditorNote
is updated to replace encoded filenames with their decoded versions :https://github.com/Sahil06012002/Anki-Android/blob/fb3eec394463e19f0b3db3fdaa5b6b1eb156879a/AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt#L2579 .
Fixes
How Has This Been Tested?
https://github.com/user-attachments/assets/721fba7e-07d0-4523-a851-3f8bfc7f6910
Checklist