-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
299 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package de.dbauer.expensetracker | ||
|
||
object Constants { | ||
const val DATABASE_NAME = "recurring-expenses" | ||
const val DEFAULT_BACKUP_NAME = "$DATABASE_NAME.rexp" | ||
const val BACKUP_MIME_TYPE = "application/octet-stream" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
app/src/main/java/de/dbauer/expensetracker/model/DatabaseBackupRestore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package de.dbauer.expensetracker.model | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import de.dbauer.expensetracker.forEachEntry | ||
import java.io.File | ||
import java.util.zip.ZipEntry | ||
import java.util.zip.ZipInputStream | ||
import java.util.zip.ZipOutputStream | ||
|
||
class DatabaseBackupRestore { | ||
fun exportDatabaseFile( | ||
databasePath: String, | ||
targetUri: Uri, | ||
applicationContext: Context, | ||
): Boolean { | ||
return try { | ||
writeFilesToZip( | ||
listOf(databasePath, "$databasePath-shm", "$databasePath-wal"), | ||
targetUri, | ||
applicationContext, | ||
) | ||
true | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
false | ||
} | ||
} | ||
|
||
fun importDatabaseFile( | ||
srcZipUri: Uri, | ||
targetPath: String, | ||
applicationContext: Context, | ||
): Boolean { | ||
return try { | ||
extractZipToDirectory( | ||
srcZipUri, | ||
targetPath, | ||
applicationContext, | ||
) | ||
true | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
false | ||
} | ||
} | ||
|
||
private fun writeFilesToZip( | ||
files: List<String>, | ||
targetUri: Uri, | ||
applicationContext: Context, | ||
) { | ||
val outputStream = applicationContext.contentResolver.openOutputStream(targetUri) ?: return | ||
ZipOutputStream(outputStream).use { zipOutputStream -> | ||
files.forEach { file -> | ||
val inputFile = File(file) | ||
val fileName = inputFile.name | ||
|
||
zipOutputStream.putNextEntry(ZipEntry(fileName)) | ||
|
||
inputFile.inputStream().use { input -> | ||
input.copyTo(zipOutputStream) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun extractZipToDirectory( | ||
srcZipUri: Uri, | ||
targetPath: String, | ||
applicationContext: Context, | ||
) { | ||
val inputStream = applicationContext.contentResolver.openInputStream(srcZipUri) ?: return | ||
ZipInputStream(inputStream).use { zipInputStream -> | ||
zipInputStream.forEachEntry { entry -> | ||
val fileName = entry.name | ||
Check failure Code scanning / CodeQL Arbitrary file access during archive extraction ("Zip Slip") High
Unsanitized archive entry, which may contain '..', is used in a
file system operation Error loading related location Loading |
||
if (fileName.contains("..")) return@forEachEntry | ||
val outputFile = File(targetPath, fileName) | ||
outputFile.outputStream().use { output -> | ||
zipInputStream.copyTo(output) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.