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 Dec 1, 2020
1 parent 9ec6a29 commit fb3fe48
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import mozilla.components.support.ktx.kotlin.isEmail
import mozilla.components.support.ktx.kotlin.isExtensionUrl
import mozilla.components.support.ktx.kotlin.isGeoLocation
import mozilla.components.support.ktx.kotlin.isPhone
import mozilla.components.support.ktx.kotlin.sanitizeFileName
import mozilla.components.support.ktx.kotlin.tryGetHostFromUrl
import mozilla.components.support.utils.DownloadUtils
import org.json.JSONObject
Expand Down Expand Up @@ -811,7 +812,7 @@ class GeckoEngineSession(
url = url,
contentLength = contentLength,
contentType = contentType,
fileName = fileName,
fileName = fileName.sanitizeFileName(),
response = response,
isPrivate = privateMode
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import mozilla.components.support.ktx.kotlin.isEmail
import mozilla.components.support.ktx.kotlin.isExtensionUrl
import mozilla.components.support.ktx.kotlin.isGeoLocation
import mozilla.components.support.ktx.kotlin.isPhone
import mozilla.components.support.ktx.kotlin.sanitizeFileName
import mozilla.components.support.ktx.kotlin.tryGetHostFromUrl
import mozilla.components.support.utils.DownloadUtils
import org.json.JSONObject
Expand Down Expand Up @@ -811,7 +812,7 @@ class GeckoEngineSession(
url = url,
contentLength = contentLength,
contentType = contentType,
fileName = fileName,
fileName = fileName.sanitizeFileName(),
response = response,
isPrivate = privateMode
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import mozilla.components.support.ktx.kotlin.isEmail
import mozilla.components.support.ktx.kotlin.isExtensionUrl
import mozilla.components.support.ktx.kotlin.isGeoLocation
import mozilla.components.support.ktx.kotlin.isPhone
import mozilla.components.support.ktx.kotlin.sanitizeFileName
import mozilla.components.support.ktx.kotlin.tryGetHostFromUrl
import mozilla.components.support.utils.DownloadUtils
import org.json.JSONObject
Expand Down Expand Up @@ -804,7 +805,7 @@ class GeckoEngineSession(
url = url,
contentLength = contentLength,
contentType = contentType,
fileName = fileName,
fileName = fileName.sanitizeFileName(),
response = response,
isPrivate = privateMode
)
Expand Down
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,13 @@ 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 subsequent dots in the file name.
return if (file.extension.trim().isNotEmpty()) {
file.name.replace("\\.\\.+".toRegex(), ".")
} else {
file.name.replace("\\.\\.?+".toRegex(), "")
}.removePrefix(".")
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,23 @@ class StringTest {

@Test
fun sanitizeFileName() {
var file = "/../../../../../../../../../../directory/file.txt"
var file = "/../../../../../../../../../../directory/file.......txt"
val fileName = "file.txt"

assertEquals(fileName, file.sanitizeFileName())

file = "/root/directory/file.txt"

assertEquals(fileName, file.sanitizeFileName())

assertEquals("file", "file".sanitizeFileName())

assertEquals("file", "file..".sanitizeFileName())

assertEquals("file", "file.".sanitizeFileName())

assertEquals("file", ".file".sanitizeFileName())

assertEquals("test.2020.12.01.txt", "test.2020.12.01.txt".sanitizeFileName())
}
}
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 fb3fe48

Please sign in to comment.