feat: Implement AttachmentFileManager for handling image attachments; add methods for saving, reading, and managing attachment files

This commit is contained in:
k1ngsterr1
2026-01-26 17:43:23 +05:00
parent 44c0151294
commit 0f652bea86
3 changed files with 265 additions and 5 deletions

View File

@@ -6,6 +6,7 @@ import com.rosetta.messenger.crypto.CryptoManager
import com.rosetta.messenger.crypto.MessageCrypto
import com.rosetta.messenger.database.*
import com.rosetta.messenger.network.*
import com.rosetta.messenger.utils.AttachmentFileManager
import com.rosetta.messenger.utils.AvatarFileManager
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
@@ -331,7 +332,10 @@ class MessageRepository private constructor(private val context: Context) {
privateKey
)
// <EFBFBD> Обрабатываем AVATAR attachments - сохраняем аватар отправителя
// 🖼️ Обрабатываем IMAGE attachments - сохраняем в файл (как в desktop)
processImageAttachments(packet.attachments, packet.chachaKey, privateKey)
// 📸 Обрабатываем AVATAR attachments - сохраняем аватар отправителя
processAvatarAttachments(packet.attachments, packet.fromPublicKey, packet.chachaKey, privateKey)
// <20>🔒 Шифруем plainMessage с использованием приватного ключа
@@ -810,6 +814,56 @@ class MessageRepository private constructor(private val context: Context) {
}
}
/**
* 🖼️ Обработка IMAGE attachments - сохранение в файл (как в desktop)
* Desktop сохраняет: writeFile(`m/${md5(attachment.id + publicKey)}`, encryptedBlob)
* Файлы (FILE тип) НЕ сохраняются - они слишком большие, загружаются с CDN
*/
private fun processImageAttachments(
attachments: List<MessageAttachment>,
encryptedKey: String,
privateKey: String
) {
val publicKey = currentAccount ?: return
for (attachment in attachments) {
// Сохраняем только IMAGE, не FILE (файлы загружаются с CDN при необходимости)
if (attachment.type == AttachmentType.IMAGE && attachment.blob.isNotEmpty()) {
try {
Log.d("MessageRepository", "🖼️ Processing IMAGE attachment: ${attachment.id}")
// 1. Расшифровываем blob с ChaCha ключом сообщения
val decryptedBlob = MessageCrypto.decryptAttachmentBlob(
attachment.blob,
encryptedKey,
privateKey
)
if (decryptedBlob != null) {
// 2. Сохраняем в файл (как в desktop)
val saved = AttachmentFileManager.saveAttachment(
context = context,
blob = decryptedBlob,
attachmentId = attachment.id,
publicKey = publicKey,
privateKey = privateKey
)
if (saved) {
Log.d("MessageRepository", "🖼️ ✅ Image saved to file: ${attachment.id}")
} else {
Log.w("MessageRepository", "🖼️ ⚠️ Failed to save image to file")
}
} else {
Log.w("MessageRepository", "🖼️ ⚠️ Decryption returned null for image")
}
} catch (e: Exception) {
Log.e("MessageRepository", "🖼️ ❌ Failed to process image attachment", e)
}
}
}
}
/**
* Сериализация attachments в JSON с RE-ENCRYPTION для хранения в БД
* Для MESSAGES типа: