Skip to content

Commit

Permalink
Adds iMeta tags to GIF urls
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Jan 5, 2025
1 parent f1d61da commit 306ce9a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ImageUploadTesting {
assertEquals("${server.baseUrl}/$initialHash", result.url?.removeSuffix(".png"))

val imageData: ByteArray =
ImageDownloader().waitAndGetImage(result.url!!, false)
ImageDownloader().waitAndGetImage(result.url!!, false)?.bytes
?: run {
fail("${server.name}: Should not be null")
return
Expand Down Expand Up @@ -148,7 +148,7 @@ class ImageUploadTesting {
Assert.assertTrue("${server.name}: Invalid result url", url.startsWith("http"))

val imageData: ByteArray =
ImageDownloader().waitAndGetImage(url, false)
ImageDownloader().waitAndGetImage(url, false)?.bytes
?: run {
fail("${server.name}: Should not be null")
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class FileHeader(
forceProxy: Boolean,
): Result<FileHeader> =
try {
val imageData: ByteArray? = ImageDownloader().waitAndGetImage(fileUrl, forceProxy)
val imageData: ImageDownloader.Blob? = ImageDownloader().waitAndGetImage(fileUrl, forceProxy)

if (imageData != null) {
prepare(imageData, mimeType, dimPrecomputed)
prepare(imageData.bytes, mimeType ?: imageData.contentType, dimPrecomputed)
} else {
Result.failure(UnableToDownload(fileUrl))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,17 @@ import java.net.HttpURLConnection
import java.net.URL

class ImageDownloader {
class Blob(
val bytes: ByteArray,
val contentType: String?,
)

suspend fun waitAndGetImage(
imageUrl: String,
forceProxy: Boolean,
): ByteArray? =
): Blob? =
withContext(Dispatchers.IO) {
var imageData: ByteArray? = null
var imageData: Blob? = null
var tentatives = 0

// Servers are usually not ready.. so tries to download it for 15 times/seconds.
Expand All @@ -59,7 +64,7 @@ class ImageDownloader {
private suspend fun tryGetTheImage(
imageUrl: String,
forceProxy: Boolean,
): ByteArray? =
): Blob? =
withContext(Dispatchers.IO) {
// TODO: Migrate to OkHttp
HttpURLConnection.setFollowRedirects(true)
Expand Down Expand Up @@ -88,7 +93,10 @@ class ImageDownloader {
}

return@withContext if (responseCode in 200..300) {
huc.inputStream.use { it.readBytes() }
Blob(
huc.inputStream.use { it.readBytes() },
huc.headerFields.get("Content-Type")?.firstOrNull(),
)
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,15 @@ class UploadOrchestrator {

updateState(0.6, UploadingState.Downloading)

val imageData: ByteArray? = ImageDownloader().waitAndGetImage(uploadResult.url, forceProxy(uploadResult.url))
val imageData: ImageDownloader.Blob? = ImageDownloader().waitAndGetImage(uploadResult.url, forceProxy(uploadResult.url))

if (imageData != null) {
updateState(0.8, UploadingState.Hashing)

val result =
FileHeader.prepare(
imageData,
uploadResult.type ?: localContentType,
imageData.bytes,
uploadResult.type ?: localContentType ?: imageData.contentType,
uploadResult.dimension,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
*/
package com.vitorpamplona.amethyst.ui.actions

import android.R.attr.mimeType
import android.content.Context
import android.util.Log
import android.webkit.MimeTypeMap
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
Expand All @@ -43,6 +45,7 @@ import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.LocationState
import com.vitorpamplona.amethyst.service.NostrSearchEventOrUserDataSource
import com.vitorpamplona.amethyst.service.uploads.FileHeader
import com.vitorpamplona.amethyst.service.uploads.MediaCompressor
import com.vitorpamplona.amethyst.service.uploads.MultiOrchestrator
import com.vitorpamplona.amethyst.service.uploads.UploadOrchestrator
Expand Down Expand Up @@ -92,6 +95,7 @@ import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.Locale
import java.util.UUID
import kotlin.math.round

Expand Down Expand Up @@ -1228,6 +1232,28 @@ open class NewPostViewModel : ViewModel() {
val lastWordStart = it.end - lastWord.length
val wordToInsert = item.url.url + " "

viewModelScope.launch(Dispatchers.IO) {
val fileExtension: String = MimeTypeMap.getFileExtensionFromUrl(item.url.url)
val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension.lowercase(Locale.getDefault()))

val forceProxy = accountViewModel?.account?.shouldUseTorForImageDownload() ?: false
val imeta =
FileHeader.prepare(item.url.url, mimeType, null, forceProxy).getOrNull()?.let {
IMetaTagBuilder(item.url.url)
.apply {
hash(it.hash)
size(it.size)
it.mimeType?.let { mimeType(it) }
it.dim?.let { dims(it) }
it.blurHash?.let { blurhash(it.blurhash) }
}.build()
}

if (imeta != null) {
iMetaAttachments += imeta
}
}

message =
TextFieldValue(
message.text.replaceRange(lastWordStart, it.end, wordToInsert),
Expand Down

0 comments on commit 306ce9a

Please sign in to comment.