Upstream merge

Internal permission change
Fix url adder
This commit is contained in:
NerdNumber9
2017-05-04 23:38:17 -04:00
parent 3f758d5981
commit 9dbb59f337
616 changed files with 4186 additions and 230 deletions

View File

@@ -0,0 +1,131 @@
package exh.ui.batchadd
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.base.fragment.BaseFragment
import exh.GalleryAdder
import exh.metadata.nullIfBlank
import kotlinx.android.synthetic.main.eh_fragment_batch_add.*
import timber.log.Timber
import kotlin.concurrent.thread
/**
* LoginActivity
*/
class BatchAddFragment : BaseFragment() {
private val galleryAdder by lazy { GalleryAdder() }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedState: Bundle?)
= inflater.inflate(R.layout.eh_fragment_batch_add, container, false)!!
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
setToolbarTitle("Batch add")
setup()
}
fun setup() {
btn_add_galleries.setOnClickListener {
val galleries = galleries_box.text.toString()
//Check text box has content
if(galleries.isNullOrBlank()) {
noGalleriesSpecified()
return@setOnClickListener
}
//Too lazy to actually deal with orientation changes
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
val splitGalleries = galleries.split("\n").map {
it.trim().nullIfBlank()
}.filterNotNull()
val dialog = MaterialDialog.Builder(context)
.title("Adding galleries...")
.progress(false, splitGalleries.size, true)
.cancelable(false)
.canceledOnTouchOutside(false)
.show()
val succeeded = mutableListOf<String>()
val failed = mutableListOf<String>()
thread {
splitGalleries.forEachIndexed { i, s ->
activity.runOnUiThread {
dialog.setContent("Processing: $s")
}
if(addGallery(s)) {
succeeded.add(s)
} else {
failed.add(s)
}
activity.runOnUiThread {
dialog.setProgress(i + 1)
}
}
//Show report
val succeededCount = succeeded.size
val failedCount = failed.size
if(succeeded.isEmpty()) succeeded += "None"
if(failed.isEmpty()) failed += "None"
val succeededReport = succeeded.joinToString(separator = "\n", prefix = "Added:\n")
val failedReport = failed.joinToString(separator = "\n", prefix = "Failed:\n")
val summary = "Summary:\nAdded: $succeededCount gallerie(s)\nFailed: $failedCount gallerie(s)"
val report = listOf(succeededReport, failedReport, summary).joinToString(separator = "\n\n")
activity.runOnUiThread {
//Enable orientation changes again
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
dialog.dismiss()
MaterialDialog.Builder(context)
.title("Batch add report")
.content(report)
.positiveText("Ok")
.cancelable(true)
.canceledOnTouchOutside(true)
.show()
}
}
}
}
fun addGallery(url: String): Boolean {
try {
galleryAdder.addGallery(url, true)
} catch(t: Throwable) {
Timber.e(t, "Could not add gallery!")
return false
}
return true
}
fun noGalleriesSpecified() {
MaterialDialog.Builder(context)
.title("No galleries to add!")
.content("You must specify at least one gallery to add!")
.positiveText("Ok")
.onPositive { materialDialog, _ -> materialDialog.dismiss() }
.cancelable(true)
.canceledOnTouchOutside(true)
.show()
}
companion object {
fun newInstance() = BatchAddFragment()
}
}

View File

@@ -0,0 +1,82 @@
package exh.ui.intercept
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
import eu.kanade.tachiyomi.ui.manga.MangaActivity
import exh.GalleryAdder
import kotlinx.android.synthetic.main.toolbar.*
import timber.log.Timber
import kotlin.concurrent.thread
class InterceptActivity : BaseActivity() {
private val galleryAdder = GalleryAdder()
var finished = false
override fun onCreate(savedInstanceState: Bundle?) {
setAppTheme()
super.onCreate(savedInstanceState)
setContentView(R.layout.eh_activity_intercept)
setupToolbar(toolbar, backNavigation = false)
if(savedInstanceState == null)
thread { setup() }
}
fun setup() {
try {
processLink()
} catch(t: Throwable) {
Timber.e(t, "Could not intercept link!")
if(!finished)
runOnUiThread {
MaterialDialog.Builder(this)
.title("Error")
.content("Could not load this gallery!")
.cancelable(true)
.canceledOnTouchOutside(true)
.cancelListener { onBackPressed() }
.positiveText("Ok")
.onPositive { _, _ -> onBackPressed() }
.dismissListener { onBackPressed() }
.show()
}
}
}
fun processLink() {
if(Intent.ACTION_VIEW == intent.action) {
val manga = galleryAdder.addGallery(intent.dataString)
if(!finished)
startActivity(MangaActivity.newIntent(this, manga, true))
onBackPressed()
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> onBackPressed()
else -> return super.onOptionsItemSelected(item)
}
return true
}
override fun onBackPressed() {
if(!finished)
runOnUiThread {
super.onBackPressed()
}
}
override fun onStop() {
super.onStop()
finished = true
}
}

View File

@@ -0,0 +1,60 @@
package exh.ui.lock
import android.os.Bundle
import com.afollestad.materialdialogs.MaterialDialog
import com.andrognito.pinlockview.PinLockListener
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_lock.*
import uy.kohesive.injekt.injectLazy
class LockActivity : BaseActivity() {
val prefs: PreferencesHelper by injectLazy()
override fun onCreate(savedInstanceState: Bundle?) {
disableLock = true
setTheme(R.style.Theme_Tachiyomi_Dark)
super.onCreate(savedInstanceState)
if(!lockEnabled(prefs)) {
finish()
return
}
setContentView(R.layout.activity_lock)
pin_lock_view.attachIndicatorDots(indicator_dots)
pin_lock_view.pinLength = prefs.lockLength().getOrDefault()
pin_lock_view.setPinLockListener(object : PinLockListener {
override fun onEmpty() {}
override fun onComplete(pin: String) {
if(sha512(pin, prefs.lockSalt().get()!!) == prefs.lockHash().get()) {
//Yay!
finish()
} else {
MaterialDialog.Builder(this@LockActivity)
.title("PIN code incorrect")
.content("The PIN code you entered is incorrect. Please try again.")
.cancelable(true)
.canceledOnTouchOutside(true)
.positiveText("Ok")
.autoDismiss(true)
.show()
pin_lock_view.resetPinLockView()
}
}
override fun onPinChange(pinLength: Int, intermediatePin: String?) {}
})
}
override fun onBackPressed() {
moveTaskToBack(true)
}
}

View File

@@ -0,0 +1,85 @@
package exh.ui.lock
import android.content.Context
import android.support.v7.preference.Preference
import android.text.InputType
import android.util.AttributeSet
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import uy.kohesive.injekt.injectLazy
import java.math.BigInteger
import java.security.SecureRandom
class LockPreference @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
Preference(context, attrs) {
val secureRandom by lazy { SecureRandom() }
val prefs: PreferencesHelper by injectLazy()
override fun onAttached() {
super.onAttached()
updateSummary()
}
fun updateSummary() {
if(lockEnabled(prefs)) {
summary = "Application is locked"
} else {
summary = "Application is not locked, tap to lock"
}
}
override fun onClick() {
super.onClick()
if(!notifyLockSecurity(context)) {
MaterialDialog.Builder(context)
.title("Lock application")
.content("Enter a pin to lock the application. Enter nothing to disable the pin lock.")
.inputRangeRes(0, 10, R.color.material_red_500)
.inputType(InputType.TYPE_CLASS_NUMBER)
.input("", "", { _, c ->
val progressDialog = MaterialDialog.Builder(context)
.title("Saving password")
.progress(true, 0)
.cancelable(false)
.show()
Observable.fromCallable {
savePassword(c.toString())
}.subscribeOn(Schedulers.computation())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
progressDialog.dismiss()
updateSummary()
}
})
.negativeText("Cancel")
.autoDismiss(true)
.cancelable(true)
.canceledOnTouchOutside(true)
.show()
}
}
fun savePassword(password: String) {
val salt: String?
val hash: String?
val length: Int
if(password.isEmpty()) {
salt = null
hash = null
length = -1
} else {
salt = BigInteger(130, secureRandom).toString(32)
hash = sha512(password, salt)
length = password.length
}
prefs.lockSalt().set(salt)
prefs.lockHash().set(hash)
prefs.lockLength().set(length)
}
}

