Skip to content

Commit 780b3e6

Browse files
committed
simplify LinkHelper.kt
Signed-off-by: alperozturk <[email protected]>
1 parent abb0a8a commit 780b3e6

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

app/src/main/java/com/nextcloud/utils/LinkHelper.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,15 @@ object LinkHelper {
7575
}
7676

7777
// region Validation
78+
private const val HTTP = "http"
79+
private const val HTTPS = "https"
80+
private const val FILE = "file"
81+
private const val CONTENT = "content"
82+
7883
/**
7984
* Validates if a string can be converted to a valid URI
8085
*/
81-
@Suppress("ComplexCondition", "TooGenericExceptionCaught")
86+
@Suppress("TooGenericExceptionCaught", "ReturnCount")
8287
fun validateAndGetURI(uriString: String?): Uri? {
8388
if (uriString.isNullOrBlank()) {
8489
Log_OC.w(TAG, "Given uriString is null or blank")
@@ -87,15 +92,12 @@ object LinkHelper {
8792

8893
return try {
8994
val uri = uriString.toUri()
90-
if (uri.scheme != null && (
91-
uri.scheme == "http" || uri.scheme == "https" || uri.scheme == "file" ||
92-
uri.scheme == "content"
93-
)
94-
) {
95-
uri
96-
} else {
97-
null
95+
if (uri.scheme == null) {
96+
return null
9897
}
98+
99+
val validSchemes = listOf(HTTP, HTTPS, FILE, CONTENT)
100+
if (uri.scheme in validSchemes) uri else null
99101
} catch (e: Exception) {
100102
Log_OC.e(TAG, "Invalid URI string: $uriString -- $e")
101103
null
@@ -105,7 +107,7 @@ object LinkHelper {
105107
/**
106108
* Validates if a URL string is valid
107109
*/
108-
@Suppress("TooGenericExceptionCaught")
110+
@Suppress("TooGenericExceptionCaught", "ReturnCount")
109111
fun validateAndGetURL(url: String?): String? {
110112
if (url.isNullOrBlank()) {
111113
Log_OC.w(TAG, "Given url is null or blank")
@@ -114,11 +116,11 @@ object LinkHelper {
114116

115117
return try {
116118
val uri = url.toUri()
117-
if (uri.scheme != null && (uri.scheme == "http" || uri.scheme == "https")) {
118-
url
119-
} else {
120-
null
119+
if (uri.scheme == null) {
120+
return null
121121
}
122+
val validSchemes = listOf(HTTP, HTTPS)
123+
if (uri.scheme in validSchemes) url else null
122124
} catch (e: Exception) {
123125
Log_OC.e(TAG, "Invalid URL: $url -- $e")
124126
null

0 commit comments

Comments
 (0)