Consolidate S3 requests into one interface.

This commit is contained in:
Cody Henthorne
2022-05-11 19:21:55 -04:00
committed by Alex Hart
parent bb963f9210
commit ecc358ef40
10 changed files with 109 additions and 177 deletions

View File

@@ -1,42 +1,22 @@
package org.thoughtcrime.securesms.emoji
import okhttp3.Request
import okhttp3.Response
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
import org.thoughtcrime.securesms.s3.S3
import java.io.IOException
private const val VERSION_URL = "https://updates.signal.org/dynamic/android/emoji/version_v3.txt"
private const val BASE_STATIC_BUCKET_URL = "https://updates.signal.org/static/android/emoji"
private const val BASE_STATIC_BUCKET_URI = "${S3.STATIC_PATH}/android/emoji"
/**
* Responsible for communicating with S3 to download Emoji related objects.
*/
object EmojiRemote {
private const val TAG = "EmojiRemote"
private val okHttpClient = ApplicationDependencies.getOkHttpClient()
private const val VERSION_URI = "${S3.DYNAMIC_PATH}/android/emoji/version_v3.txt"
@JvmStatic
@Throws(IOException::class)
fun getVersion(): Int {
val request = Request.Builder()
.get()
.url(VERSION_URL)
.build()
try {
okHttpClient.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
throw IOException()
}
return response.body()?.bytes()?.let { String(it).trim().toIntOrNull() } ?: throw IOException()
}
} catch (e: IOException) {
throw e
}
return S3.getLong(VERSION_URI).toInt()
}
/**
@@ -44,23 +24,7 @@ object EmojiRemote {
*/
@JvmStatic
fun getMd5(emojiRequest: EmojiRequest): ByteArray? {
val request = Request.Builder()
.head()
.url(emojiRequest.url)
.build()
try {
okHttpClient.newCall(request).execute().use { response ->
if (!response.isSuccessful) {
throw IOException()
}
return response.header("ETag")?.toByteArray()
}
} catch (e: IOException) {
Log.w(TAG, "Could not retrieve md5", e)
return null
}
return S3.getObjectMD5(emojiRequest.uri)
}
/**
@@ -68,21 +32,16 @@ object EmojiRemote {
*/
@JvmStatic
fun getObject(emojiRequest: EmojiRequest): Response {
val request = Request.Builder()
.get()
.url(emojiRequest.url)
.build()
return okHttpClient.newCall(request).execute()
return S3.getObject(emojiRequest.uri)
}
}
interface EmojiRequest {
val url: String
val uri: String
}
class EmojiJsonRequest(version: Int) : EmojiRequest {
override val url: String = "$BASE_STATIC_BUCKET_URL/$version/emoji_data.json"
override val uri: String = "$BASE_STATIC_BUCKET_URI/$version/emoji_data.json"
}
class EmojiImageRequest(
@@ -91,7 +50,7 @@ class EmojiImageRequest(
name: String,
format: String
) : EmojiRequest {
override val url: String = "$BASE_STATIC_BUCKET_URL/$version/$density/$name.$format"
override val uri: String = "$BASE_STATIC_BUCKET_URI/$version/$density/$name.$format"
}
class EmojiFileRequest(
@@ -99,5 +58,5 @@ class EmojiFileRequest(
density: String,
name: String,
) : EmojiRequest {
override val url: String = "$BASE_STATIC_BUCKET_URL/$version/$density/$name"
override val uri: String = "$BASE_STATIC_BUCKET_URI/$version/$density/$name"
}