feat: Improve logging for image and avatar downloads with detailed status updates

This commit is contained in:
2026-01-27 20:48:33 +05:00
parent 017f13cb90
commit abe0705a70
2 changed files with 222 additions and 40 deletions

View File

@@ -542,54 +542,83 @@ fun ImageAttachment(
scope.launch {
try {
downloadStatus = DownloadStatus.DOWNLOADING
Log.d(TAG, "📥 Downloading image: ${attachment.id}, tag: $downloadTag")
Log.d(TAG, "=====================================")
Log.d(TAG, "📥 Starting IMAGE download")
Log.d(TAG, "📝 Attachment ID: ${attachment.id}")
Log.d(TAG, "🏷️ Download tag: $downloadTag")
Log.d(TAG, "👤 Sender public key: ${senderPublicKey.take(16)}...")
Log.d(TAG, "=====================================")
// Скачиваем зашифрованный контент
Log.d(TAG, "⬇️ Downloading encrypted content from CDN...")
val startTime = System.currentTimeMillis()
val encryptedContent = TransportManager.downloadFile(attachment.id, downloadTag)
val downloadTime = System.currentTimeMillis() - startTime
Log.d(TAG, "✅ Downloaded ${encryptedContent.length} chars in ${downloadTime}ms")
downloadProgress = 0.5f
downloadStatus = DownloadStatus.DECRYPTING
Log.d(TAG, "🔓 Decrypting image...")
Log.d(TAG, "🔓 Starting decryption...")
// КРИТИЧНО: chachaKey ЗАШИФРОВАН в БД (как в Desktop)
// Сначала расшифровываем его: Buffer.from(await decrypt(message.chacha_key, privatePlain), "binary").toString('utf-8')
// Сначала расшифровываем его, получаем raw bytes
Log.d(TAG, "🔑 Decrypting ChaCha key from sender...")
val decryptedKeyAndNonce = MessageCrypto.decryptKeyFromSender(chachaKey, privateKey)
val decryptKeyString = String(decryptedKeyAndNonce, Charsets.UTF_8)
Log.d(TAG, "🔑 Decrypted chacha_key: ${decryptKeyString.length} chars")
Log.d(TAG, "🔑 ChaCha key decrypted: ${decryptedKeyAndNonce.size} bytes")
// Теперь используем расшифрованный ключ как password для PBKDF2
val decrypted = MessageCrypto.decryptAttachmentBlobWithPassword(
// Используем decryptAttachmentBlobWithPlainKey который правильно конвертирует bytes в password
Log.d(TAG, "🔓 Decrypting image blob with PBKDF2...")
val decryptStartTime = System.currentTimeMillis()
val decrypted = MessageCrypto.decryptAttachmentBlobWithPlainKey(
encryptedContent,
decryptKeyString
decryptedKeyAndNonce
)
val decryptTime = System.currentTimeMillis() - decryptStartTime
Log.d(TAG, "🔓 Decryption completed in ${decryptTime}ms")
downloadProgress = 0.8f
if (decrypted != null) {
Log.d(TAG, "✅ Decrypted blob: ${decrypted.length} chars")
withContext(Dispatchers.IO) {
Log.d(TAG, "🖼️ Converting to bitmap...")
imageBitmap = base64ToBitmap(decrypted)
Log.d(TAG, "✅ Bitmap created: ${imageBitmap?.width}x${imageBitmap?.height}")
// 💾 Сохраняем в файловую систему (как в Desktop)
AttachmentFileManager.saveAttachment(
Log.d(TAG, "💾 Saving to local storage...")
val saved = AttachmentFileManager.saveAttachment(
context = context,
blob = decrypted,
attachmentId = attachment.id,
publicKey = senderPublicKey,
privateKey = privateKey
)
Log.d(TAG, "💾 Image saved to local storage")
Log.d(TAG, "💾 Image saved to local storage: $saved")
}
downloadProgress = 1f
downloadStatus = DownloadStatus.DOWNLOADED
Log.d(TAG, "✅ Image downloaded and decrypted")
Log.d(TAG, "=====================================")
Log.d(TAG, "✅ IMAGE DOWNLOAD COMPLETE")
Log.d(TAG, "=====================================")
} else {
Log.e(TAG, "❌ Decryption returned null")
Log.e(TAG, "=====================================")
Log.e(TAG, "❌ DECRYPTION RETURNED NULL")
Log.e(TAG, "=====================================")
downloadStatus = DownloadStatus.ERROR
}
} catch (e: Exception) {
Log.e(TAG, "❌ Download failed: ${e.message}", e)
Log.e(TAG, "=====================================")
Log.e(TAG, "❌ IMAGE DOWNLOAD FAILED")
Log.e(TAG, "📝 Attachment ID: ${attachment.id}")
Log.e(TAG, "🏷️ Download tag: $downloadTag")
Log.e(TAG, "❌ Error: ${e.message}")
Log.e(TAG, "=====================================")
e.printStackTrace()
downloadStatus = DownloadStatus.ERROR
}
}
} else {
Log.w(TAG, "⚠️ Cannot download image: empty download tag for ${attachment.id}")
}
}
@@ -892,13 +921,12 @@ fun FileAttachment(
downloadStatus = DownloadStatus.DECRYPTING
// КРИТИЧНО: chachaKey ЗАШИФРОВАН в БД (как в Desktop)
// Сначала расшифровываем его
// Сначала расшифровываем его, получаем raw bytes
val decryptedKeyAndNonce = MessageCrypto.decryptKeyFromSender(chachaKey, privateKey)
val decryptKeyString = String(decryptedKeyAndNonce, Charsets.UTF_8)
val decrypted = MessageCrypto.decryptAttachmentBlobWithPassword(
val decrypted = MessageCrypto.decryptAttachmentBlobWithPlainKey(
encryptedContent,
decryptKeyString
decryptedKeyAndNonce
)
downloadProgress = 0.9f
@@ -1184,47 +1212,83 @@ fun AvatarAttachment(
scope.launch {
try {
downloadStatus = DownloadStatus.DOWNLOADING
Log.d(TAG, "📥 Downloading avatar...")
Log.d(TAG, "=====================================")
Log.d(TAG, "👤 Starting AVATAR download")
Log.d(TAG, "📝 Attachment ID: ${attachment.id}")
Log.d(TAG, "🏷️ Download tag: $downloadTag")
Log.d(TAG, "👤 Sender public key: ${senderPublicKey.take(16)}...")
Log.d(TAG, "=====================================")
Log.d(TAG, "⬇️ Downloading encrypted avatar from CDN...")
val startTime = System.currentTimeMillis()
val encryptedContent = TransportManager.downloadFile(attachment.id, downloadTag)
val downloadTime = System.currentTimeMillis() - startTime
Log.d(TAG, "✅ Downloaded ${encryptedContent.length} chars in ${downloadTime}ms")
downloadStatus = DownloadStatus.DECRYPTING
Log.d(TAG, "🔓 Starting decryption...")
// КРИТИЧНО: chachaKey ЗАШИФРОВАН в БД (как в Desktop)
// Сначала расшифровываем его
// Сначала расшифровываем его, получаем raw bytes
Log.d(TAG, "🔑 Decrypting ChaCha key from sender...")
val decryptedKeyAndNonce = MessageCrypto.decryptKeyFromSender(chachaKey, privateKey)
val decryptKeyString = String(decryptedKeyAndNonce, Charsets.UTF_8)
Log.d(TAG, "🔑 ChaCha key decrypted: ${decryptedKeyAndNonce.size} bytes")
val decrypted = MessageCrypto.decryptAttachmentBlobWithPassword(
// Используем decryptAttachmentBlobWithPlainKey который правильно конвертирует bytes в password
Log.d(TAG, "🔓 Decrypting avatar blob with PBKDF2...")
val decryptStartTime = System.currentTimeMillis()
val decrypted = MessageCrypto.decryptAttachmentBlobWithPlainKey(
encryptedContent,
decryptKeyString
decryptedKeyAndNonce
)
val decryptTime = System.currentTimeMillis() - decryptStartTime
Log.d(TAG, "🔓 Decryption completed in ${decryptTime}ms")
if (decrypted != null) {
Log.d(TAG, "✅ Decrypted blob: ${decrypted.length} chars")
withContext(Dispatchers.IO) {
Log.d(TAG, "🖼️ Converting to bitmap...")
avatarBitmap = base64ToBitmap(decrypted)
Log.d(TAG, "✅ Bitmap created: ${avatarBitmap?.width}x${avatarBitmap?.height}")
// 💾 Сохраняем в файловую систему по attachment.id (как в Desktop)
// Desktop: writeFile(`a/${md5(attachment.id + publicKey)}`, encrypted)
AvatarFileManager.saveAvatarByAttachmentId(
Log.d(TAG, "💾 Saving avatar to file system by attachment ID...")
val path = AvatarFileManager.saveAvatarByAttachmentId(
context = context,
base64Image = decrypted,
attachmentId = attachment.id,
publicKey = senderPublicKey
)
Log.d(TAG, "💾 Avatar saved to local file system")
Log.d(TAG, "💾 Avatar saved to: $path")
}
// Сохраняем аватар в репозиторий (для UI обновления)
Log.d(TAG, "💾 Saving avatar to repository for user ${senderPublicKey.take(16)}...")
avatarRepository?.saveAvatar(senderPublicKey, decrypted)
downloadStatus = DownloadStatus.DOWNLOADED
Log.d(TAG, "✅ Avatar downloaded and saved")
Log.d(TAG, "=====================================")
Log.d(TAG, "✅ AVATAR DOWNLOAD COMPLETE")
Log.d(TAG, "=====================================")
} else {
Log.e(TAG, "=====================================")
Log.e(TAG, "❌ AVATAR DECRYPTION RETURNED NULL")
Log.e(TAG, "=====================================")
downloadStatus = DownloadStatus.ERROR
}
} catch (e: Exception) {
Log.e(TAG, "❌ Avatar download failed", e)
Log.e(TAG, "=====================================")
Log.e(TAG, "❌ AVATAR DOWNLOAD FAILED")
Log.e(TAG, "📝 Attachment ID: ${attachment.id}")
Log.e(TAG, "🏷️ Download tag: $downloadTag")
Log.e(TAG, "👤 Sender: ${senderPublicKey.take(16)}...")
Log.e(TAG, "❌ Error: ${e.message}")
Log.e(TAG, "=====================================")
e.printStackTrace()
downloadStatus = DownloadStatus.ERROR
}
}
} else {
Log.w(TAG, "⚠️ Cannot download avatar: empty download tag for ${attachment.id}")
}
}