Skip to content

Commit

Permalink
feat(images): option to use external proxy (#313)
Browse files Browse the repository at this point in the history
* feat: use external proxy for images

* feat(images): use imgur as alternate provider

* chore: update translation

* chore: apply suggestion

* fix(imgur): add missing declaration to git

* refactor(imgur): merge response classes

* chore: apply suggestions

* chore: apply suggestions
  • Loading branch information
kyrie25 authored Oct 26, 2024
1 parent e8e227b commit 9d17a1b
Show file tree
Hide file tree
Showing 37 changed files with 258 additions and 16 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ object Prefs {
const val RPC_BUTTONS_DATA = "saved_rpc_buttons_data"
const val RPC_USE_LOW_RES_ICON = "use_low_res_app_icons"
const val CONFIGS_DIRECTORY = "configs_directory"
const val USE_IMGUR = "use_imgur"
// Saved Image Asset ids
const val SAVED_IMAGES = "saved_images"
// Saved ArtWork
Expand Down
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-fa/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-fil/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-hr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-in/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>


</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-kr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,6 @@
<string name="config_name_empty">설정 이름은 비워둘 수 없습니다</string>
<string name="restart">재시작</string>
<string name="custom_rpc_running">사용자 정의 RPC 실행 중</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-mm/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>


</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,6 @@
<string name="config_name_empty">Nazwa konfiguracji nie może być pusta</string>
<string name="restart">Restartuj</string>
<string name="custom_rpc_running">Własne RPC jest uruchomiony</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-pt/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,7 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>

</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@
<string name="config_name_empty">Название конфигурации не может быть пустым</string>
<string name="restart">Перезапустить</string>
<string name="custom_rpc_running">Свой RPC запущен</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-th/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-tr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@
<string name="config_name_empty">Tên cấu hình không được để trống</string>
<string name="restart">Khởi chạy lại</string>
<string name="custom_rpc_running">RPC tuỳ chỉnh đang chạy</string>
<string name="use_imgur">Lưu trữ hình ảnh trên Imgur</string>
<string name="use_imgur_desc">Sử dụng Imgur làm nơi lưu trữ hình ảnh.</string>
</resources>
2 changes: 2 additions & 0 deletions common/resources/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
6 changes: 4 additions & 2 deletions common/resources/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
<string name="request_for_storage_access">Storage permission required for config features to work. Please grant the permission.</string>
<string name="grant_permission">Grant Permission</string>
<string name="use_low_res_icon">Use Low Resolution Images</string>
<string name="use_low_res_icon_desc">Use a lower resolution app icon for displaying on RPC (turn this on if the RPC is crashing). This setting will also be applied to Apps .</string>
<string name="use_low_res_icon_desc">Use a lower resolution app icon for displaying on RPC (turn this on if the RPC is crashing). This setting will also be applied to Apps.</string>
<string name="delete_saved_icon_urls">Delete Saved Icon Links</string>
<string name="delete_saved_icon_urls_desc">Delete app icon URLs saved in local files (Only use this in rare cases as it will send all the images again to discord servers which will consume more data)</string>
<string name="delete_saved_icon_urls_desc">Delete app icon URLs saved in local files (Only use this as needed as it will send images to Discord servers again, which will consume more data)</string>
<string name="app_crashed">App Crashed</string>
<string name="share_crash_logs_desc">Please send the Logs in Discord channel with the following stacktrace :</string>
<string name="share_crash_logs">SHARE LOGS</string>
Expand Down Expand Up @@ -188,4 +188,6 @@
<string name="config_name_empty">Config name cannot be empty</string>
<string name="restart">Restart</string>
<string name="custom_rpc_running">Custom RPC is running</string>
<string name="use_imgur">Imgur as Image Proxy</string>
<string name="use_imgur_desc">Use Imgur as an alternate image proxy.</string>
</resources>
1 change: 1 addition & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ android {
buildConfigFieldFromGradleProperty("BASE_URL","BASE_URL")
buildConfigFieldFromGradleProperty("DISCORD_API_BASE_URL","DISCORD_API_URL")
buildConfigFieldFromGradleProperty("GITHUB_API_BASE_URL","GITHUB_API_URL")
buildConfigFieldFromGradleProperty("IMGUR_API_BASE_URL", "IMGUR_API_URL")
}
}

Expand Down
10 changes: 9 additions & 1 deletion data/src/main/java/com/my/kizzy/data/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import com.my.kizzy.data.remote.ApiService
import com.my.kizzy.data.remote.Base
import com.my.kizzy.data.remote.Discord
import com.my.kizzy.data.remote.Github
import com.my.kizzy.data.remote.Imgur
import com.my.kizzy.data.remote.ImgurApiService
import com.my.kizzy.data.repository.KizzyRepositoryImpl
import com.my.kizzy.domain.repository.KizzyRepository
import dagger.Module
Expand Down Expand Up @@ -53,6 +55,11 @@ object AppModule {
@Github
fun provideGithubBaseUrl() = BuildConfig.GITHUB_API_BASE_URL

@Provides
@Singleton
@Imgur
fun provideImgurBaseUrl() = BuildConfig.IMGUR_API_BASE_URL

@Provides
fun provideJson() = Json {
ignoreUnknownKeys = true
Expand Down Expand Up @@ -88,7 +95,8 @@ object AppModule {
@Provides
fun providesKizzyRepository(
apiService: ApiService,
imgurApiService: ImgurApiService
): KizzyRepository {
return KizzyRepositoryImpl(apiService)
return KizzyRepositoryImpl(apiService, imgurApiService)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class GetCurrentPlayingMedia @Inject constructor(
context = context,
bitmap = bitmap,
packageName = mediaController.packageName,
title = title
// <Main artist>|<Album or Title>
title = "${metadata.let { metadataResolver.getAlbumArtists(it) }}|${metadata.let { metadataResolver.getAlbum(it) } ?: title}"
)
} else smallIcon = null
return CommonRpc(name = appName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@ class MetadataResolver @Inject constructor() {
MediaMetadata.METADATA_KEY_ALBUM
) else null
}

fun getAlbumArtists(metadata: MediaMetadata): String? {
return if (!metadata.getString(MediaMetadata.METADATA_KEY_ALBUM_ARTIST).isNullOrEmpty()) metadata.getString(
MediaMetadata.METADATA_KEY_ALBUM_ARTIST
) else getArtistOrAuthor(metadata)
}
}
5 changes: 5 additions & 0 deletions data/src/main/java/com/my/kizzy/data/remote/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package com.my.kizzy.data.remote

import com.my.kizzy.domain.model.samsung_rpc.GalaxyPresence
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.forms.MultiPartFormDataContent
import io.ktor.client.request.forms.formData
import io.ktor.client.request.get
Expand All @@ -21,9 +22,11 @@ import io.ktor.client.request.parameter
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.request.url
import io.ktor.client.statement.HttpResponse
import io.ktor.http.ContentType
import io.ktor.http.Headers
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.http.contentType
import java.io.File
import javax.inject.Inject
Expand All @@ -40,6 +43,7 @@ class ApiService @Inject constructor(
parameter("url", url)
}
}

suspend fun uploadImage(file: File) = runCatching {
client.post {
url("$baseUrl/upload")
Expand Down Expand Up @@ -84,6 +88,7 @@ class ApiService @Inject constructor(
setBody(galaxyPresence)
}
}

suspend fun checkForUpdate() = runCatching {
client.get {
url("$githubBaseUrl/repos/dead8309/Kizzy/releases/latest")
Expand Down
24 changes: 24 additions & 0 deletions data/src/main/java/com/my/kizzy/data/remote/ExternalAsset.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
*
* ******************************************************************
* * * Copyright (C) 2022
* * * ExternalAsset.kt is part of Kizzy
* * * and can not be copied and/or distributed without the express
* * * permission of yzziK(Vaibhav)
* * *****************************************************************
*
*
*/

package com.my.kizzy.data.remote

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class ExternalAsset(
@SerialName("external_asset_path")
val externalAssetPath: String,
@SerialName("url")
val url: String
)
66 changes: 66 additions & 0 deletions data/src/main/java/com/my/kizzy/data/remote/ImgurApiService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
*
* ******************************************************************
* * * Copyright (C) 2022
* * * ImgurApiService.kt is part of Kizzy
* * * and can not be copied and/or distributed without the express
* * * permission of yzziK(Vaibhav)
* * *****************************************************************
*
*
*/

package com.my.kizzy.data.remote

import com.my.kizzy.data.rpc.Constants.APPLICATION_ID
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.request.forms.MultiPartFormDataContent
import io.ktor.client.request.forms.formData
import io.ktor.client.request.headers
import io.ktor.client.request.post
import io.ktor.client.request.setBody
import io.ktor.client.request.url
import io.ktor.client.statement.HttpResponse
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.http.contentType
import java.io.File
import javax.inject.Inject

class ImgurApiService @Inject constructor(
private val client: HttpClient,
@Discord private val discordBaseUrl: String,
@Imgur private val imgurBaseUrl: String,
) {
suspend fun getImage(url: String, token: String) = runCatching {
client.post {
url("$discordBaseUrl/applications/$APPLICATION_ID/external-assets")
headers {
append(HttpHeaders.Authorization, token)
append(HttpHeaders.ContentType, "application/json")
}
setBody(mapOf("urls" to arrayOf(url)))
}
}

suspend fun uploadImage(file: File) = runCatching {
client.post {
url("$imgurBaseUrl/image")
headers {
// Imgur web client API key, unchanged for at least >5 years as of 2024
append(HttpHeaders.Authorization, "Client-ID 546c25a59c58ad7")
}
contentType(ContentType.MultiPart.FormData)
setBody(
MultiPartFormDataContent(
formData {
append("image", file.readBytes())
append("type", "raw")
}
)
)
}
}
}
Loading

0 comments on commit 9d17a1b

Please sign in to comment.