uploadFull.php, downloadFull.php are working. without AES

This commit is contained in:
oxidiert
2025-08-12 17:47:55 +02:00
parent a074146eb9
commit bd8074cb13
3 changed files with 29 additions and 20 deletions

View File

@ -14,7 +14,7 @@ object DatabaseDownloader {
private const val DB_NAME = "questionnaire_database"
private const val API_TOKEN = "MEIN_SUPER_GEHEIMES_TOKEN_12345"
private const val SERVER_DOWNLOAD_URL = "http://49.13.157.44/download.php?token=$API_TOKEN"
private const val SERVER_DOWNLOAD_URL = "http://49.13.157.44/downloadFull.php?token=$API_TOKEN"
private val client = OkHttpClient()
@ -33,20 +33,17 @@ object DatabaseDownloader {
return@launch
}
val encryptedFile = File(context.cacheDir, "downloaded_encrypted_database.db")
val downloadedFile = File(context.cacheDir, "downloaded_database")
response.body?.byteStream()?.use { input ->
FileOutputStream(encryptedFile).use { output ->
FileOutputStream(downloadedFile).use { output ->
input.copyTo(output)
}
}
Log.d("DOWNLOAD", "Verschlüsselte Datei gespeichert: ${encryptedFile.absolutePath}")
val decryptedFile = File(context.cacheDir, "decrypted_database.db")
AES256Helper.decryptFile(encryptedFile, decryptedFile)
Log.d("DOWNLOAD", "Datei gespeichert: ${downloadedFile.absolutePath}")
val dbFile = context.getDatabasePath(DB_NAME)
if (dbFile.exists()) dbFile.delete()
decryptedFile.copyTo(dbFile, overwrite = true)
downloadedFile.copyTo(dbFile, overwrite = true)
Log.d("DOWNLOAD", "Neue DB erfolgreich eingesetzt")

View File

@ -1,6 +1,7 @@
package com.dano.test1
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.util.Log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
@ -15,24 +16,35 @@ import kotlin.system.exitProcess
object DatabaseUploader {
private const val DB_NAME = "questionnaire_database"
private const val ENCRYPTED_FILE_NAME = "exported_encrypted_database.db"
private const val SERVER_UPLOAD_URL = "http://49.13.157.44/upload.php"
private const val SERVER_UPLOAD_URL = "http://49.13.157.44/uploadFull.php"
private const val API_TOKEN = "MEIN_SUPER_GEHEIMES_TOKEN_12345"
private val client = OkHttpClient()
fun uploadEncryptedDatabase(context: Context) {
fun uploadDatabase(context: Context) {
CoroutineScope(Dispatchers.IO).launch {
try {
val dbFile = context.getDatabasePath(DB_NAME)
if (!dbFile.exists()) return@launch
if (!dbFile.exists()) {
Log.e("UPLOAD", "Datenbankdatei existiert nicht: ${dbFile.absolutePath}")
return@launch
}
val encryptedFile = File(context.cacheDir, ENCRYPTED_FILE_NAME)
AES256Helper.encryptFile(dbFile, encryptedFile)
// WAL-Daten in die Hauptdatei schreiben
try {
val db = SQLiteDatabase.openDatabase(
dbFile.absolutePath,
null,
SQLiteDatabase.OPEN_READWRITE
)
db.execSQL("PRAGMA wal_checkpoint(FULL);")
db.close()
Log.d("UPLOAD", "WAL-Checkpoint erfolgreich durchgeführt.")
} catch (e: Exception) {
Log.e("UPLOAD", "Fehler beim WAL-Checkpoint", e)
}
Log.d("UPLOAD", "Datei verschlüsselt: ${encryptedFile.absolutePath}")
uploadFile(encryptedFile, dbFile)
uploadFile(dbFile)
} catch (e: Exception) {
Log.e("UPLOAD", "Fehler beim Hochladen der DB", e)
@ -40,7 +52,7 @@ object DatabaseUploader {
}
}
private fun uploadFile(file: File, originalDbFile: File) {
private fun uploadFile(file: File) {
val requestBody = MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart(
@ -63,7 +75,7 @@ object DatabaseUploader {
override fun onResponse(call: Call, response: Response) {
if (response.isSuccessful) {
Log.d("UPLOAD", "Upload erfolgreich: ${response.message}")
if (originalDbFile.delete()) {
if (file.delete()) {
Log.d("UPLOAD", "Lokale DB gelöscht.")
exitProcess(0)
} else {

View File

@ -555,7 +555,7 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
GlobalValues.LAST_CLIENT_CODE = clientCode
Toast.makeText(activity, "Datenbank wird verschlüsselt...", Toast.LENGTH_SHORT).show()
DatabaseUploader.uploadEncryptedDatabase(activity)
DatabaseUploader.uploadDatabase(activity)
}
}