Added Custom Artist images backup

This commit is contained in:
Prathamesh More 2021-11-27 20:07:22 +05:30
parent 63bd71b7e6
commit dab5ddb838

View file

@ -23,6 +23,7 @@ object BackupHelper {
zipItems.addAll(getDatabaseZipItems(context)) zipItems.addAll(getDatabaseZipItems(context))
zipItems.addAll(getSettingsZipItems(context)) zipItems.addAll(getSettingsZipItems(context))
getUserImageZipItems(context)?.let { zipItems.addAll(it) } getUserImageZipItems(context)?.let { zipItems.addAll(it) }
zipItems.addAll(getCustomArtistZipItems(context))
zipAll(zipItems, backupFile) zipAll(zipItems, backupFile)
} }
@ -80,6 +81,28 @@ object BackupHelper {
} }
} }
private fun getCustomArtistZipItems(context: Context): List<ZipItem> {
val zipItemList = mutableListOf<ZipItem>()
val sharedPrefPath = context.filesDir.parentFile?.absolutePath + "/shared_prefs/"
zipItemList.addAll(
File(context.filesDir, "custom_artist_images")
.listFiles()?.map {
ZipItem(
it.absolutePath,
"$CUSTOM_ARTISTS_PATH${File.separator}custom_artist_images${File.separator}${it.name}"
)
}?.toList() ?: listOf()
)
zipItemList.add(
ZipItem(
sharedPrefPath + File.separator + "custom_artist_image.xml",
"$CUSTOM_ARTISTS_PATH${File.separator}prefs${File.separator}custom_artist_image.xml"
)
)
return zipItemList
}
suspend fun restoreBackup(context: Context, inputStream: InputStream?) { suspend fun restoreBackup(context: Context, inputStream: InputStream?) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
ZipInputStream(inputStream).use { ZipInputStream(inputStream).use {
@ -88,6 +111,16 @@ object BackupHelper {
if (entry.isDatabaseEntry()) restoreDatabase(context, it, entry) if (entry.isDatabaseEntry()) restoreDatabase(context, it, entry)
if (entry.isPreferenceEntry()) restorePreferences(context, it, entry) if (entry.isPreferenceEntry()) restorePreferences(context, it, entry)
if (entry.isImageEntry()) restoreImages(context, it, entry) if (entry.isImageEntry()) restoreImages(context, it, entry)
if (entry.isCustomArtistImageEntry()) restoreCustomArtistImages(
context,
it,
entry
)
if (entry.isCustomArtistPrefEntry()) restoreCustomArtistPrefs(
context,
it,
entry
)
entry = it.nextEntry entry = it.nextEntry
} }
} }
@ -137,6 +170,48 @@ object BackupHelper {
} }
} }
private fun restoreCustomArtistImages(
context: Context,
zipIn: ZipInputStream,
zipEntry: ZipEntry
) {
val parentFolder = File(context.filesDir, "custom_artist_images")
if (!parentFolder.exists()) {
parentFolder.mkdirs()
}
BufferedOutputStream(
FileOutputStream(
File(
parentFolder,
zipEntry.getFileName()
)
)
).use { bos ->
val bytesIn = ByteArray(DEFAULT_BUFFER_SIZE)
var read: Int
while (zipIn.read(bytesIn).also { read = it } != -1) {
bos.write(bytesIn, 0, read)
}
}
}
private fun restoreCustomArtistPrefs(
context: Context,
zipIn: ZipInputStream,
zipEntry: ZipEntry
) {
val filePath =
context.filesDir.parentFile?.absolutePath + "/shared_prefs/" + zipEntry.getFileName()
BufferedOutputStream(FileOutputStream(filePath)).use { bos ->
val bytesIn = ByteArray(DEFAULT_BUFFER_SIZE)
var read: Int
while (zipIn.read(bytesIn).also { read = it } != -1) {
bos.write(bytesIn, 0, read)
}
}
}
val backupRootPath = val backupRootPath =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
.toString() + "/RetroMusic/Backups/" .toString() + "/RetroMusic/Backups/"
@ -145,6 +220,7 @@ object BackupHelper {
private const val DATABASES_PATH = "databases" private const val DATABASES_PATH = "databases"
private const val SETTINGS_PATH = "prefs" private const val SETTINGS_PATH = "prefs"
private const val IMAGES_PATH = "userImages" private const val IMAGES_PATH = "userImages"
private const val CUSTOM_ARTISTS_PATH = "artistImages"
private const val THEME_PREFS_KEY_DEFAULT = "[[kabouzeid_app-theme-helper]]" private const val THEME_PREFS_KEY_DEFAULT = "[[kabouzeid_app-theme-helper]]"
private fun ZipEntry.isDatabaseEntry(): Boolean { private fun ZipEntry.isDatabaseEntry(): Boolean {
@ -159,6 +235,14 @@ object BackupHelper {
return name.startsWith(IMAGES_PATH) return name.startsWith(IMAGES_PATH)
} }
private fun ZipEntry.isCustomArtistImageEntry(): Boolean {
return name.startsWith(CUSTOM_ARTISTS_PATH) && name.contains("custom_artist_images")
}
private fun ZipEntry.isCustomArtistPrefEntry(): Boolean {
return name.startsWith(CUSTOM_ARTISTS_PATH) && name.contains("prefs")
}
private fun ZipEntry.getFileName(): String { private fun ZipEntry.getFileName(): String {
return name.substring(name.lastIndexOf(File.separator)) return name.substring(name.lastIndexOf(File.separator))
} }