Skip to content

Commit

Permalink
chore: apply suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrie25 committed Oct 26, 2024
1 parent b04b7c6 commit 159760b
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.tencent.mmkv.MMKV
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlin.time.Duration.Companion.days
import kotlin.time.Duration.Companion.hours

object Prefs {
val kv = MMKV.defaultMMKV()
Expand Down Expand Up @@ -89,11 +89,10 @@ object Prefs {
val lastDeleted = get(
key = LAST_DELETED,
// Force delete everyone's saved images for the first time
// Same time as Cloudflare cache invalidation
defaultValue = System.currentTimeMillis() - 7.days.inWholeMilliseconds
defaultValue = System.currentTimeMillis() - 24.hours.inWholeMilliseconds
)
val currentTime = System.currentTimeMillis()
if (currentTime - lastDeleted > 7.days.inWholeMilliseconds) {
if (currentTime - lastDeleted > 24.hours.inWholeMilliseconds) {
remove(SAVED_IMAGES)
remove(SAVED_ARTWORK)
set(LAST_DELETED, currentTime)
Expand Down
3 changes: 2 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 @@ -18,6 +18,7 @@ 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 @@ -94,7 +95,7 @@ object AppModule {
@Provides
fun providesKizzyRepository(
apiService: ApiService,
imgurApiService: ApiService.ImgurApiService
imgurApiService: ImgurApiService
): KizzyRepository {
return KizzyRepositoryImpl(apiService, imgurApiService)
}
Expand Down
8 changes: 0 additions & 8 deletions data/src/main/java/com/my/kizzy/data/remote/ApiResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,4 @@ import kotlinx.serialization.Serializable
data class ApiResponse(
@SerialName("id")
val id: String
)

@Serializable
data class ExternalAsset(
@SerialName("external_asset_path")
val externalAssetPath: String,
@SerialName("url")
val url: String
)
65 changes: 4 additions & 61 deletions data/src/main/java/com/my/kizzy/data/remote/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ import io.ktor.http.contentType
import java.io.File
import javax.inject.Inject

open class ApiService @Inject constructor(
class ApiService @Inject constructor(
private val client: HttpClient,
@Base private val baseUrl: String,
@Discord private val discordBaseUrl: String,
@Github private val githubBaseUrl: String,
) {
open suspend fun getImage(url: String, token: String) = runCatching {
suspend fun getImage(url: String) = runCatching {
client.get {
url("$baseUrl/image")
parameter("url", url)
}
}.getOrNull()?.toImageAsset()

open suspend fun uploadImage(file: File, token: String) = runCatching {
suspend fun uploadImage(file: File) = runCatching {
client.post {
url("$baseUrl/upload")
setBody(MultiPartFormDataContent(
Expand All @@ -59,7 +59,7 @@ open class ApiService @Inject constructor(
}
}.getOrNull()?.toImageAsset()

open suspend fun HttpResponse.toImageAsset(): String? {
suspend fun HttpResponse.toImageAsset(): String? {
return try {
if (this.status == HttpStatusCode.OK)
this.body<ApiResponse>().id
Expand All @@ -70,63 +70,6 @@ open class ApiService @Inject constructor(
}
}

class ImgurApiService @Inject constructor(
private val client: HttpClient,
@Discord private val discordBaseUrl: String,
@Imgur private val imgurBaseUrl: String,
) : ApiService(client, "", discordBaseUrl, "") {
override 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)))
}
}.getOrNull()?.toExternalImage()

override suspend fun uploadImage(file: File, token: String) = 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")
}
)
)
}
}.getOrNull()?.toImageAsset()?.let { this.getImage(it, token) }

override suspend fun HttpResponse.toImageAsset(): String? {
return try {
if (this.status == HttpStatusCode.OK)
this.body<ImgurResponse>().data.link
else
null
} catch (e: Exception) {
null
}
}

suspend fun HttpResponse.toExternalImage(): String? {
return try {
if (this.status == HttpStatusCode.OK)
"mp:" + this.body<Array<ExternalAsset>>().first().externalAssetPath
else
null
} catch (e: Exception) {
null
}
}
}

suspend fun getGames() = runCatching {
client.get {
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
)
88 changes: 88 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,88 @@
/*
*
* ******************************************************************
* * * 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)))
}
}.getOrNull()?.toExternalImage()

suspend fun uploadImage(file: File, token: String) = 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")
}
)
)
}
}.getOrNull()?.toImageAsset()?.let { this.getImage(it, token) }

suspend fun HttpResponse.toImageAsset(): String? {
return try {
if (this.status == HttpStatusCode.OK)
this.body<ImgurResponse>().data.link
else
null
} catch (e: Exception) {
null
}
}

suspend fun HttpResponse.toExternalImage(): String? {
return try {
if (this.status == HttpStatusCode.OK)
"mp:" + this.body<Array<ExternalAsset>>().first().externalAssetPath
else
null
} catch (e: Exception) {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package com.my.kizzy.data.repository

import com.my.kizzy.data.remote.ApiService
import com.my.kizzy.data.remote.GamesResponse
import com.my.kizzy.data.remote.ImgurApiService
import com.my.kizzy.data.remote.toGame
import com.my.kizzy.domain.model.Contributor
import com.my.kizzy.domain.model.Game
Expand All @@ -29,22 +30,22 @@ import javax.inject.Inject

class KizzyRepositoryImpl @Inject constructor(
private val api: ApiService,
private val imgurApi: ApiService.ImgurApiService
private val imgurApi: ImgurApiService
): KizzyRepository {

override suspend fun getImage(url: String, token: String): String? {
override suspend fun getImage(url: String): String? {
return if (Prefs[Prefs.USE_IMGUR, false]) {
imgurApi.getImage(url, token)
imgurApi.getImage(url, Prefs[Prefs.TOKEN])
} else {
api.getImage(url, token)
api.getImage(url)
}
}

override suspend fun uploadImage(file: File, token: String): String? {
override suspend fun uploadImage(file: File): String? {
return if (Prefs[Prefs.USE_IMGUR, false]) {
imgurApi.uploadImage(file, token)
imgurApi.uploadImage(file, Prefs[Prefs.TOKEN])
} else {
api.uploadImage(file, token)
api.uploadImage(file)
}
}

Expand Down
2 changes: 1 addition & 1 deletion data/src/main/java/com/my/kizzy/data/rpc/KizzyRPC.kt
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ class KizzyRPC(
),
afk = true,
since = startTimestamps,
status = this.status
status = Constants.DND
)
)
}
Expand Down
6 changes: 3 additions & 3 deletions data/src/main/java/com/my/kizzy/data/rpc/RpcImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ sealed class RpcImage {

class ExternalImage(val image: String) : RpcImage() {
override suspend fun resolveImage(repository: KizzyRepository): String? {
return repository.getImage(image, Prefs[Prefs.TOKEN])
return repository.getImage(image)
}
}

Expand All @@ -56,7 +56,7 @@ sealed class RpcImage {
): String? {
val applicationInfo = context.getAppInfo(packageName)
val bitmap = applicationInfo.toBitmap(context)
val response = repository.uploadImage(bitmap.toFile(context, "image"), Prefs[Prefs.TOKEN])
val response = repository.uploadImage(bitmap.toFile(context, "image"))
response?.let {
savedImages[packageName] = it
Prefs[Prefs.SAVED_IMAGES] = Json.encodeToString(savedImages)
Expand All @@ -78,7 +78,7 @@ sealed class RpcImage {
return if (savedImages.containsKey(schema))
savedImages[schema]
else {
val result = repository.uploadImage(bitmap.toFile(this.context, "art"), Prefs[Prefs.TOKEN])
val result = repository.uploadImage(bitmap.toFile(this.context, "art"))
result?.let {
savedImages[schema] = it
Prefs[Prefs.SAVED_ARTWORK] = Json.encodeToString(savedImages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import com.my.kizzy.domain.model.user.User
import java.io.File

interface KizzyRepository {
suspend fun getImage(url: String, token: String): String?
suspend fun uploadImage(file: File, token: String): String?
suspend fun getImage(url: String): String?
suspend fun uploadImage(file: File): String?
suspend fun getGames(): List<Game>
suspend fun getUser(userid: String): User
suspend fun getContributors(): List<Contributor>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import javax.inject.Inject
class UploadGalleryImageUseCase @Inject constructor(
private val kizzyRepository: KizzyRepository
) {
suspend operator fun invoke(file: File, token: String): String? {
suspend operator fun invoke(file: File): String? {
return try {
file.deleteOnExit()
kizzyRepository.uploadImage(file, token)
kizzyRepository.uploadImage(file)
} catch (ex: Exception) {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class CustomScreenViewModel @Inject constructor(
}

private suspend fun uploadImage(file: File, result: (String) -> Unit) {
uploadGalleryImageUseCase(file, Prefs[Prefs.TOKEN])?.let {
uploadGalleryImageUseCase(file)?.let {
withContext(Dispatchers.Main) {
result(it.drop(3))
}
Expand Down
Loading

0 comments on commit 159760b

Please sign in to comment.