BackupGithub fix

This commit is contained in:
Antony 2022-08-22 02:24:42 +02:00
parent 24009c5caa
commit 2e5e5020df
3 changed files with 21 additions and 14 deletions

View file

@ -19,7 +19,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
data class GithubOAuthEntity(
var user: String,
var pass: String
var token: String
)
companion object {
const val GITHUB_USER_KEY: String = "github_user" // user data like profile
@ -30,10 +30,10 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
}
override suspend fun login(data: InAppAuthAPI.LoginData): Boolean {
switchToNewAccount()
val username = data.username ?: throw IllegalArgumentException ("Requires Username")
val repoUrl = data.username ?: throw IllegalArgumentException ("Requires Username")
val password = data.password ?: throw IllegalArgumentException ("Requires Password")
try {
setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(username, password))
setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity(repoUrl, password))
registerAccount()
return true
} catch (e: Exception) {
@ -46,11 +46,11 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
override fun getLatestLoginData(): InAppAuthAPI.LoginData? {
val current = getAuthKey() ?: return null
return InAppAuthAPI.LoginData(username = current.user, current.pass)
return InAppAuthAPI.LoginData(username = current.user, password = current.token)
}
override suspend fun initialize() {
currentSession = getAuthKey() ?: return // just in case the following fails
setKey(currentSession!!.user, currentSession!!.pass)
setKey(currentSession!!.user, currentSession!!.token)
}
override fun logOut() {
AcraApplication.removeKey(accountId, GITHUB_USER_KEY)
@ -63,7 +63,7 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){
return AuthAPI.LoginInfo(
profilePicture = null,
name = user.user,
accountIndex = accountIndex
accountIndex = accountIndex,
)
}
return null

View file

@ -15,6 +15,7 @@ import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.getPref
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setPaddingBottom
import com.lagradost.cloudstream3.ui.settings.SettingsFragment.Companion.setUpToolbar
import com.lagradost.cloudstream3.utils.BackupUtils.BackupGithub
import com.lagradost.cloudstream3.utils.BackupUtils.backup
import com.lagradost.cloudstream3.utils.BackupUtils.restorePrompt
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
@ -41,7 +42,7 @@ class SettingsUpdates : PreferenceFragmentCompat() {
//val settingsManager = PreferenceManager.getDefaultSharedPreferences(requireContext())
getPref(R.string.backup_key)?.setOnPreferenceClickListener {
activity?.backup()
activity?.BackupGithub()
return@setOnPreferenceClickListener true
}

View file

@ -16,11 +16,13 @@ import com.lagradost.cloudstream3.R
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.plugins.PLUGINS_KEY
import com.lagradost.cloudstream3.plugins.PLUGINS_KEY_LOCAL
import com.lagradost.cloudstream3.syncproviders.AccountManager.Companion.githubApi
import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_CACHED_LIST
import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_SHOULD_UPDATE_LIST
import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_TOKEN_KEY
import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_UNIXTIME_KEY
import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.ANILIST_USER_KEY
import com.lagradost.cloudstream3.syncproviders.providers.GithubApi
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_CACHED_LIST
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_REFRESH_TOKEN_KEY
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_SHOULD_UPDATE_LIST
@ -298,12 +300,14 @@ object BackupUtils {
val backup = this.getBackup()
ioSafe {
val tmpDir = createTempDir()
val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username")
val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username")
val git = Git.cloneRepository()
.setURI("https://github.com/Github_USERNAME/Repo_NAME.git")
.setURI("$repoUrl.git")
.setDirectory(tmpDir)
.setTimeout(30)
.setCredentialsProvider(
UsernamePasswordCredentialsProvider("HERE GOES GITHUB TOKEN", "")
UsernamePasswordCredentialsProvider(token, "")
)
.call()
@ -317,13 +321,13 @@ object BackupUtils {
.call()
git.remoteAdd()
.setName("origin")
.setUri(URIish("https://github.com/Github_USERNAME/Repo_NAME.git"))
.setUri(URIish("$repoUrl.git"))
.call()
git.push()
.setRemote("https://github.com/Github_USERNAME/Repo_NAME.git")
.setRemote("$repoUrl.git")
.setTimeout(30)
.setCredentialsProvider(
UsernamePasswordCredentialsProvider("HERE GOES GITHUB TOKEN", "")
UsernamePasswordCredentialsProvider(token, "")
)
.call();
}
@ -335,12 +339,14 @@ object BackupUtils {
fun FragmentActivity.restorePromptGithub(){
ioSafe {
val tmpDir = createTempDir()
val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username")
val token = githubApi.getLatestLoginData()?.password ?: throw IllegalArgumentException ("Requires Username")
Git.cloneRepository()
.setURI("https://github.com/Github_USERNAME/Repo_NAME.git")
.setURI("$repoUrl.git")
.setDirectory(tmpDir)
.setTimeout(30)
.setCredentialsProvider(
UsernamePasswordCredentialsProvider("", "")
UsernamePasswordCredentialsProvider("$token", "")
)
.call()
val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readLines()