This commit is contained in:
Vitor Pamplona
2025-05-15 11:49:34 -04:00
8 changed files with 212 additions and 1 deletions

View File

@@ -0,0 +1,135 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.amethyst.ui.components
import android.content.Context
import android.net.Uri
import android.util.Log
import androidx.core.content.FileProvider
import com.vitorpamplona.amethyst.Amethyst
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileInputStream
import java.io.IOException
object ShareHelper {
private const val TAG = "ShareHelper"
private const val DEFAULT_EXTENSION = "jpg"
private const val SHARED_FILE_PREFIX = "shared_media"
// Media type magic numbers
private val JPEG_MAGIC = byteArrayOf(0xFF.toByte(), 0xD8.toByte())
private val PNG_MAGIC = byteArrayOf(0x89.toByte(), 0x50.toByte(), 0x4E.toByte(), 0x47.toByte())
private val WEBP_HEADER_START = "RIFF".toByteArray()
private val WEBP_HEADER_END = "WEBP".toByteArray()
private val GIF_MAGIC = "GIF8".toByteArray()
suspend fun getSharableUriFromUrl(
context: Context,
imageUrl: String,
): Pair<Uri, String> =
withContext(Dispatchers.IO) {
// Safely get snapshot and file
Amethyst.instance.diskCache.openSnapshot(imageUrl)?.use { snapshot ->
val file = snapshot.data.toFile()
// Determine file extension and prepare sharable file
val fileExtension = getImageExtension(file)
val fileCopy = prepareSharableFile(context, file, fileExtension)
// Return sharable uri
return@use Pair(
FileProvider.getUriForFile(context, "${context.packageName}.provider", fileCopy),
fileExtension,
)
} ?: throw IOException("Unable to open snapshot for: $imageUrl")
}
private fun getImageExtension(file: File): String =
try {
FileInputStream(file).use { inputStream ->
val header = ByteArray(12)
val bytesRead = inputStream.read(header)
if (bytesRead < 4) {
// If we couldn't read at least 4 bytes, default to jpg
return DEFAULT_EXTENSION
}
when {
// JPEG: Check first 2 bytes
matchesMagicNumbers(header, 0, JPEG_MAGIC) -> "jpg"
// PNG: Check first 4 bytes
matchesMagicNumbers(header, 0, PNG_MAGIC) -> "png"
// GIF: Check first 4 bytes for "GIF8"
matchesMagicNumbers(header, 0, GIF_MAGIC) -> "gif"
// WEBP: Check "RIFF" (bytes 0-3) and "WEBP" (bytes 8-11)
matchesMagicNumbers(header, 0, WEBP_HEADER_START) &&
bytesRead >= 12 &&
matchesMagicNumbers(header, 8, WEBP_HEADER_END) -> "webp"
else -> DEFAULT_EXTENSION
}
}
} catch (e: IOException) {
Log.w(TAG, "Could not determine image type for ${file.name}, defaulting to $DEFAULT_EXTENSION", e)
DEFAULT_EXTENSION
}
private fun matchesMagicNumbers(
data: ByteArray,
offset: Int,
magicBytes: ByteArray,
): Boolean {
if (offset + magicBytes.size > data.size) {
return false
}
for (i in magicBytes.indices) {
if (data[offset + i] != magicBytes[i]) {
return false
}
}
return true
}
private fun prepareSharableFile(
context: Context,
originalFile: File,
extension: String,
): File {
val timestamp = System.currentTimeMillis()
val sharableFile = File(context.cacheDir, "${SHARED_FILE_PREFIX}_$timestamp.$extension")
try {
originalFile.copyTo(sharableFile, overwrite = true)
} catch (e: IOException) {
Log.e(TAG, "Failed to copy file for sharing", e)
throw e
}
return sharableFile
}
}

View File

