feat: Enhance file upload logging and error handling in TransportManager and ChatViewModel
This commit is contained in:
@@ -88,17 +88,22 @@ object TransportManager {
|
|||||||
val server = getActiveServer()
|
val server = getActiveServer()
|
||||||
|
|
||||||
Log.d(TAG, "📤 Uploading file: $id to $server")
|
Log.d(TAG, "📤 Uploading file: $id to $server")
|
||||||
|
Log.d(TAG, "📤 Content length: ${content.length}, first 50 chars: ${content.take(50)}")
|
||||||
|
|
||||||
// Добавляем в список загрузок
|
// Добавляем в список загрузок
|
||||||
_uploading.value = _uploading.value + TransportState(id, 0)
|
_uploading.value = _uploading.value + TransportState(id, 0)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 🔥 КРИТИЧНО: Преобразуем строку в байты (как desktop делает new Blob([content]))
|
||||||
|
val contentBytes = content.toByteArray(Charsets.UTF_8)
|
||||||
|
Log.d(TAG, "📤 Content bytes length: ${contentBytes.size}")
|
||||||
|
|
||||||
val requestBody = MultipartBody.Builder()
|
val requestBody = MultipartBody.Builder()
|
||||||
.setType(MultipartBody.FORM)
|
.setType(MultipartBody.FORM)
|
||||||
.addFormDataPart(
|
.addFormDataPart(
|
||||||
"file",
|
"file",
|
||||||
id,
|
id,
|
||||||
content.toRequestBody("application/octet-stream".toMediaType())
|
contentBytes.toRequestBody("application/octet-stream".toMediaType())
|
||||||
)
|
)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
@@ -110,6 +115,8 @@ object TransportManager {
|
|||||||
val response = suspendCoroutine<Response> { cont ->
|
val response = suspendCoroutine<Response> { cont ->
|
||||||
client.newCall(request).enqueue(object : Callback {
|
client.newCall(request).enqueue(object : Callback {
|
||||||
override fun onFailure(call: Call, e: IOException) {
|
override fun onFailure(call: Call, e: IOException) {
|
||||||
|
Log.e(TAG, "❌ Upload failed for $id: ${e.message}")
|
||||||
|
Log.e(TAG, "❌ Stack trace: ${e.stackTraceToString()}")
|
||||||
cont.resumeWithException(e)
|
cont.resumeWithException(e)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,12 +127,16 @@ object TransportManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!response.isSuccessful) {
|
if (!response.isSuccessful) {
|
||||||
|
val errorBody = response.body?.string() ?: "No error body"
|
||||||
|
Log.e(TAG, "❌ Upload failed: ${response.code}, body: $errorBody")
|
||||||
throw IOException("Upload failed: ${response.code}")
|
throw IOException("Upload failed: ${response.code}")
|
||||||
}
|
}
|
||||||
|
|
||||||
val responseBody = response.body?.string()
|
val responseBody = response.body?.string()
|
||||||
?: throw IOException("Empty response")
|
?: throw IOException("Empty response")
|
||||||
|
|
||||||
|
Log.d(TAG, "📤 Response body: $responseBody")
|
||||||
|
|
||||||
// Parse JSON response to get tag
|
// Parse JSON response to get tag
|
||||||
val tag = org.json.JSONObject(responseBody).getString("t")
|
val tag = org.json.JSONObject(responseBody).getString("t")
|
||||||
|
|
||||||
|
|||||||
@@ -1934,9 +1934,14 @@ class ChatViewModel(application: Application) : AndroidViewModel(application) {
|
|||||||
var uploadTag = ""
|
var uploadTag = ""
|
||||||
|
|
||||||
if (!isSavedMessages) {
|
if (!isSavedMessages) {
|
||||||
Log.d(TAG, "👤 📤 Uploading avatar to Transport Server...")
|
try {
|
||||||
uploadTag = TransportManager.uploadFile(avatarAttachmentId, encryptedAvatarBlob)
|
Log.d(TAG, "👤 📤 Uploading avatar to Transport Server...")
|
||||||
Log.d(TAG, "👤 📤 Upload complete, tag: $uploadTag")
|
uploadTag = TransportManager.uploadFile(avatarAttachmentId, encryptedAvatarBlob)
|
||||||
|
Log.d(TAG, "👤 📤 Upload complete, tag: $uploadTag")
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG, "👤 ❌ Failed to upload avatar to Transport Server", e)
|
||||||
|
throw e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Preview содержит tag::blurhash (как в desktop)
|
// Preview содержит tag::blurhash (как в desktop)
|
||||||
|
|||||||
Reference in New Issue
Block a user