Skip to content

Commit

Permalink
Fixed: Some EPUB files are not downloading.
Browse files Browse the repository at this point in the history
* The issue was epub fileName containing the colon ":" in it that is not supported by most of fileSystem, that's why it is not creating the file in fileSystem and we are not able to download the epub file.
* So to fix this we have improved our `getDecodedFileName` method which returns the fileName of the epub file, here we are removing the colon from fileName if any contains. For this change we have added the test cases as well for our `getDecodedFileName` function to properly test it.
* We also refined our downloadFileFromUrl method. Previously, the generateSequence function was used to create new files with underscores and incremented numbers, anticipating multiple attempts to save the same file. However, since we now save files only once in our storage, this feature is no longer utilized. This enhancement is detailed in issue #2879.
* Added epub query in our manifest to properly open epub files in external application.
  • Loading branch information
MohitMaliDeveloper authored and kelson42 committed Mar 11, 2024
1 parent b678e69 commit b2f3b51
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ class FileUtilsInstrumentationTest {
DummyUrlData(
"https://kiwix.org/contributors/images/wikipedia",
null
),
DummyUrlData(
"https://kiwix.org/contributors/images/wikipedia:hello.epub",
"wikipediahello.epub"
),
DummyUrlData(
"https://kiwix.org/contributors/Y Gododin: A Poem of the Battle: of Cattraeth.9842.epub",
"Y Gododin A Poem of the Battle of Cattraeth.9842.epub"
)
)
dummyUrlArray.forEach {
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
</intent>
<!-- To open PDF files in external application -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="application/pdf" />
</intent>
<!-- To open EPUB files in external application -->
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="application/pdf" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,15 @@ object FileUtils {
* We are placing a condition here for if the file name does not have a .bin extension,
then it returns the original file name.
* Remove colon if any contains in the fileName since most of the fileSystem
will not allow to create file which contains colon in it.
see https://github.com/kiwix/kiwix-android/issues/3737
*/
fun getDecodedFileName(url: String?): String? {
var fileName: String? = null
val decodedFileName = URLUtil.guessFileName(url, null, null)
if (!decodedFileName.endsWith(".bin")) {
fileName = decodedFileName
fileName = decodedFileName.replace(":", "")
}
return fileName
}
Expand Down Expand Up @@ -444,17 +447,8 @@ object FileUtils {
)
if (!root.isFileExist()) root.mkdir()
}
if (File(root, fileName).isFileExist()) return File(root, fileName)
val fileToSave = sequence {
yield(File(root, fileName))
yieldAll(
generateSequence(1) { it + 1 }.map {
File(
root, fileName.replace(".", "_$it.")
)
}
)
}.first { !it.isFileExist() }
val fileToSave = File(root, fileName)
if (fileToSave.isFileExist()) return fileToSave
val source = if (url == null) Uri.parse(src) else Uri.parse(url)
return try {
zimReaderContainer.load("$source", emptyMap()).data.use { inputStream ->
Expand Down

0 comments on commit b2f3b51

Please sign in to comment.