[tbb-commits] [Git][tpo/applications/firefox-android][firefox-android-115.2.1-13.5-1] Bug 1906024 - Format download file names better a=diannaS [BP131]

ma1 (@ma1) git at gitlab.torproject.org
Mon Sep 30 22:34:05 UTC 2024



ma1 pushed to branch firefox-android-115.2.1-13.5-1 at The Tor Project / Applications / firefox-android


Commits:
257aa5e5 by hackademix at 2024-09-30T21:41:21+02:00
Bug 1906024 - Format download file names better  a=diannaS [BP131]

- - - - -


1 changed file:

- android-components/components/support/ktx/src/main/java/mozilla/components/support/ktx/kotlin/String.kt


Changes:

=====================================
android-components/components/support/ktx/src/main/java/mozilla/components/support/ktx/kotlin/String.kt
=====================================
@@ -49,6 +49,8 @@ const val MAX_URI_LENGTH = 25000
 
 private const val FILE_PREFIX = "file://"
 private const val MAX_VALID_PORT = 65_535
+private const val SPACE = " "
+private const val UNDERSCORE = "_"
 
 /**
  * Shortens URLs to be more user friendly.
@@ -303,7 +305,27 @@ fun String.sanitizeFileName(): String {
         file.name.replace("\\.\\.+".toRegex(), ".")
     } else {
         file.name.replace(".", "")
-    }
+    }.replaceContinuousSpaces()
+        .replaceEscapedCharacters()
+        .trim()
+}
+
+
+/**
+ * Replaces control characters from ASCII 0 to ASCII 19 with '_' so the file name is valid
+ * and is correctly displayed.
+ */
+private fun String.replaceEscapedCharacters(): String {
+    val escapedCharactersRegex = "[\\x00-\\x13*\"?<>:|\\\\]".toRegex()
+    return replace(escapedCharactersRegex, UNDERSCORE)
+}
+
+/**
+ * Replaces continuous spaces with a single space.
+ */
+private fun String.replaceContinuousSpaces(): String {
+    val escapedCharactersRegex = "[\\p{Z}\\s]+".toRegex()
+    return replace(escapedCharactersRegex, SPACE)
 }
 
 /**



View it on GitLab: https://gitlab.torproject.org/tpo/applications/firefox-android/-/commit/257aa5e5be63fe9ec2a14f2f2ad5b3b0a929dcd1

-- 
View it on GitLab: https://gitlab.torproject.org/tpo/applications/firefox-android/-/commit/257aa5e5be63fe9ec2a14f2f2ad5b3b0a929dcd1
You're receiving this email because of your account on gitlab.torproject.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.torproject.org/pipermail/tbb-commits/attachments/20240930/449deb8b/attachment-0001.htm>


More information about the tbb-commits mailing list