From 9d6a52375c8788eeeb7e79f781aade56ab306481 Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Mon, 30 Nov 2020 16:55:27 -0500 Subject: [PATCH] Closes #9073 remove unwanted dots in the file name --- .../feature/downloads/ext/DownloadState.kt | 12 +++++++----- .../mozilla/components/support/ktx/kotlin/String.kt | 6 +++++- .../components/support/ktx/kotlin/StringTest.kt | 6 +++++- docs/changelog.md | 1 + 4 files changed, 18 insertions(+), 7 deletions(-) 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..523ed47e6d0 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 = if (file.extension.isBlank()) file.extension else ".${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..c7944b8600a 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()) @@ -180,5 +180,9 @@ class StringTest { file = "/root/directory/file.txt" assertEquals(fileName, file.sanitizeFileName()) + + assertEquals("file", "file".sanitizeFileName()) + + assertEquals("file", "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