Skip to content

Commit

Permalink
Releases/v0.3.1
Browse files Browse the repository at this point in the history
## Improvements

* fix: Restarts broken due to invalid progress data (#41)



Co-authored-by: Emily Dixon <[email protected]>
  • Loading branch information
daytime-em authored May 10, 2023
1 parent 67c562b commit 3c79d38
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 31 deletions.
2 changes: 1 addition & 1 deletion library/src/main/java/com/mux/video/upload/MuxUploadSdk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object MuxUploadSdk {
UploadMetrics.initialize(appContext)

if (resumeStoppedUploads) {
MuxUploadManager.resumeAllCachedJobs()
val upl = MuxUploadManager.resumeAllCachedJobs()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mux.video.upload.api

import android.util.Log
import androidx.annotation.MainThread
import com.mux.video.upload.MuxUploadSdk
import com.mux.video.upload.internal.*
Expand Down Expand Up @@ -154,14 +155,6 @@ object MuxUploadManager {
private fun newObserveProgressJob(upload: UploadInfo): Job {
// This job has up to three children, one for each of the state flows on UploadInfo
return mainScope.launch {
upload.progressFlow?.let { flow ->
launch {
flow.collect { state ->
launch(Dispatchers.IO) { writeUploadState(upload, state) }
}
}
}

upload.successFlow?.let { flow -> launch { flow.collect { jobFinished(upload) } } }
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mux.video.upload.internal

import android.net.Uri
import android.util.Log
import com.mux.video.upload.MuxUploadSdk
import com.mux.video.upload.api.MuxUpload
import com.mux.video.upload.internal.network.asCountingRequestBody
Expand All @@ -18,9 +19,8 @@ import java.io.IOException
*/
internal class ChunkWorker private constructor(
private val chunk: Chunk,
private val maxRetries: Int,
private val uploadInfo: UploadInfo,
private val videoMimeType: String,
private val remoteUri: Uri,
private val progressFlow: MutableSharedFlow<MuxUpload.Progress>,
) {
companion object {
Expand All @@ -32,11 +32,10 @@ internal class ChunkWorker private constructor(
@JvmSynthetic
internal fun create(
chunk: Chunk,
maxRetries: Int,
uploadInfo: UploadInfo,
videoMimeType: String,
remoteUri: Uri,
progressFlow: MutableSharedFlow<MuxUpload.Progress>,
): ChunkWorker = ChunkWorker(chunk, maxRetries, videoMimeType, remoteUri, progressFlow)
): ChunkWorker = ChunkWorker(chunk, uploadInfo, videoMimeType, progressFlow)
}

private val logger get() = MuxUploadSdk.logger
Expand All @@ -46,7 +45,7 @@ internal class ChunkWorker private constructor(

@Throws
suspend fun upload(): MuxUpload.Progress {
val moreRetries = { triesSoFar: Int -> triesSoFar > maxRetries }
val moreRetries = { triesSoFar: Int -> triesSoFar < uploadInfo.retriesPerChunk }
suspend fun tryUpload(triesSoFar: Int): Result<MuxUpload.Progress> {
try {
val (finalState, httpResponse) = doUpload()
Expand Down Expand Up @@ -77,7 +76,7 @@ internal class ChunkWorker private constructor(
}
}

return tryUpload(0).getOrThrow()
return tryUpload(0).getOrThrow().also { writeUploadState(uploadInfo, it) }
}

@Throws
Expand Down Expand Up @@ -116,7 +115,7 @@ internal class ChunkWorker private constructor(
} // stream.asCountingRequestBody

val request = Request.Builder()
.url(remoteUri.toString())
.url(uploadInfo.remoteUri.toString())
.put(putBody)
.header("Content-Type", videoMimeType)
.header(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ internal class UploadJobFactory private constructor(
progressFlow: MutableSharedFlow<MuxUpload.Progress>
): ChunkWorker = ChunkWorker.create(
chunk = chunk,
maxRetries = uploadInfo.retriesPerChunk,
remoteUri = uploadInfo.remoteUri,
uploadInfo = uploadInfo,
videoMimeType = MIME_TYPE_GENERIC_VIDEO,
progressFlow = progressFlow,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mux.video.upload.internal
import android.content.Context
import android.content.SharedPreferences
import android.net.Uri
import android.util.Log
import com.mux.video.upload.api.MuxUpload
import org.json.JSONArray
import org.json.JSONObject
Expand Down Expand Up @@ -46,18 +47,20 @@ internal fun forgetUploadState(uploadInfo: UploadInfo) {

@JvmSynthetic
internal fun readAllCachedUploads(): List<UploadInfo> {
return UploadPersistence.readEntries().map { it.value }.map {
UploadInfo(
remoteUri = Uri.parse(it.url),
file = it.file,
chunkSize = it.chunkSize,
retriesPerChunk = it.retriesPerChunk,
optOut = it.optOut,
uploadJob = null,
successFlow = null,
progressFlow = null,
errorFlow = null,
)
return UploadPersistence.readEntries()
.map { it.value }
.map {
UploadInfo(
remoteUri = Uri.parse(it.url),
file = it.file,
chunkSize = it.chunkSize,
retriesPerChunk = it.retriesPerChunk,
optOut = it.optOut,
uploadJob = null,
successFlow = null,
progressFlow = null,
errorFlow = null,
)
}
}

Expand Down Expand Up @@ -100,13 +103,15 @@ private object UploadPersistence {
}

@Throws
@Synchronized
private fun writeEntries(entries: Map<String, UploadEntry>) {
val entriesJson = JSONArray()
entries.forEach { entriesJson.put(it.value.toJson()) }
prefs.edit().putString(LIST_KEY, entriesJson.toString()).apply()
}

@Throws
@Synchronized
private fun fetchEntries(): MutableMap<String, UploadEntry> {
val jsonStr = prefs.getString(LIST_KEY, null)
return if (jsonStr == null) {
Expand Down

0 comments on commit 3c79d38

Please sign in to comment.