|
|
|
|
@ -3,7 +3,7 @@ package net.pokeranalytics.android.api |
|
|
|
|
import android.content.Context |
|
|
|
|
import kotlinx.coroutines.CoroutineScope |
|
|
|
|
import kotlinx.coroutines.Dispatchers |
|
|
|
|
import kotlinx.coroutines.launch |
|
|
|
|
import kotlinx.coroutines.async |
|
|
|
|
import net.pokeranalytics.android.util.extensions.isNetworkAvailable |
|
|
|
|
import okhttp3.MediaType |
|
|
|
|
import okhttp3.MultipartBody |
|
|
|
|
@ -40,7 +40,7 @@ object BackupApi { |
|
|
|
|
val service = BackupService() |
|
|
|
|
|
|
|
|
|
// curl -F recipient=laurent@staxriver.com -F file=@test.txt https://www.pokeranalytics.net/backup/send |
|
|
|
|
fun backupFile(context: Context, mail: String, fileName: String, fileContent: String): Boolean { |
|
|
|
|
suspend fun backupFile(context: Context, mail: String, fileName: String, fileContent: String): Boolean { |
|
|
|
|
|
|
|
|
|
val filePart = MultipartBody.Part.createFormData( |
|
|
|
|
"file", |
|
|
|
|
@ -51,12 +51,20 @@ object BackupApi { |
|
|
|
|
val mailPart = MultipartBody.Part.createFormData("recipient", mail) |
|
|
|
|
|
|
|
|
|
return if (context.isNetworkAvailable()) { |
|
|
|
|
CoroutineScope(context = Dispatchers.IO).launch { |
|
|
|
|
var success = false |
|
|
|
|
val job = CoroutineScope(context = Dispatchers.IO).async { |
|
|
|
|
success = try { |
|
|
|
|
val response = service.backupApi.postFile(mailPart, filePart).execute() |
|
|
|
|
Timber.d("response code = ${response.code()}") |
|
|
|
|
Timber.d("success = ${response.isSuccessful}") |
|
|
|
|
} |
|
|
|
|
true |
|
|
|
|
} catch (e: Exception) { |
|
|
|
|
Timber.d("!!! backup failed: ${e.message}") |
|
|
|
|
false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
job.await() |
|
|
|
|
return success |
|
|
|
|
} else { |
|
|
|
|
false |
|
|
|
|
} |
|
|
|
|
|