Merge pull request #960 from greenart7c3/base64
Convert base64 text to images/gifs
This commit is contained in:
@@ -20,6 +20,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.vitorpamplona.amethyst.ui.components
|
package com.vitorpamplona.amethyst.ui.components
|
||||||
|
|
||||||
|
import android.util.Base64
|
||||||
import androidx.compose.foundation.border
|
import androidx.compose.foundation.border
|
||||||
import androidx.compose.foundation.clickable
|
import androidx.compose.foundation.clickable
|
||||||
import androidx.compose.foundation.layout.Arrangement
|
import androidx.compose.foundation.layout.Arrangement
|
||||||
@@ -28,6 +29,7 @@ import androidx.compose.foundation.layout.Column
|
|||||||
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
import androidx.compose.foundation.layout.ExperimentalLayoutApi
|
||||||
import androidx.compose.foundation.layout.FlowRow
|
import androidx.compose.foundation.layout.FlowRow
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
import androidx.compose.foundation.text.InlineTextContent
|
import androidx.compose.foundation.text.InlineTextContent
|
||||||
import androidx.compose.foundation.text.appendInlineContent
|
import androidx.compose.foundation.text.appendInlineContent
|
||||||
@@ -47,6 +49,8 @@ import androidx.compose.runtime.setValue
|
|||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.layout.ContentScale
|
||||||
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.platform.LocalDensity
|
import androidx.compose.ui.platform.LocalDensity
|
||||||
import androidx.compose.ui.platform.LocalFontFamilyResolver
|
import androidx.compose.ui.platform.LocalFontFamilyResolver
|
||||||
import androidx.compose.ui.platform.LocalLayoutDirection
|
import androidx.compose.ui.platform.LocalLayoutDirection
|
||||||
@@ -61,7 +65,12 @@ import androidx.compose.ui.unit.LayoutDirection
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.em
|
import androidx.compose.ui.unit.em
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
|
import coil.compose.AsyncImagePainter
|
||||||
|
import coil.compose.SubcomposeAsyncImage
|
||||||
|
import coil.compose.SubcomposeAsyncImageContent
|
||||||
|
import coil.request.ImageRequest
|
||||||
import com.vitorpamplona.amethyst.commons.compose.produceCachedState
|
import com.vitorpamplona.amethyst.commons.compose.produceCachedState
|
||||||
|
import com.vitorpamplona.amethyst.commons.richtext.Base64Segment
|
||||||
import com.vitorpamplona.amethyst.commons.richtext.BechSegment
|
import com.vitorpamplona.amethyst.commons.richtext.BechSegment
|
||||||
import com.vitorpamplona.amethyst.commons.richtext.CashuSegment
|
import com.vitorpamplona.amethyst.commons.richtext.CashuSegment
|
||||||
import com.vitorpamplona.amethyst.commons.richtext.EmailSegment
|
import com.vitorpamplona.amethyst.commons.richtext.EmailSegment
|
||||||
@@ -74,6 +83,7 @@ import com.vitorpamplona.amethyst.commons.richtext.InvoiceSegment
|
|||||||
import com.vitorpamplona.amethyst.commons.richtext.LinkSegment
|
import com.vitorpamplona.amethyst.commons.richtext.LinkSegment
|
||||||
import com.vitorpamplona.amethyst.commons.richtext.PhoneSegment
|
import com.vitorpamplona.amethyst.commons.richtext.PhoneSegment
|
||||||
import com.vitorpamplona.amethyst.commons.richtext.RegularTextSegment
|
import com.vitorpamplona.amethyst.commons.richtext.RegularTextSegment
|
||||||
|
import com.vitorpamplona.amethyst.commons.richtext.RichTextParser
|
||||||
import com.vitorpamplona.amethyst.commons.richtext.RichTextViewerState
|
import com.vitorpamplona.amethyst.commons.richtext.RichTextViewerState
|
||||||
import com.vitorpamplona.amethyst.commons.richtext.SchemelessUrlSegment
|
import com.vitorpamplona.amethyst.commons.richtext.SchemelessUrlSegment
|
||||||
import com.vitorpamplona.amethyst.commons.richtext.Segment
|
import com.vitorpamplona.amethyst.commons.richtext.Segment
|
||||||
@@ -86,6 +96,7 @@ import com.vitorpamplona.amethyst.model.checkForHashtagWithIcon
|
|||||||
import com.vitorpamplona.amethyst.service.CachedRichTextParser
|
import com.vitorpamplona.amethyst.service.CachedRichTextParser
|
||||||
import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled
|
import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled
|
||||||
import com.vitorpamplona.amethyst.ui.components.markdown.RenderContentAsMarkdown
|
import com.vitorpamplona.amethyst.ui.components.markdown.RenderContentAsMarkdown
|
||||||
|
import com.vitorpamplona.amethyst.ui.note.BlankNote
|
||||||
import com.vitorpamplona.amethyst.ui.note.LoadUser
|
import com.vitorpamplona.amethyst.ui.note.LoadUser
|
||||||
import com.vitorpamplona.amethyst.ui.note.NoteCompose
|
import com.vitorpamplona.amethyst.ui.note.NoteCompose
|
||||||
import com.vitorpamplona.amethyst.ui.note.toShortenHex
|
import com.vitorpamplona.amethyst.ui.note.toShortenHex
|
||||||
@@ -101,6 +112,7 @@ import fr.acinq.secp256k1.Hex
|
|||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
fun isMarkdown(content: String): Boolean =
|
fun isMarkdown(content: String): Boolean =
|
||||||
content.startsWith("> ") ||
|
content.startsWith("> ") ||
|
||||||
@@ -433,6 +445,47 @@ private fun RenderWordWithPreview(
|
|||||||
is HashIndexEventSegment -> TagLink(word, true, quotesLeft, backgroundColor, accountViewModel, nav)
|
is HashIndexEventSegment -> TagLink(word, true, quotesLeft, backgroundColor, accountViewModel, nav)
|
||||||
is SchemelessUrlSegment -> NoProtocolUrlRenderer(word)
|
is SchemelessUrlSegment -> NoProtocolUrlRenderer(word)
|
||||||
is RegularTextSegment -> Text(word.segmentText)
|
is RegularTextSegment -> Text(word.segmentText)
|
||||||
|
is Base64Segment -> ImageFromBase64(word.segmentText)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun ImageFromBase64(base64String: String) {
|
||||||
|
val context = LocalContext.current
|
||||||
|
var imageBytes by remember { mutableStateOf<ByteArray?>(null) }
|
||||||
|
LaunchedEffect(base64String) {
|
||||||
|
imageBytes =
|
||||||
|
withContext(Dispatchers.IO) {
|
||||||
|
var base64String2 = base64String.removePrefix("data:image/jpeg;base64,")
|
||||||
|
RichTextParser.imageExtensions.forEach {
|
||||||
|
base64String2 = base64String2.removePrefix("data:image/$it;base64,")
|
||||||
|
}
|
||||||
|
runCatching { Base64.decode(base64String2, Base64.DEFAULT) }.getOrNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (imageBytes == null) {
|
||||||
|
BlankNote()
|
||||||
|
} else {
|
||||||
|
val request =
|
||||||
|
ImageRequest
|
||||||
|
.Builder(context)
|
||||||
|
.data(imageBytes)
|
||||||
|
.build()
|
||||||
|
|
||||||
|
SubcomposeAsyncImage(
|
||||||
|
model = request,
|
||||||
|
contentDescription = null,
|
||||||
|
contentScale = ContentScale.FillWidth,
|
||||||
|
modifier = Modifier.fillMaxWidth(),
|
||||||
|
) {
|
||||||
|
when (painter.state) {
|
||||||
|
is AsyncImagePainter.State.Success -> {
|
||||||
|
SubcomposeAsyncImageContent()
|
||||||
|
}
|
||||||
|
else -> BlankNote()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -24,7 +24,7 @@ class ExpandableTextCutOffCalculator {
|
|||||||
companion object {
|
companion object {
|
||||||
private const val SHORT_TEXT_LENGTH = 350
|
private const val SHORT_TEXT_LENGTH = 350
|
||||||
private const val SHORTEN_AFTER_LINES = 10
|
private const val SHORTEN_AFTER_LINES = 10
|
||||||
private const val TOO_FAR_SEACH_THE_OTHER_WAY = 450
|
private const val TOO_FAR_SEARCH_THE_OTHER_WAY = 450
|
||||||
|
|
||||||
fun indexToCutOff(content: String): Int {
|
fun indexToCutOff(content: String): Int {
|
||||||
// Cuts the text in the first space or new line after SHORT_TEXT_LENGTH characters
|
// Cuts the text in the first space or new line after SHORT_TEXT_LENGTH characters
|
||||||
@@ -37,7 +37,7 @@ class ExpandableTextCutOffCalculator {
|
|||||||
|
|
||||||
val min = minOf(firstSpaceAfterCut, firstNewLineAfterCut, firstLineAfterLineLimits)
|
val min = minOf(firstSpaceAfterCut, firstNewLineAfterCut, firstLineAfterLineLimits)
|
||||||
|
|
||||||
if (min > TOO_FAR_SEACH_THE_OTHER_WAY) {
|
if (min > TOO_FAR_SEARCH_THE_OTHER_WAY) {
|
||||||
val newString = content.take(SHORT_TEXT_LENGTH)
|
val newString = content.take(SHORT_TEXT_LENGTH)
|
||||||
val firstSpaceBeforeCut =
|
val firstSpaceBeforeCut =
|
||||||
newString.lastIndexOf(' ').let { if (it < 0) content.length else it }
|
newString.lastIndexOf(' ').let { if (it < 0) content.length else it }
|
||||||
|
|||||||
@@ -81,6 +81,21 @@ class RichTextParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun parseBase64Images(content: String): LinkedHashSet<String> {
|
||||||
|
val regex = "data:image/(${imageExtensions.joinToString(separator = "|") { it } });base64,[a-zA-Z0-9+/]+={0,2}"
|
||||||
|
val pattern = Pattern.compile(regex)
|
||||||
|
val matcher = pattern.matcher(content)
|
||||||
|
|
||||||
|
val base64Images = mutableListOf<String>()
|
||||||
|
|
||||||
|
// Find all matches and add them to the list
|
||||||
|
while (matcher.find()) {
|
||||||
|
base64Images.add(matcher.group())
|
||||||
|
}
|
||||||
|
|
||||||
|
return base64Images.mapTo(LinkedHashSet(base64Images.size)) { it }
|
||||||
|
}
|
||||||
|
|
||||||
fun parseValidUrls(content: String): LinkedHashSet<String> {
|
fun parseValidUrls(content: String): LinkedHashSet<String> {
|
||||||
val urls = UrlDetector(content, UrlDetectorOptions.Default).detect()
|
val urls = UrlDetector(content, UrlDetectorOptions.Default).detect()
|
||||||
|
|
||||||
@@ -203,6 +218,13 @@ class RichTextParser {
|
|||||||
): Segment {
|
): Segment {
|
||||||
if (word.isEmpty()) return RegularTextSegment(word)
|
if (word.isEmpty()) return RegularTextSegment(word)
|
||||||
|
|
||||||
|
if (word.startsWith("data:image")) {
|
||||||
|
val base64Images = parseBase64Images(word)
|
||||||
|
if (base64Images.isNotEmpty()) {
|
||||||
|
return Base64Segment(word)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (images.contains(word)) return ImageSegment(word)
|
if (images.contains(word)) return ImageSegment(word)
|
||||||
|
|
||||||
if (urls.contains(word)) return LinkSegment(word)
|
if (urls.contains(word)) return LinkSegment(word)
|
||||||
|
|||||||
+4
@@ -91,6 +91,10 @@ class BechSegment(
|
|||||||
) : Segment(segment)
|
) : Segment(segment)
|
||||||
|
|
||||||
@Immutable
|
@Immutable
|
||||||
|
class Base64Segment(
|
||||||
|
segment: String,
|
||||||
|
) : Segment(segment)
|
||||||
|
|
||||||
open class HashIndexSegment(
|
open class HashIndexSegment(
|
||||||
segment: String,
|
segment: String,
|
||||||
val hex: String,
|
val hex: String,
|
||||||
|
|||||||
Reference in New Issue
Block a user