komikku/app/src/main/java/exh/EXHMigrations.kt

183 lines
6.6 KiB
Kotlin
Raw Normal View History

package exh
2019-04-18 23:47:13 +02:00
import android.content.Context
2019-04-18 23:40:13 +02:00
import com.elvishew.xlog.XLog
2020-08-02 06:50:52 +02:00
import com.pushtorefresh.storio.sqlite.queries.Query
import com.pushtorefresh.storio.sqlite.queries.RawQuery
import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.data.backup.models.DHistory
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Chapter
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.database.models.MangaImpl
import eu.kanade.tachiyomi.data.database.models.Track
2020-08-02 06:50:52 +02:00
import eu.kanade.tachiyomi.data.database.resolvers.MangaUrlPutResolver
import eu.kanade.tachiyomi.data.database.tables.MangaTable
2020-05-04 01:33:28 +02:00
import eu.kanade.tachiyomi.data.library.LibraryUpdateJob
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
2020-05-04 01:33:28 +02:00
import eu.kanade.tachiyomi.data.updater.UpdaterJob
import eu.kanade.tachiyomi.extension.ExtensionUpdateJob
2019-04-14 18:48:59 +02:00
import exh.source.BlacklistedSources
2019-04-18 23:47:13 +02:00
import java.io.File
import java.net.URI
import java.net.URISyntaxException
2020-05-04 00:34:46 +02:00
import uy.kohesive.injekt.injectLazy
object EXHMigrations {
private val db: DatabaseHelper by injectLazy()
2019-04-18 23:40:13 +02:00
private val logger = XLog.tag("EXHMigrations")
/**
* Performs a migration when the application is updated.
*
* @param preferences Preferences of the application.
* @return true if a migration is performed, false otherwise.
*/
fun upgrade(preferences: PreferencesHelper): Boolean {
val context = preferences.context
2020-05-11 01:22:10 +02:00
val oldVersion = preferences.eh_lastVersionCode().get()
2019-04-18 23:40:13 +02:00
try {
if (oldVersion < BuildConfig.VERSION_CODE) {
2020-05-04 01:33:28 +02:00
// Fresh install
if (oldVersion == 0) {
// Set up default background tasks
UpdaterJob.setupTask(context)
ExtensionUpdateJob.setupTask(context)
LibraryUpdateJob.setupTask(context)
return false
}
2020-08-02 06:50:52 +02:00
if (oldVersion < 4) {
db.inTransaction {
// Migrate Tsumino source IDs
db.lowLevel().executeSQL(
RawQuery.builder()
.query(
"""
UPDATE ${MangaTable.TABLE}
SET ${MangaTable.COL_SOURCE} = $HBROWSE_SOURCE_ID
WHERE ${MangaTable.COL_SOURCE} = 6912
""".trimIndent()
)
.affectsTables(MangaTable.TABLE)
.build()
)
// Migrate BHrowse URLs
val hBrowseManga = db.db.get()
.listOfObjects(Manga::class.java)
.withQuery(
Query.builder()
.table(MangaTable.TABLE)
.where("${MangaTable.COL_SOURCE} = $HBROWSE_SOURCE_ID")
.build()
)
.prepare()
.executeAsBlocking()
hBrowseManga.forEach {
2020-08-02 21:08:47 +02:00
it.url = it.url + "/c00001/"
2020-08-02 06:50:52 +02:00
}
db.db.put()
.objects(hBrowseManga)
// Extremely slow without the resolver :/
.withPutResolver(MangaUrlPutResolver())
.prepare()
.executeAsBlocking()
}
}
2020-05-04 01:33:28 +02:00
2020-05-04 00:34:46 +02:00
// if (oldVersion < 1) { }
// do stuff here when releasing changed crap
2019-08-13 07:31:44 +02:00
2019-07-30 01:36:16 +02:00
// TODO BE CAREFUL TO NOT FUCK UP MergedSources IF CHANGING URLs
preferences.eh_lastVersionCode().set(BuildConfig.VERSION_CODE)
2019-04-18 23:40:13 +02:00
return true
}
2020-05-04 00:34:46 +02:00
} catch (e: Exception) {
logger.e("Failed to migrate app from $oldVersion -> ${BuildConfig.VERSION_CODE}!", e)
}
return false
}
fun migrateBackupEntry(manga: MangaImpl): MangaImpl {
// Migrate HentaiCafe source IDs
2020-05-04 00:34:46 +02:00
if (manga.source == 6908L) {
manga.source = HENTAI_CAFE_SOURCE_ID!!
}
2020-02-22 11:12:30 +01:00
// Migrate Tsumino source IDs
2020-05-04 00:34:46 +02:00
if (manga.source == 6909L) {
manga.source = TSUMINO_SOURCE_ID!!
2020-02-22 11:12:30 +01:00
}
2020-08-02 06:50:52 +02:00
if (manga.source == 6912L) {
manga.source = HBROWSE_SOURCE_ID!!
}
// Migrate nhentai URLs
2020-05-04 00:34:46 +02:00
if (manga.source == NHENTAI_SOURCE_ID) {
manga.url = getUrlWithoutDomain(manga.url)
}
// Allow importing of nhentai extension backups
2020-05-04 00:34:46 +02:00
if (manga.source in BlacklistedSources.NHENTAI_EXT_SOURCES) {
manga.source = NHENTAI_SOURCE_ID
}
2019-04-14 18:48:59 +02:00
// Allow importing of English PervEden extension backups
2020-05-04 00:34:46 +02:00
if (manga.source in BlacklistedSources.PERVEDEN_EN_EXT_SOURCES) {
2019-04-14 18:48:59 +02:00
manga.source = PERV_EDEN_EN_SOURCE_ID
}
// Allow importing of Italian PervEden extension backups
2020-05-04 00:34:46 +02:00
if (manga.source in BlacklistedSources.PERVEDEN_IT_EXT_SOURCES) {
manga.source = PERV_EDEN_IT_SOURCE_ID
}
2019-04-14 18:48:59 +02:00
// Allow importing of EHentai extension backups
2020-05-04 00:34:46 +02:00
if (manga.source in BlacklistedSources.EHENTAI_EXT_SOURCES) {
manga.source = EH_SOURCE_ID
}
return manga
}
2019-04-18 23:47:13 +02:00
private fun backupDatabase(context: Context, oldMigrationVersion: Int) {
val backupLocation = File(File(context.filesDir, "exh_db_bck"), "$oldMigrationVersion.bck.db")
2020-05-04 00:34:46 +02:00
if (backupLocation.exists()) return // Do not backup same version twice
2019-04-18 23:47:13 +02:00
val dbLocation = context.getDatabasePath(db.lowLevel().sqliteOpenHelper().databaseName)
try {
dbLocation.copyTo(backupLocation, overwrite = true)
2020-05-04 00:34:46 +02:00
} catch (t: Throwable) {
2019-04-18 23:47:13 +02:00
XLog.w("Failed to backup database!")
}
}
private fun getUrlWithoutDomain(orig: String): String {
return try {
val uri = URI(orig)
var out = uri.path
2020-05-02 06:46:24 +02:00
if (uri.query != null) {
out += "?" + uri.query
2020-05-02 06:46:24 +02:00
}
if (uri.fragment != null) {
out += "#" + uri.fragment
2020-05-02 06:46:24 +02:00
}
out
} catch (e: URISyntaxException) {
orig
}
}
}
data class BackupEntry(
2020-05-04 00:34:46 +02:00
val manga: Manga,
val chapters: List<Chapter>,
val categories: List<String>,
val history: List<DHistory>,
val tracks: List<Track>
)