File tree Expand file tree Collapse file tree 6 files changed +47
-5
lines changed
main/java/com/shifthackz/aisdv1/core/common/extensions
test/java/com/shifthackz/aisdv1/core/common/extensions
data/src/main/java/com/shifthackz/aisdv1/data Expand file tree Collapse file tree 6 files changed +47
-5
lines changed Original file line number Diff line number Diff line change @@ -13,4 +13,5 @@ dependencies {
1313 implementation di. koinCore
1414 implementation reactive. rxjava
1515 implementation log. timber
16+ testImplementation test. junit
1617}
Original file line number Diff line number Diff line change 11package com.shifthackz.aisdv1.core.common.extensions
22
33private const val PROTOCOL_DELIMITER = " ://"
4+ private const val PROTOCOL_HOLDER = " [[_PROTOCOL_]]"
45
56fun String.withoutUrlProtocol (): String {
67 if (! this .contains(PROTOCOL_DELIMITER )) return this
78 val decomposed = this .split(PROTOCOL_DELIMITER )
89 if (decomposed.size < 2 ) return this
910 return decomposed.last()
1011}
12+
13+ fun String.fixUrlSlashes (): String = this
14+ .replace(PROTOCOL_DELIMITER , PROTOCOL_HOLDER )
15+ .replace(Regex (" /{2,}" ), " /" )
16+ .let { str -> if (str.last() == ' /' ) str.substring(0 , str.lastIndex) else str }
17+ .replace(PROTOCOL_HOLDER , PROTOCOL_DELIMITER )
Original file line number Diff line number Diff line change 1+ package com.shifthackz.aisdv1.core.common.extensions
2+
3+ import org.junit.Assert
4+ import org.junit.Test
5+
6+ class StringExtensionsTest {
7+
8+ @Test
9+ fun `given string with slash at and, expected no slash at end` () {
10+ val actual = " http://192.168.228.9:7860/" .fixUrlSlashes()
11+ val expected = " http://192.168.228.9:7860"
12+ Assert .assertEquals(expected, actual)
13+ }
14+
15+ @Test
16+ fun `given string with 3 slashes at and, expected no slashes at end` () {
17+ val actual = " http://192.168.228.9:7860///" .fixUrlSlashes()
18+ val expected = " http://192.168.228.9:7860"
19+ Assert .assertEquals(expected, actual)
20+ }
21+
22+ @Test
23+ fun `given string with even and odd duplicates and slash at end, expected no duplicate slashes and slash at end` () {
24+ val actual = " http://192.168.228.9:7860///path1////path2/" .fixUrlSlashes()
25+ val expected = " http://192.168.228.9:7860/path1/path2"
26+ Assert .assertEquals(expected, actual)
27+ }
28+ }
Original file line number Diff line number Diff line change 11package com.shifthackz.aisdv1.data.di
22
3+ import com.shifthackz.aisdv1.core.common.extensions.fixUrlSlashes
34import com.shifthackz.aisdv1.core.common.links.LinksProvider
45import com.shifthackz.aisdv1.data.gateway.ServerConnectivityGatewayImpl
56import com.shifthackz.aisdv1.data.provider.ServerUrlProvider
@@ -33,7 +34,10 @@ val remoteDataSourceModule = module {
3334 val links = get<LinksProvider >()
3435 val chain = if (prefs.useSdAiCloud) Single .fromCallable(links::cloudUrl)
3536 else Single .fromCallable(prefs::serverUrl)
36- chain.map { baseUrl -> " $baseUrl /$endpoint " }
37+
38+ chain
39+ .map(String ::fixUrlSlashes)
40+ .map { baseUrl -> " $baseUrl /$endpoint " }
3741 }
3842 }
3943
Original file line number Diff line number Diff line change 11package com.shifthackz.aisdv1.data.preference
22
33import android.content.SharedPreferences
4+ import com.shifthackz.aisdv1.core.common.extensions.fixUrlSlashes
45import com.shifthackz.aisdv1.domain.entity.Settings
56import com.shifthackz.aisdv1.domain.preference.PreferenceManager
67import io.reactivex.rxjava3.core.BackpressureStrategy
@@ -15,10 +16,10 @@ class PreferenceManagerImpl(
1516 BehaviorSubject .createDefault(Unit )
1617
1718 override var serverUrl: String
18- get() = if (useSdAiCloud) " "
19- else preferences.getString( KEY_SERVER_URL , " " ) ? : " "
19+ get() = ( if (useSdAiCloud) " " else preferences.getString( KEY_SERVER_URL , " " )
20+ ? : " " ).fixUrlSlashes()
2021 set(value) = preferences.edit()
21- .putString(KEY_SERVER_URL , if (useSdAiCloud) " " else value)
22+ .putString(KEY_SERVER_URL , ( if (useSdAiCloud) " " else value).fixUrlSlashes() )
2223 .apply ()
2324 .also { onPreferencesChanged() }
2425
Original file line number Diff line number Diff line change 11package com.shifthackz.aisdv1.data.remote
22
3+ import com.shifthackz.aisdv1.core.common.extensions.fixUrlSlashes
34import com.shifthackz.aisdv1.data.mappers.mapToAiGenResult
45import com.shifthackz.aisdv1.data.mappers.mapToRequest
56import com.shifthackz.aisdv1.data.provider.ServerUrlProvider
@@ -20,7 +21,7 @@ internal class StableDiffusionGenerationRemoteDataSource(
2021 override fun checkAvailability () = serverUrlProvider(" /" )
2122 .flatMapCompletable(api::healthCheck)
2223
23- override fun checkAvailability (url : String ) = api.healthCheck(url)
24+ override fun checkAvailability (url : String ) = api.healthCheck(url.fixUrlSlashes() )
2425
2526 override fun textToImage (payload : TextToImagePayload ) = serverUrlProvider(PATH_TXT_TO_IMG )
2627 .flatMap { url -> api.textToImage(url, payload.mapToRequest()) }
You can’t perform that action at this time.
0 commit comments