@@ -20,6 +20,8 @@
*/
package com.vitorpamplona.amethyst.ui.components
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.AnimatedVisibilityScope
@@ -47,6 +49,7 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -104,6 +107,7 @@ import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlin.time.Duration.Companion.seconds
@@ -680,6 +684,7 @@ fun ShareImageAction(
hash = content.hash,
mimeType = content.mimeType,
onDismiss = onDismiss,
content = content,
)
} else if (content is MediaPreloadedContent) {
ShareImageAction(
@@ -692,6 +697,7 @@ fun ShareImageAction(
hash = null,
mimeType = content.mimeType,
onDismiss = onDismiss,
content = content,
)
}
}
@@ -708,7 +714,10 @@ fun ShareImageAction(
hash: String?,
mimeType: String?,
onDismiss: () -> Unit,
content: BaseMediaContent? = null,
) {
val scope = rememberCoroutineScope()
DropdownMenu(
expanded = popupExpanded.value,
onDismissRequest = onDismiss,
@@ -751,10 +760,49 @@ fun ShareImageAction(
},
)
}
content?.let {
if (content is MediaUrlImage) {
val context = LocalContext.current
videoUri?.let {
if (videoUri.isNotEmpty()) {
DropdownMenuItem(
text = { Text(stringRes(R.string.share_image)) },
onClick = {
scope.launch { shareImageFile(context, videoUri, mimeType) }
onDismiss()
},
)
}
}
}
}
}
}
private suspend fun verifyHash(content: MediaUrlContent): Boolean? {
private suspend fun shareImageFile(
context: Context,
videoUri: String,
mimeType: String?,
) {
// Get sharable URI and file extension
val (uri, fileExtension) = ShareHelper.getSharableUriFromUrl(context, videoUri)
// Determine mime type, use provided or derive from extension
val determinedMimeType = mimeType ?: "image/$fileExtension"
// Create share intent
val shareIntent =
Intent(Intent.ACTION_SEND).apply {
type = determinedMimeType
putExtra(Intent.EXTRA_STREAM, uri)
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}
context.startActivity(Intent.createChooser(shareIntent, null))
}
private fun verifyHash(content: MediaUrlContent): Boolean? {
if (content.hash == null) return null
Amethyst.instance.diskCache.openSnapshot(content.url)?.use { snapshot ->

View File

@@ -936,4 +936,5 @@
<string name="torrent_no_apps">Pro otevření a stažení souboru nejsou nainstalovány žádné torrent aplikace.</string>
<string name="select_list_to_filter">Vyberte seznam pro filtrování kanálu</string>
<string name="temporary_account">Odhlásit se na zámek zařízení</string>
<string name="share_image">Sdílet obrázek…</string>
</resources>

View File

@@ -941,4 +941,5 @@ anz der Bedingungen ist erforderlich</string>
<string name="torrent_no_apps">Keine Torrent-Apps installiert, um die Datei zu öffnen und herunterzuladen.</string>
<string name="select_list_to_filter">Liste zum Filtern des Feeds auswählen</string>
<string name="temporary_account">Beim Sperren des Geräts abmelden</string>
<string name="share_image">Bild teilen…</string>
</resources>

View File

@@ -216,6 +216,13 @@
<string name="unfollow">अनुचरण ना करें</string>
<string name="channel_created">प्रणाली बनायी गयी</string>
<string name="channel_information_changed_to">"प्रणाली जानकारी परिवर्तित की गयी"</string>
<string name="ephemeral_relay_chat">नश्वर चर्चा</string>
<string name="relay_chat">पुनःप्रसारक चर्चा</string>
<string name="relay_chat_title">पुनःप्रसारक चर्चाएँ</string>
<string name="relay_chat_explainer">पुनःप्रसारक चर्चाएँ आवासी पुनःप्रसारक द्वारा नियन्त्रित चर्चा समुदाएँ हैं।
वे नोस्टर पर सभी के लिए दृश्य हैं तथा उनमें सभी भाग ले सकते हैं।
वे उत्कृष्ट हैं खुले समुदायों के लिए विशिष्ट विषयों पर। इनमें से कुछ समुदाएँ अस्थायी हैं।
तथा इसीलिए समय के साथ चर्चा सन्देश अदृश्य हो जाते हैं</string>
<string name="public_chat">सार्वजनिक चर्चा</string>
<string name="public_chat_title">सार्वजनिक चर्चा उपतथ्य</string>
<string name="public_chat_explainer">सार्वजनिक चर्चाएँ सबके लिए दृश्यमान हैं नोस्टर पर तथा सभी
@@ -489,9 +496,18 @@
<string name="content_warning_hide_all_sensitive_content">संवेदनशील विषयवस्तु सर्वदा छिपाएँ</string>
<string name="content_warning_show_all_sensitive_content">संवेदनशील विषयवस्तु सर्वदा दिखाएँ</string>
<string name="content_warning_see_warnings">विषयवस्तु चेतावनियाँ सर्वदा दिखाएँ</string>
<string name="content_warning_hide_all_sensitive_content_option">छिपाएँ</string>
<string name="content_warning_show_all_sensitive_content_option">दिखाएँ</string>
<string name="content_warning_see_warnings_option">चेतावनी दें</string>
<string name="recommended_apps">अनुशंसित : </string>
<string name="filter_spam_from_strangers">अपरिचित जन के भेजे गये कचरालेखों को छलनी द्वारा हटाएँ</string>
<string name="warn_when_posts_have_reports_from_your_follows">चेतावनी दें जब पत्र सूचित किये गये हों आपके द्वारा अनुचरित व्यक्तियों से</string>
<string name="filter_spam_from_strangers_title">कचरालेख छलनी</string>
<string name="filter_spam_from_strangers_explainer">अज्ञात लोगों से पत्र छिपाएँ जो निरन्तर 5 अथवा अधिक बार यथावत समान थे</string>
<string name="warn_when_posts_have_reports_from_your_follows_title">सूचनाएँ प्राप्त होने पर चेतावनी दें</string>
<string name="warn_when_posts_have_reports_from_your_follows_explainer">चेतावनी सन्देश दिखाता है जब प्रकाशित पत्र 5 अथवा अधिक बार सूचित हो आपके द्वारा अनुचरित लेखाओं से</string>
<string name="show_sensitive_content_title">संवेदनशील विषयवस्तु दिखाएँ</string>
<string name="show_sensitive_content_explainer">चेतावनी सन्देश दिखाता है जब प्रकाशित पत्र के लेखक ने उसे संवेदनशील चिह्नित किया</string>
<string name="new_reaction_symbol">नया प्रतिक्रिया चिह्न</string>
<string name="no_reaction_type_setup_long_press_to_change">इस उपयोगकर्ता के लिए कोई प्रतिक्रिया प्रकार पूर्व चयनित नहीं। हृदयचिह्न घुण्डी पर दीर्घतः दबाएँ परिवर्तन करने के लिए</string>
<string name="zapraiser">ज्सापोपार्जन योजना</string>
@@ -543,6 +559,8 @@
<string name="are_you_sure_you_want_to_log_out">निर्गमनांकन करने पर आपकी सारी स्थानीय जानकारी मिट जाएगी। सुनिश्चित करें कि आपके निजी कुंचिकाएँ सुरक्षित रखें हैं अपनी लेखा नहीं खोना चाहते हैं तो। क्या आप आगे बढना चाहते हैं?</string>
<string name="followed_tags">अनुचरित विषयसूचक</string>
<string name="relay_setup">पुनःप्रसारक</string>
<string name="discover_follows">अनुचरण पोटलियाँ</string>
<string name="discover_reads">पठितव्य</string>
<string name="discover_content">लेख आविष्करण</string>
<string name="discover_marketplace">पण्यक्षेत्र</string>
<string name="discover_live">तत्क्षणप्रसार</string>
@@ -606,6 +624,7 @@
<string name="new_feature_nip17_activate">सक्रिय करें</string>
<string name="messages_create_public_chat">सार्वजनिक</string>
<string name="messages_create_public_private_chat_description">नया निजी अथवा सार्वजनिक झुण्ड</string>
<string name="messages_relay_based">पुनःप्रसारक</string>
<string name="messages_new_message">निजी</string>
<string name="messages_new_message_to">के लिए</string>
<string name="messages_new_message_subject">विषय</string>
@@ -798,6 +817,7 @@
<string name="new_post">नया पत्र प्रकाशन</string>
<string name="new_short">नये छोटे : चित्र अथवा चलचित्र</string>
<string name="new_community_note">नया सामुदायिक टीका</string>
<string name="new_product">नया उत्पाद</string>
<string name="open_all_reactions_to_this_post">इस पत्र प्रकाशन के सभी प्रतिक्रियाओं को खोलें</string>
<string name="close_all_reactions_to_this_post">इस पत्र प्रकाशन के सभी प्रतिक्रियाओं को अवरोधित करें</string>
<string name="reply_description">उत्तर</string>
@@ -947,4 +967,6 @@
<string name="select_list_to_filter">सूचनावली छानने के लिए सूची चुनें</string>
<string name="temporary_account">यन्त्र ताला लगने पर निर्गमनांकन करें</string>
<string name="private_message">निजी सन्देश</string>
<string name="group_relay">चर्चा पुनःप्रसारक</string>
<string name="group_relay_explanation">वह पुनःप्रसारक जिससे इस चर्चा के सभी उपयोगकर्ता जुडते हैं</string>
</resources>

View File

@@ -958,4 +958,5 @@
<string name="private_message">Mensagem Privada</string>
<string name="group_relay">Relé de chat</string>
<string name="group_relay_explanation">O relé a qual todos os usuários deste chat se conectam</string>
<string name="share_image">Compartilhar imagem…</string>
</resources>

View File

@@ -1170,4 +1170,5 @@
<string name="group_relay">Chat Relay</string>
<string name="group_relay_explanation">The relay that all users of this chat connect to</string>
<string name="share_image">Share image…</string>
</resources>

View File

@@ -3,4 +3,6 @@
<external-path
name="external_files"
path="." />
<cache-path name="cache" path="." />
</paths>