View File

@@ -0,0 +1,91 @@
package exh.ui.lock
import android.annotation.TargetApi
import android.app.Activity
import android.app.AppOpsManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.provider.Settings
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.security.MessageDigest
import kotlin.experimental.and
/**
* Password hashing utils
*/
/**
* Yes, I know SHA512 is fast, but bcrypt on mobile devices is too slow apparently
*/
fun sha512(passwordToHash: String, salt: String): String {
val md = MessageDigest.getInstance("SHA-512")
md.update(salt.toByteArray(charset("UTF-8")))
val bytes = md.digest(passwordToHash.toByteArray(charset("UTF-8")))
val sb = StringBuilder()
for (i in bytes.indices) {
sb.append(Integer.toString((bytes[i] and 0xff.toByte()) + 0x100, 16).substring(1))
}
return sb.toString()
}
/**
* Check if lock is enabled
*/
fun lockEnabled(prefs: PreferencesHelper = Injekt.get())
= prefs.lockHash().get() != null
&& prefs.lockSalt().get() != null
&& prefs.lockLength().getOrDefault() != -1
/**
* Lock the screen
*/
fun showLockActivity(activity: Activity) {
activity.startActivity(Intent(activity, LockActivity::class.java))
}
/**
* Check if the lock will function properly
*
* @return true if action is required, false if lock is working properly
*/
fun notifyLockSecurity(context: Context): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !hasAccessToUsageStats(context)) {
MaterialDialog.Builder(context)
.title("Permission required")
.content("${context.getString(R.string.app_name)} requires the usage stats permission to detect when you leave the app. " +
"This is required for the application lock to function properly. " +
"Press OK to grant this permission now.")
.negativeText("Cancel")
.positiveText("Ok")
.onPositive { _, _ ->
context.startActivity(Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS))
}
.autoDismiss(true)
.cancelable(false)
.show()
return true
} else {
return false
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
fun hasAccessToUsageStats(context: Context): Boolean {
try {
val packageManager = context.packageManager
val applicationInfo = packageManager.getApplicationInfo(context.packageName, 0)
val appOpsManager = context.getSystemService(Context.APP_OPS_SERVICE) as AppOpsManager
val mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName)
return (mode == AppOpsManager.MODE_ALLOWED)
} catch (e: PackageManager.NameNotFoundException) {
return false
}
}

View File

