fix: truncate long image filenames when saving to prevent ENAMETOOLONG - #1324
Open
ibrahim-iqbal wants to merge 1 commit into
Open
fix: truncate long image filenames when saving to prevent ENAMETOOLONG#1324ibrahim-iqbal wants to merge 1 commit into
ibrahim-iqbal wants to merge 1 commit into
Conversation
URLUtil.guessFileName() can produce a filename longer than the 255-byte
limit ext4/f2fs enforces on Android, so saving an image with a long
title or URL-derived name crashes with
java.io.FileNotFoundException: open failed: ENAMETOOLONG
on ContentResolver.openFileDescriptor. Sanitize the name to at most
200 chars while preserving the file extension so the resulting file
still opens as an image.
Fixes ReadYouApp#1315.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1315.
`URLUtil.guessFileName()` derives the save-target name from the URL when the response doesn't carry an unambiguous `Content-Disposition`. For image URLs with long titles / hashed paths, that name can exceed the 255-byte filename limit ext4/f2fs enforces on Android. `ContentResolver.openFileDescriptor` then blows up with:
```
java.io.FileNotFoundException: open failed: ENAMETOOLONG (File name too long)
at me.ash.reader.infrastructure.android.AndroidImageDownloader.downloadImage2.invokeSuspend(AndroidImageDownloader.kt:181)
```
The tap-to-save flow is a 100% crash on any image whose guessed name goes past the limit — reproducer in #1315.
Added a small `sanitizeFileName` helper that caps the name at 200 chars (headroom under the 255-byte limit for typical single-byte scripts) while preserving the file extension so the saved file still opens as an image. Applied at the single call site right after `URLUtil.guessFileName` so both the Android Q+ MediaStore path and the pre-Q direct-file path get the sanitized name.
Test plan
Would need a device to verify end-to-end, which I don't have set up for this repo. Logically covered:
Happy to add a unit test if you'd like — didn't see an existing test file for AndroidImageDownloader in the app module, so didn't want to seed a new one without your steer.