diff --git a/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/ext/DownloadState.kt b/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/ext/DownloadState.kt index 53cff70a32b..f65796d04cb 100644 --- a/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/ext/DownloadState.kt +++ b/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/ext/DownloadState.kt @@ -10,6 +10,7 @@ import mozilla.components.concept.fetch.Headers import mozilla.components.concept.fetch.Headers.Names.CONTENT_DISPOSITION import mozilla.components.concept.fetch.Headers.Names.CONTENT_LENGTH import mozilla.components.concept.fetch.Headers.Names.CONTENT_TYPE +import mozilla.components.support.ktx.kotlin.sanitizeFileName import mozilla.components.support.utils.DownloadUtils import java.io.InputStream import java.net.URLConnection @@ -35,12 +36,13 @@ internal fun DownloadState.withResponse(headers: Headers, stream: InputStream?): contentType = headers[CONTENT_TYPE] } + val newFileName = if (fileName.isNullOrBlank()) { + DownloadUtils.guessFileName(contentDisposition, destinationDirectory, url, contentType) + } else { + fileName + } return copy( - fileName = if (fileName.isNullOrBlank()) { - DownloadUtils.guessFileName(contentDisposition, destinationDirectory, url, contentType) - } else { - fileName - }, + fileName = newFileName?.sanitizeFileName(), contentType = contentType, contentLength = contentLength ?: headers[CONTENT_LENGTH]?.toLongOrNull() ) diff --git a/components/support/ktx/src/main/java/mozilla/components/support/ktx/kotlin/String.kt b/components/support/ktx/src/main/java/mozilla/components/support/ktx/kotlin/String.kt index ca9e36fef72..e14af6dd4db 100644 --- a/components/support/ktx/src/main/java/mozilla/components/support/ktx/kotlin/String.kt +++ b/components/support/ktx/src/main/java/mozilla/components/support/ktx/kotlin/String.kt @@ -146,7 +146,11 @@ fun String.sanitizeURL(): String { * For example for an input of "/../../../../../../directory/file.txt" you will get "file.txt" */ fun String.sanitizeFileName(): String { - return this.substringAfterLast(File.separatorChar) + val file = File(this.substringAfterLast(File.separatorChar)) + // Remove unwanted dots in the file name. + val name = file.nameWithoutExtension.replace(".", "") + val ext = file.extension + return "$name.$ext" } /** diff --git a/components/support/ktx/src/test/java/mozilla/components/support/ktx/kotlin/StringTest.kt b/components/support/ktx/src/test/java/mozilla/components/support/ktx/kotlin/StringTest.kt index 198d447d037..8d383d1f464 100644 --- a/components/support/ktx/src/test/java/mozilla/components/support/ktx/kotlin/StringTest.kt +++ b/components/support/ktx/src/test/java/mozilla/components/support/ktx/kotlin/StringTest.kt @@ -172,7 +172,7 @@ class StringTest { @Test fun sanitizeFileName() { - var file = "/../../../../../../../../../../directory/file.txt" + var file = "/../../../../../../../../../../directory/file.......txt" val fileName = "file.txt" assertEquals(fileName, file.sanitizeFileName()) diff --git a/docs/changelog.md b/docs/changelog.md index f7fe4c59cec..bd108f1741c 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -17,6 +17,7 @@ permalink: /changelog/ * **feature-downloads** * 🚒 Bug fixed [issue #9033](https://github.com/mozilla-mobile/android-components/issues/9033) - Fix resuming downloads in slow networks more details see the [Fenix issue](https://github.com/mozilla-mobile/fenix/issues/9354#issuecomment-731267368). + * 🚒 Bug fixed [issue #9073](https://github.com/mozilla-mobile/android-components/issues/9073) - Fix crash downloading a file with multiple dots on it, for more details see the [Fenix issue](https://github.com/mozilla-mobile/fenix/issues/16443). * **feature-app-links** * Added handling of PackageItemInfo.packageName NullPointerException on some Xiaomi and TCL devices