@@ -0,0 +1,218 @@
package exh.ui.login
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.MenuItem
import android.webkit.CookieManager
import android.webkit.WebView
import android.webkit.WebViewClient
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.online.all.EHentai
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
import exh.EXH_SOURCE_ID
import kotlinx.android.synthetic.main.eh_activity_login.*
import kotlinx.android.synthetic.main.toolbar.*
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import timber.log.Timber
import uy.kohesive.injekt.injectLazy
import java.net.HttpCookie
/**
* LoginActivity
*/
class LoginActivity : BaseActivity() {
val preferenceManager: PreferencesHelper by injectLazy()
val sourceManager: SourceManager by injectLazy()
override fun onCreate(savedInstanceState: Bundle?) {
setAppTheme()
super.onCreate(savedInstanceState)
setContentView(R.layout.eh_activity_login)
setup()
setupToolbar(toolbar, backNavigation = false)
}
fun setup() {
btn_cancel.setOnClickListener { onBackPressed() }
btn_recheck.setOnClickListener { webview.loadUrl("http://exhentai.org/") }
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().removeAllCookies {
runOnUiThread {
startWebview()
}
}
} else {
CookieManager.getInstance().removeAllCookie()
startWebview()
}
}
fun startWebview() {
webview.settings.javaScriptEnabled = true
webview.settings.domStorageEnabled = true
webview.loadUrl("https://forums.e-hentai.org/index.php?act=Login")
webview.setWebViewClient(object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
super.onPageFinished(view, url)
Timber.d(url)
val parsedUrl = Uri.parse(url)
if(parsedUrl.host.equals("forums.e-hentai.org", ignoreCase = true)) {
//Hide distracting content
view.loadUrl(HIDE_JS)
//Check login result
if(parsedUrl.getQueryParameter("code")?.toInt() != 0) {
if(checkLoginCookies(url)) view.loadUrl("http://exhentai.org/")
}
} else if(parsedUrl.host.equals("exhentai.org", ignoreCase = true)) {
//At ExHentai, check that everything worked out...
if(applyExHentaiCookies(url)) {
preferenceManager.enableExhentai().set(true)
finishLogin()
}
}
}
})
}
fun finishLogin() {
val progressDialog = MaterialDialog.Builder(this)
.title("Finalizing login")
.progress(true, 0)
.content("Please wait...")
.cancelable(false)
.show()
val eh = sourceManager
.getOnlineSources()
.find { it.id == EXH_SOURCE_ID } as EHentai
Observable.fromCallable {
//I honestly have no idea why we need to call this twice, but it works, so whatever
try {
eh.fetchFavorites()
} catch(ignored: Exception) {}
try {
eh.fetchFavorites()
} catch(ignored: Exception) {}
}.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
progressDialog.dismiss()
onBackPressed()
}
}
/**
* Check if we are logged in
*/
fun checkLoginCookies(url: String): Boolean {
getCookies(url)?.let { parsed ->
return parsed.filter {
(it.name.equals(MEMBER_ID_COOKIE, ignoreCase = true)
|| it.name.equals(PASS_HASH_COOKIE, ignoreCase = true))
&& it.value.isNotBlank()
}.count() >= 2
}
return false
}
/**
* Parse cookies at ExHentai
*/
fun applyExHentaiCookies(url: String): Boolean {
getCookies(url)?.let { parsed ->
var memberId: String? = null
var passHash: String? = null
var igneous: String? = null
parsed.forEach {
when (it.name.toLowerCase()) {
MEMBER_ID_COOKIE -> memberId = it.value
PASS_HASH_COOKIE -> passHash = it.value
IGNEOUS_COOKIE -> igneous = it.value
}
}
//Missing a cookie
if (memberId == null || passHash == null || igneous == null) return false
//Update prefs
preferenceManager.memberIdVal().set(memberId)
preferenceManager.passHashVal().set(passHash)
preferenceManager.igneousVal().set(igneous)
return true
}
return false
}
fun getCookies(url: String): List<HttpCookie>?
= CookieManager.getInstance().getCookie(url)?.let {
it.split("; ").flatMap {
HttpCookie.parse(it)
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> onBackPressed()
else -> return super.onOptionsItemSelected(item)
}
return true
}
companion object {
const val MEMBER_ID_COOKIE = "ipb_member_id"
const val PASS_HASH_COOKIE = "ipb_pass_hash"
const val IGNEOUS_COOKIE = "igneous"
const val HIDE_JS = """
javascript:(function () {
document.getElementsByTagName('body')[0].style.visibility = 'hidden';
document.getElementsByName('submit')[0].style.visibility = 'visible';
document.querySelector('td[width="60%"][valign="top"]').style.visibility = 'visible';
function hide(e) {if(e !== null && e !== undefined) e.style.display = 'none';}
hide(document.querySelector(".errorwrap"));
hide(document.querySelector('td[width="40%"][valign="top"]'));
var child = document.querySelector(".page").querySelector('div');
child.style.padding = null;
var ft = child.querySelectorAll('table');
var fd = child.parentNode.querySelectorAll('div > div');
var fh = document.querySelector('#border').querySelectorAll('td > table');
hide(ft[0]);
hide(ft[1]);
hide(fd[1]);
hide(fd[2]);
hide(child.querySelector('br'));
var error = document.querySelector(".page > div > .borderwrap");
if(error !== null) error.style.visibility = 'visible';
hide(fh[0]);
hide(fh[1]);
hide(document.querySelector("#gfooter"));
hide(document.querySelector(".copyright"));
document.querySelectorAll("td").forEach(function(e) {
e.style.color = "white";
});
var pc = document.querySelector(".postcolor");
if(pc !== null) pc.style.color = "#26353F";
})()
"""
}
}

