Skip to content

Commit 2ee50ad

Browse files
committed
fix: file sanitization
1 parent 943473f commit 2ee50ad

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

common/src/main/kotlin/com/lambda/util/StringUtils.kt

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
package com.lambda.util
22

33
object StringUtils {
4+
/**
5+
* Returns a sanitized file path for both Unix and Linux systems
6+
*/
47
fun String.sanitizeForFilename() =
5-
replace("\\", "_")
6-
.replace("/", "_")
7-
.replace(":", "_")
8-
.replace("*", "_")
9-
.replace("?", "_")
10-
.replace("\"", "_")
11-
.replace("<", "_")
12-
.replace(">", "_")
13-
.replace("|", "_")
8+
replace(Regex("[\\\\/:*?\"<>|]"), "_")
149
.trim()
1510
.take(255) // truncate to 255 characters for Windows compatibility
1611

17-
// ToDo: Fix this. Does not work for some reason
18-
// fun String.sanitizeForFilename(): String {
19-
// val invalidChars = Regex.fromLiteral("[\\\\/*?|<>:\"\\[\\]\\(\\)\\s]")
20-
// val safeChars = Regex.fromLiteral("[^\\p{L}\\p{N}_\\-~]")
21-
// return replace(invalidChars, "_")
22-
// .replace(safeChars, "")
23-
// .trim()
24-
// .take(255) // truncate to 255 characters for Windows compatibility
25-
// }
26-
2712

13+
/**
14+
* Capitalizes the first character of a string using its Unicode mapping
15+
*/
2816
fun String.capitalize() = replaceFirstChar { it.titlecase() }
2917

3018
/**
@@ -84,4 +72,4 @@ object StringUtils {
8472

8573
return cost[len0 - 1]
8674
}
87-
}
75+
}

0 commit comments

Comments
 (0)