feat: Implement local file management for avatar attachments with download status checks
This commit is contained in:
@@ -107,6 +107,79 @@ object AvatarFileManager {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверить существует ли аватар по attachment ID (как в Desktop)
|
||||
* Desktop: readFile(`a/${md5(attachment.id + publicKey)}`)
|
||||
*
|
||||
* @param context Android context
|
||||
* @param attachmentId ID attachment
|
||||
* @param publicKey Публичный ключ пользователя
|
||||
* @return true если файл существует
|
||||
*/
|
||||
fun hasAvatarByAttachmentId(context: Context, attachmentId: String, publicKey: String): Boolean {
|
||||
val path = generatePathByAttachmentId(attachmentId, publicKey)
|
||||
val dir = File(context.filesDir, AVATAR_DIR)
|
||||
val file = File(dir, path)
|
||||
return file.exists()
|
||||
}
|
||||
|
||||
/**
|
||||
* Прочитать аватар по attachment ID (как в Desktop)
|
||||
* Desktop: readFile(`a/${md5(attachment.id + publicKey)}`)
|
||||
*
|
||||
* @param context Android context
|
||||
* @param attachmentId ID attachment
|
||||
* @param publicKey Публичный ключ пользователя
|
||||
* @return Base64-encoded изображение или null
|
||||
*/
|
||||
fun readAvatarByAttachmentId(context: Context, attachmentId: String, publicKey: String): String? {
|
||||
val path = generatePathByAttachmentId(attachmentId, publicKey)
|
||||
return readAvatar(context, path)
|
||||
}
|
||||
|
||||
/**
|
||||
* Сохранить аватар по attachment ID (как в Desktop)
|
||||
* Desktop: writeFile(`a/${md5(attachment.id + publicKey)}`, encrypted)
|
||||
*
|
||||
* @param context Android context
|
||||
* @param base64Image Base64-encoded изображение
|
||||
* @param attachmentId ID attachment
|
||||
* @param publicKey Публичный ключ пользователя
|
||||
* @return Путь к файлу
|
||||
*/
|
||||
fun saveAvatarByAttachmentId(context: Context, base64Image: String, attachmentId: String, publicKey: String): String {
|
||||
val path = generatePathByAttachmentId(attachmentId, publicKey)
|
||||
|
||||
// Шифруем данные с паролем "rosetta-a"
|
||||
val encrypted = CryptoManager.encryptWithPassword(base64Image, AVATAR_PASSWORD)
|
||||
|
||||
// Сохраняем в файловую систему
|
||||
val dir = File(context.filesDir, AVATAR_DIR)
|
||||
dir.mkdirs()
|
||||
|
||||
val parts = path.split("/")
|
||||
if (parts.size == 2) {
|
||||
val subDir = File(dir, parts[0])
|
||||
subDir.mkdirs()
|
||||
val file = File(subDir, parts[1])
|
||||
file.writeText(encrypted)
|
||||
android.util.Log.d("AvatarFileManager", "💾 Avatar saved to: ${file.absolutePath}")
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
/**
|
||||
* Генерировать путь по attachment ID (как в Desktop)
|
||||
* Desktop: `a/${md5(attachment.id + publicKey)}`
|
||||
*/
|
||||
private fun generatePathByAttachmentId(attachmentId: String, publicKey: String): String {
|
||||
val md5 = MessageDigest.getInstance("MD5")
|
||||
.digest("$attachmentId$publicKey".toByteArray())
|
||||
.joinToString("") { "%02x".format(it) }
|
||||
return "a/$md5"
|
||||
}
|
||||
|
||||
/**
|
||||
* Генерировать MD5 путь для аватара (совместимо с desktop)
|
||||
* Desktop код:
|
||||
|
||||
Reference in New Issue
Block a user