View File

@@ -0,0 +1,136 @@
package exh.ui.migration
import android.app.Activity
import android.content.pm.ActivityInfo
import android.text.Html
import com.afollestad.materialdialogs.MaterialDialog
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.online.all.EHentai
import exh.isExSource
import exh.isLewdSource
import exh.metadata.MetadataHelper
import exh.metadata.copyTo
import exh.metadata.genericCopyTo
import timber.log.Timber
import uy.kohesive.injekt.injectLazy
import kotlin.concurrent.thread
class MetadataFetchDialog {
val metadataHelper by lazy { MetadataHelper() }
val db: DatabaseHelper by injectLazy()
val sourceManager: SourceManager by injectLazy()
val preferenceHelper: PreferencesHelper by injectLazy()
fun show(context: Activity) {
//Too lazy to actually deal with orientation changes
context.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
val progressDialog = MaterialDialog.Builder(context)
.title("Fetching library metadata")
.content("Preparing library")
.progress(false, 0, true)
.cancelable(false)
.canceledOnTouchOutside(false)
.show()
thread {
db.deleteMangasNotInLibrary().executeAsBlocking()
val libraryMangas = db.getLibraryMangas()
.executeAsBlocking()
.filter {
isLewdSource(it.source)
&& metadataHelper.fetchMetadata(it.url, it.source) == null
}
context.runOnUiThread {
progressDialog.maxProgress = libraryMangas.size
}
//Actual metadata fetch code
libraryMangas.forEachIndexed { i, manga ->
context.runOnUiThread {
progressDialog.setContent("Processing: ${manga.title}")
progressDialog.setProgress(i + 1)
}
try {
val source = sourceManager.get(manga.source)
source?.let {
manga.copyFrom(it.fetchMangaDetails(manga).toBlocking().first())
metadataHelper.fetchMetadata(manga.url, manga.source)?.genericCopyTo(manga)
}
} catch(t: Throwable) {
Timber.e(t, "Could not migrate manga!")
}
}
context.runOnUiThread {
progressDialog.dismiss()
//Enable orientation changes again
context.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
displayMigrationComplete(context)
}
}
}
fun askMigration(activity: Activity) {
var extra = ""
db.getLibraryMangas().asRxSingle().subscribe {
//Not logged in but have ExHentai galleries
if(!preferenceHelper.enableExhentai().getOrDefault()) {
it.find { isExSource(it.source) }?.let {
extra = "<b><font color='red'>If you use ExHentai, please log in first before fetching your library metadata!</font></b><br><br>"
}
}
activity.runOnUiThread {
MaterialDialog.Builder(activity)
.title("Fetch library metadata")
.content(Html.fromHtml("You need to fetch your library's metadata before tag searching in the library will function.<br><br>" +
"This process may take a long time depending on your library size and will also use up a significant amount of internet bandwidth.<br><br>" +
extra +
"This process can be done later if required."))
.positiveText("Migrate")
.negativeText("Later")
.onPositive { _, _ -> show(activity) }
.onNegative({ _, _ -> adviseMigrationLater(activity) })
.cancelable(false)
.canceledOnTouchOutside(false)
.dismissListener {
preferenceHelper.migrateLibraryAsked().set(true)
}.show()
}
}
}
fun adviseMigrationLater(activity: Activity) {
MaterialDialog.Builder(activity)
.title("Metadata fetch canceled")
.content("Library metadata fetch has been canceled.\n\n" +
"You can run this operation later by going to: Settings > E-Hentai > Migrate library metadata")
.positiveText("Ok")
.cancelable(true)
.canceledOnTouchOutside(true)
.show()
}
fun displayMigrationComplete(activity: Activity) {
MaterialDialog.Builder(activity)
.title("Migration complete")
.content("${activity.getString(R.string.app_name)} is now ready for use!")
.positiveText("Ok")
.cancelable(true)
.canceledOnTouchOutside(true)
.show()
}
}

