From 6ab90504f4a1a1ef7ec987fdd10a11ecc5d291e3 Mon Sep 17 00:00:00 2001 From: Antony Date: Wed, 24 Aug 2022 02:38:49 +0200 Subject: [PATCH] fixes --- .../syncproviders/providers/GithubApi.kt | 102 ++++++++++-------- .../cloudstream3/utils/BackupUtils.kt | 11 +- 2 files changed, 60 insertions(+), 53 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 316bd927..86148a27 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 @@ -3,6 +3,7 @@ package com.lagradost.cloudstream3.syncproviders.providers import com.fasterxml.jackson.annotation.JsonProperty import com.lagradost.cloudstream3.* import com.lagradost.cloudstream3.AcraApplication.Companion.getKey +import com.lagradost.cloudstream3.AcraApplication.Companion.removeKey import com.lagradost.cloudstream3.AcraApplication.Companion.setKey import com.lagradost.cloudstream3.mvvm.logError import com.lagradost.cloudstream3.syncproviders.AuthAPI @@ -46,55 +47,61 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ ) + private fun commitFile(repoUrl: String, githubToken: String){ + val tmpDir = createTempDir() + val git = Git.cloneRepository() + .setURI("https://github.com/$repoUrl.git") + .setDirectory(tmpDir) + .setTimeout(30) + .setCredentialsProvider( + UsernamePasswordCredentialsProvider(githubToken, "") + ) + .call() + createTempFile("backup", "txt", tmpDir) + git.add() + .addFilepattern(".") + .call() + git.commit() + .setAll(true) + .setMessage("Update backup") + .call() + git.remoteAdd() + .setName("origin") + .setUri(URIish("https://github.com/$repoUrl.git")) + .call() + git.push() + .setRemote("https://github.com/$repoUrl.git") + .setTimeout(30) + .setCredentialsProvider( + UsernamePasswordCredentialsProvider(githubToken, "") + ) + .call(); + tmpDir.delete() + } +private class repos{ + +} private suspend fun initLogin(githubToken: String): Boolean{ val response = app.post("https://api.github.com/user/repos", headers= mapOf( - Pair("Accept" , "application/vnd.github+json"), - Pair("Authorization", "token $githubToken"), + "Accept" to "application/vnd.github+json", + "Authorization" to "token $githubToken" ), requestBody = """{"name":"sync data for Cloudstream", "description": "Private repo for cloudstream Account", "private": true}""".toRequestBody( RequestBodyTypes.JSON.toMediaTypeOrNull())) - if (response.isSuccessful) { - val repoUrl = tryParseJson(response.text).let { - setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( - token = githubToken, - repoUrl = it?.repoUrl?: run { - return false - })) - it.repoUrl - } - val tmpDir = createTempDir() - val git = Git.cloneRepository() - .setURI("https://github.com/$repoUrl.git") - .setDirectory(tmpDir) - .setTimeout(30) - .setCredentialsProvider( - UsernamePasswordCredentialsProvider(githubToken, "") - ) - .call() - createTempFile("backup", "txt", tmpDir) - git.add() - .addFilepattern(".") - .call() - git.commit() - .setAll(true) - .setMessage("Update backup") - .call() - git.remoteAdd() - .setName("origin") - .setUri(URIish("https://github.com/$repoUrl.git")) - .call() - git.push() - .setRemote("https://github.com/$repoUrl.git") - .setTimeout(30) - .setCredentialsProvider( - UsernamePasswordCredentialsProvider(githubToken, "") - ) - .call(); - return true + if (!response.isSuccessful) {return false} + + val repoUrl = tryParseJson(response.text).let { + setKey(accountId, GITHUB_USER_KEY, GithubOAuthEntity( + token = githubToken, + repoUrl = it?.repoUrl?: run { + return false + })) + it.repoUrl } - return false + commitFile(repoUrl, githubToken) + return true } override suspend fun login(data: InAppAuthAPI.LoginData): Boolean { @@ -118,23 +125,24 @@ class GithubApi(index: Int) : InAppAuthAPIManager(index){ return InAppAuthAPI.LoginData(username = current.repoUrl, password = current.token) } override suspend fun initialize() { - currentSession = getAuthKey() ?: return // just in case the following fails - setKey(currentSession!!.repoUrl, currentSession!!.token) + currentSession = getAuthKey() + val repoUrl = currentSession?.repoUrl ?: return + val token = currentSession?.token ?: return + setKey(repoUrl, token) } override fun logOut() { - AcraApplication.removeKey(accountId, GITHUB_USER_KEY) + removeKey(accountId, GITHUB_USER_KEY) removeAccountKeys() currentSession = getAuthKey() } override fun loginInfo(): AuthAPI.LoginInfo? { - getAuthKey()?.let { user -> - return AuthAPI.LoginInfo( + return getAuthKey()?.let { user -> + AuthAPI.LoginInfo( profilePicture = null, name = user.repoUrl, accountIndex = accountIndex, ) } - return null } } \ No newline at end of file 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 5fcd5cf0..363ca804 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt @@ -23,6 +23,7 @@ import com.lagradost.cloudstream3.syncproviders.providers.AniListApi.Companion.A 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.GithubApi.Companion.GITHUB_USER_KEY 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 @@ -76,7 +77,7 @@ object BackupUtils { // The plugins themselves are not backed up PLUGINS_KEY, PLUGINS_KEY_LOCAL, - + GITHUB_USER_KEY, OPEN_SUBTITLES_USER_KEY, "nginx_user", // Nginx user key @@ -335,7 +336,7 @@ object BackupUtils { } - fun FragmentActivity.restorePromptGithub(){ + fun FragmentActivity.restorePromptGithub() = ioSafe { val tmpDir = createTempDir() val repoUrl = githubApi.getLatestLoginData()?.username ?: throw IllegalArgumentException ("Requires Username") @@ -348,8 +349,7 @@ object BackupUtils { UsernamePasswordCredentialsProvider("$token", "") ) .call() - val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readLines() - ?.joinToString() + val jsondata = tmpDir.listFiles()?.first { it.name != ".git" }?.readText().toString() val data = parseJson(jsondata?: "") this@restorePromptGithub.restore( data, @@ -357,5 +357,4 @@ object BackupUtils { restoreDataStore = true ) } - } -} \ No newline at end of file +}