Skip to content

Commit

Permalink
fix: catch exception when parsing file for transcription (#42) (opene…
Browse files Browse the repository at this point in the history
  • Loading branch information
dixidroid authored Nov 18, 2024
1 parent a27e22d commit c6b8fa5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
29 changes: 17 additions & 12 deletions core/src/main/java/org/openedx/core/module/TranscriptManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import okhttp3.OkHttpClient
import org.openedx.core.module.download.AbstractDownloader
import org.openedx.core.utils.Directories
import org.openedx.core.utils.IOUtils
import org.openedx.core.utils.Logger
import org.openedx.core.utils.Sha1Util
import org.openedx.foundation.utils.FileUtil
import subtitleFile.FormatSRT
Expand All @@ -21,6 +22,8 @@ class TranscriptManager(
val fileUtil: FileUtil
) {

private val logger = Logger(TAG)

private val transcriptDownloader = object : AbstractDownloader() {
override val client: OkHttpClient
get() = OkHttpClient.Builder().build()
Expand Down Expand Up @@ -62,17 +65,18 @@ class TranscriptManager(
}

private suspend fun startTranscriptDownload(downloadLink: String) {
if (!has(downloadLink)) {
val file = File(getTranscriptDir(), Sha1Util.SHA1(downloadLink))
val result = transcriptDownloader.download(
downloadLink,
file.path
)
if (result == AbstractDownloader.DownloadResult.SUCCESS) {
getInputStream(downloadLink)?.let {
val transcriptTimedTextObject =
convertIntoTimedTextObject(it)
transcriptObject = transcriptTimedTextObject
if (has(downloadLink)) return
val file = File(getTranscriptDir(), Sha1Util.SHA1(downloadLink))
val result = transcriptDownloader.download(
downloadLink,
file.path
)
if (result == AbstractDownloader.DownloadResult.SUCCESS) {
getInputStream(downloadLink)?.let {
try {
transcriptObject = convertIntoTimedTextObject(it)
} catch (e: NullPointerException) {
logger.e(throwable = e, submitCrashReport = true)
}
}
}
Expand All @@ -86,7 +90,7 @@ class TranscriptManager(
try {
transcriptObject = convertIntoTimedTextObject(transcriptInputStream)
} catch (e: Exception) {
e.printStackTrace()
logger.e(throwable = e, submitCrashReport = true)
}
} else {
startTranscriptDownload(transcriptUrl)
Expand Down Expand Up @@ -127,6 +131,7 @@ class TranscriptManager(
}

companion object {
private const val TAG = "TranscriptManager"
private const val FILE_VALIDITY_DURATION_HOURS = 5L
}
}
16 changes: 15 additions & 1 deletion core/src/main/java/org/openedx/core/utils/Logger.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package org.openedx.core.utils

import android.util.Log
import com.google.firebase.crashlytics.FirebaseCrashlytics
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import org.openedx.core.BuildConfig
import org.openedx.core.config.Config

class Logger(private val tag: String) : KoinComponent {

private val config by inject<Config>()

class Logger(private val tag: String) {
fun d(message: () -> String) {
if (BuildConfig.DEBUG) Log.d(tag, message())
}
Expand All @@ -12,6 +19,13 @@ class Logger(private val tag: String) {
if (BuildConfig.DEBUG) Log.e(tag, message())
}

fun e(throwable: Throwable, submitCrashReport: Boolean = false) {
if (BuildConfig.DEBUG) throwable.printStackTrace()
if (submitCrashReport && config.getFirebaseConfig().enabled) {
FirebaseCrashlytics.getInstance().recordException(throwable)
}
}

fun i(message: () -> String) {
if (BuildConfig.DEBUG) Log.i(tag, message())
}
Expand Down

0 comments on commit c6b8fa5

Please sign in to comment.