Skip to content

Commit

Permalink
Closes mozilla-mobile#9073 remove unwanted dots in the file name
Browse files Browse the repository at this point in the history
  • Loading branch information
Amejia481 committed Nov 30, 2020
1 parent 9ec6a29 commit b9d2f9f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b9d2f9f

Please sign in to comment.