Skip to content

Commit abb0a8a

Browse files
committed
move validations to the link helper
Signed-off-by: alperozturk <[email protected]>
1 parent 9a5cad8 commit abb0a8a

File tree

2 files changed

+57
-54
lines changed

2 files changed

+57
-54
lines changed

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

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import android.net.Uri
1515
import android.widget.ImageView
1616
import androidx.annotation.DrawableRes
1717
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory
18-
import androidx.core.net.toUri
1918
import com.bumptech.glide.Glide
2019
import com.bumptech.glide.RequestBuilder
2120
import com.bumptech.glide.load.engine.DiskCacheStrategy
@@ -26,6 +25,8 @@ import com.bumptech.glide.request.target.AppWidgetTarget
2625
import com.bumptech.glide.request.target.BitmapImageViewTarget
2726
import com.bumptech.glide.request.target.Target
2827
import com.nextcloud.android.lib.resources.dashboard.DashboardWidgetItem
28+
import com.nextcloud.utils.LinkHelper.validateAndGetURI
29+
import com.nextcloud.utils.LinkHelper.validateAndGetURL
2930
import com.owncloud.android.lib.common.utils.Log_OC
3031
import com.owncloud.android.utils.DisplayUtils.SVG_SIZE
3132
import com.owncloud.android.utils.svg.SvgSoftwareLayerSetter
@@ -40,60 +41,7 @@ import com.owncloud.android.utils.svg.SvgSoftwareLayerSetter
4041
object GlideHelper {
4142
private const val TAG = "GlideHelper"
4243

43-
// region Validation
44-
/**
45-
* Validates if a string can be converted to a valid URI
46-
*/
47-
@Suppress("ComplexCondition", "TooGenericExceptionCaught")
48-
private fun validateAndGetURI(uriString: String?): Uri? {
49-
if (uriString.isNullOrBlank()) {
50-
Log_OC.w(TAG, "Given uriString is null or blank")
51-
return null
52-
}
53-
54-
return try {
55-
val uri = uriString.toUri()
56-
if (uri.scheme != null && (
57-
uri.scheme == "http" || uri.scheme == "https" || uri.scheme == "file" ||
58-
uri.scheme == "content"
59-
)
60-
) {
61-
uri
62-
} else {
63-
null
64-
}
65-
} catch (e: Exception) {
66-
Log_OC.w(TAG, "Invalid URI string: $uriString -- $e")
67-
null
68-
}
69-
}
70-
71-
/**
72-
* Validates if a URL string is valid
73-
*/
74-
@Suppress("TooGenericExceptionCaught")
75-
private fun validateAndGetURL(url: String?): String? {
76-
if (url.isNullOrBlank()) {
77-
Log_OC.w(TAG, "Given url is null or blank")
78-
return null
79-
}
80-
81-
return try {
82-
val uri = url.toUri()
83-
if (uri.scheme != null && (uri.scheme == "http" || uri.scheme == "https")) {
84-
url
85-
} else {
86-
null
87-
}
88-
} catch (e: Exception) {
89-
Log_OC.w(TAG, "Invalid URL: $url -- $e")
90-
null
91-
}
92-
}
93-
// endregion
94-
9544
// region SVG
96-
9745
/**
9846
* Creates a Glide request builder specifically for loading SVG images from a [Uri].
9947
*

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ package com.nextcloud.utils
1010
import android.content.ActivityNotFoundException
1111
import android.content.Context
1212
import android.content.Intent
13+
import android.net.Uri
1314
import androidx.core.net.toUri
1415
import com.nextcloud.client.account.User
16+
import com.owncloud.android.lib.common.utils.Log_OC
1517
import com.owncloud.android.ui.activity.FileDisplayActivity
1618
import java.util.Optional
1719
import kotlin.jvm.optionals.getOrNull
1820

1921
object LinkHelper {
2022
const val APP_NEXTCLOUD_NOTES = "it.niedermann.owncloud.notes"
2123
const val APP_NEXTCLOUD_TALK = "com.nextcloud.talk2"
24+
private const val TAG = "LinkHelper"
2225

2326
/**
2427
* Open specified app and, if not installed redirect to corresponding download.
@@ -70,4 +73,56 @@ object LinkHelper {
7073
context.startActivity(intent)
7174
}
7275
}
76+
77+
// region Validation
78+
/**
79+
* Validates if a string can be converted to a valid URI
80+
*/
81+
@Suppress("ComplexCondition", "TooGenericExceptionCaught")
82+
fun validateAndGetURI(uriString: String?): Uri? {
83+
if (uriString.isNullOrBlank()) {
84+
Log_OC.w(TAG, "Given uriString is null or blank")
85+
return null
86+
}
87+
88+
return try {
89+
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
98+
}
99+
} catch (e: Exception) {
100+
Log_OC.e(TAG, "Invalid URI string: $uriString -- $e")
101+
null
102+
}
103+
}
104+
105+
/**
106+
* Validates if a URL string is valid
107+
*/
108+
@Suppress("TooGenericExceptionCaught")
109+
fun validateAndGetURL(url: String?): String? {
110+
if (url.isNullOrBlank()) {
111+
Log_OC.w(TAG, "Given url is null or blank")
112+
return null
113+
}
114+
115+
return try {
116+
val uri = url.toUri()
117+
if (uri.scheme != null && (uri.scheme == "http" || uri.scheme == "https")) {
118+
url
119+
} else {
120+
null
121+
}
122+
} catch (e: Exception) {
123+
Log_OC.e(TAG, "Invalid URL: $url -- $e")
124+
null
125+
}
126+
}
127+
// endregion
73128
}

0 commit comments

Comments
 (0)