feat: add remote sync capability - improve downloading

This commit is contained in:
Martin Filo 2023-04-05 01:19:32 +02:00
parent 5ec443916b
commit 92578e54dd
2 changed files with 31 additions and 11 deletions

View file

@ -83,6 +83,12 @@ interface BackupAPI<LOGIN_DATA> {
throttle(input) throttle(input)
} }
fun workNow(input: INPUT? = null) {
Log.d("SYNC_API", "[$id] runs immediate")
stop()
onWork(input)
}
fun stop() { fun stop() {
runnable?.let { runnable?.let {
handler.removeCallbacks(it) handler.removeCallbacks(it)

View file

@ -28,8 +28,8 @@ import com.lagradost.cloudstream3.utils.AppUtils.toJson
import com.lagradost.cloudstream3.utils.BackupUtils import com.lagradost.cloudstream3.utils.BackupUtils
import com.lagradost.cloudstream3.utils.BackupUtils.getBackup import com.lagradost.cloudstream3.utils.BackupUtils.getBackup
import com.lagradost.cloudstream3.utils.BackupUtils.restore import com.lagradost.cloudstream3.utils.BackupUtils.restore
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
import com.lagradost.cloudstream3.utils.DataStore import com.lagradost.cloudstream3.utils.DataStore
import com.lagradost.cloudstream3.utils.DataStore.getSharedPrefs
import com.lagradost.cloudstream3.utils.DataStore.removeKey import com.lagradost.cloudstream3.utils.DataStore.removeKey
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import java.io.InputStream import java.io.InputStream
@ -100,7 +100,7 @@ class GoogleDriveApi(index: Int) :
) )
storeValue(K.TOKEN, googleTokenResponse) storeValue(K.TOKEN, googleTokenResponse)
runDownloader() runDownloader(true)
tempAuthFlow = null tempAuthFlow = null
return true return true
@ -114,7 +114,9 @@ class GoogleDriveApi(index: Int) :
return return
} }
runDownloader() ioSafe {
runDownloader(true)
}
} }
override fun loginInfo(): AuthAPI.LoginInfo? { override fun loginInfo(): AuthAPI.LoginInfo? {
@ -203,7 +205,7 @@ class GoogleDriveApi(index: Int) :
old.orEmpty().keys.subtract(new.orEmpty().keys).toTypedArray() old.orEmpty().keys.subtract(new.orEmpty().keys).toTypedArray()
override fun Context.createBackup(loginData: InAppOAuth2API.LoginData) { override fun Context.createBackup(loginData: InAppOAuth2API.LoginData) {
val drive = getDriveService()!! val drive = getDriveService() ?: return
val fileName = loginData.fileName val fileName = loginData.fileName
val syncFileId = loginData.syncFileId val syncFileId = loginData.syncFileId
@ -219,7 +221,10 @@ class GoogleDriveApi(index: Int) :
val fileId = getOrCreateSyncFileId(drive, loginData) val fileId = getOrCreateSyncFileId(drive, loginData)
if (fileId != null) { if (fileId != null) {
try { try {
val file = drive.files().update(fileId, fileMetadata, fileContent).execute() val file = drive.files()
.update(fileId, fileMetadata, fileContent)
.setKeepRevisionForever(false)
.execute()
loginData.syncFileId = file.id loginData.syncFileId = file.id
} catch (_: Exception) { } catch (_: Exception) {
val file = drive.files().create(fileMetadata, fileContent).execute() val file = drive.files().create(fileMetadata, fileContent).execute()
@ -277,11 +282,15 @@ class GoogleDriveApi(index: Int) :
?.getOrNull(0) ?.getOrNull(0)
?.id ?.id
if (existingFileId != null && loginData.syncFileId == null) { if (loginData.syncFileId == null) {
loginData.syncFileId = existingFileId if (existingFileId != null) {
storeValue(K.LOGIN_DATA, loginData) loginData.syncFileId = existingFileId
storeValue(K.LOGIN_DATA, loginData)
return existingFileId return existingFileId
}
return null
} }
val verifyId = drive.files().get(existingFileId) val verifyId = drive.files().get(existingFileId)
@ -337,8 +346,13 @@ class GoogleDriveApi(index: Int) :
} }
} }
private fun runDownloader() { private fun runDownloader(runNow: Boolean = false) {
continuousDownloader.work() if (runNow) {
continuousDownloader.workNow()
} else {
continuousDownloader.work()
}
} }
private fun getCredentialsFromStore(): Credential? { private fun getCredentialsFromStore(): Credential? {