From 2e5e5020df3c4d22ecba23721ba758410ec7447c Mon Sep 17 00:00:00 2001 From: Antony Date: Mon, 22 Aug 2022 02:24:42 +0200 Subject: [PATCH] BackupGithub fix --- .../syncproviders/providers/GithubApi.kt | 12 +++++------ .../ui/settings/SettingsUpdates.kt | 3 ++- .../cloudstream3/utils/BackupUtils.kt | 20 ++++++++++++------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt index 9666dc34..c7c7e10b 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/syncproviders/providers/GithubApi.kt @@ -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 diff --git a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt index 6b19042a..9cabd473 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/ui/settings/SettingsUpdates.kt @@ -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 } diff --git a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt index 57ca32b4..7257da58 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -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()