View File

@@ -0,0 +1,16 @@
package exh.ui.migration
class MigrationStatus {
companion object {
val NOT_INITIALIZED = -1
val COMPLETED = 0
//Migration process
val NOTIFY_USER = 1
val OPEN_BACKUP_MENU = 2
val PERFORM_BACKUP = 3
val FINALIZE_MIGRATION = 4
val MAX_MIGRATION_STEPS = 2
}
}

View File

@@ -0,0 +1,79 @@
package exh.ui.migration
import eu.kanade.tachiyomi.data.database.DatabaseHelper
import eu.kanade.tachiyomi.data.database.models.Manga
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import exh.isExSource
import exh.isLewdSource
import exh.metadata.MetadataHelper
import uy.kohesive.injekt.injectLazy
class UrlMigrator {
private val db: DatabaseHelper by injectLazy()
private val prefs: PreferencesHelper by injectLazy()
private val metadataHelper: MetadataHelper by lazy { MetadataHelper() }
fun perform() {
db.inTransaction {
val dbMangas = db.getMangas()
.executeAsBlocking()
//Find all EX mangas
val qualifyingMangas = dbMangas.asSequence().filter {
isLewdSource(it.source)
}
val possibleDups = mutableListOf<Manga>()
val badMangas = mutableListOf<Manga>()
qualifyingMangas.forEach {
if(it.url.startsWith("g/")) //Missing slash at front so we are bad
badMangas.add(it)
else
possibleDups.add(it)
}
//Sort possible dups so we can use binary search on it
possibleDups.sortBy { it.url }
badMangas.forEach { manga ->
//Build fixed URL
val urlWithSlash = "/" + manga.url
//Fix metadata if required
val metadata = metadataHelper.fetchEhMetadata(manga.url, isExSource(manga.source))
metadata?.url?.let {
if(it.startsWith("g/")) { //Check if metadata URL has no slash
metadata.url = urlWithSlash //Fix it
metadataHelper.writeGallery(metadata, manga.source) //Write new metadata to disk
}
}
//If we have a dup (with the fixed url), use the dup instead
val possibleDup = possibleDups.binarySearchBy(urlWithSlash, selector = { it.url })
if(possibleDup >= 0) {
//Make sure it is favorited if we are
if(manga.favorite) {
val dup = possibleDups[possibleDup]
dup.favorite = true
db.insertManga(dup).executeAsBlocking() //Update DB with changes
}
//Delete ourself (but the dup is still there)
db.deleteManga(manga).executeAsBlocking()
return@forEach
}
//No dup, correct URL and reinsert ourselves
manga.url = urlWithSlash
db.insertManga(manga).executeAsBlocking()
}
}
}
fun tryMigration() {
if(!prefs.hasPerformedURLMigration().getOrDefault()) {
perform()
prefs.hasPerformedURLMigration().set(true)
}
}
}