Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 39 additions & 25 deletions AnkiDroid/src/main/java/com/ichi2/anki/ViewerResourceHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,44 +38,58 @@ class ViewerResourceHandler(
private val mediaDir = CollectionHelper.getMediaDirectory(context)

fun shouldInterceptRequest(request: WebResourceRequest): WebResourceResponse? {
val url = request.url
val path = url.path

if (request.method != "GET" || path == null) {
return null
}
if (path == "/favicon.ico") {
return WebResourceResponse(null, null, ByteArrayInputStream(ByteArray(0)))
}

try {
if (path.startsWith(MATHJAX_PATH_PREFIX)) {
val path = request.url.path ?: return null
val range = request.requestHeaders[RANGE_HEADER]
return when {
request.method != "GET" -> null
path == "/favicon.ico" ->
WebResourceResponse(
null,
null,
ByteArrayInputStream(ByteArray(0)),
)
path.startsWith(MATHJAX_PATH_PREFIX) -> {
val mathjaxAssetPath =
Paths
.get(
"backend/js/vendor/mathjax",
path.removePrefix(MATHJAX_PATH_PREFIX),
).pathString
val inputStream = assetManager.open(mathjaxAssetPath)
return WebResourceResponse(guessMimeType(path), null, inputStream)
try {
WebResourceResponse(guessMimeType(path), null, inputStream)
} catch (_: Exception) {
Timber.d("File $mathjaxAssetPath not found")
null
}
Comment on lines +63 to +68
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a functional change. Exceptions thrown above this aren't catch. And the assertManager.open part is the most likely to throw

}

val file = File(mediaDir, path)
if (!file.exists()) {
return null
range != null -> {
handlePartialContent(file(path) ?: return null, range)
}
request.requestHeaders[RANGE_HEADER]?.let { range ->
return handlePartialContent(file, range)
else -> {
try {
val inputStream = FileInputStream(file(path) ?: return null)
val mimeType = guessMimeType(path)
return WebResourceResponse(mimeType, null, inputStream)
} catch (_: Exception) {
Timber.d("File $path not found")
return null
}
}
val inputStream = FileInputStream(file)
val mimeType = guessMimeType(path)
return WebResourceResponse(mimeType, null, inputStream)
} catch (e: Exception) {
Timber.d("File not found")
return null
}
}

/**
* Returns the file at path if it exists,
*/
private fun file(path: String) =
try {
File(mediaDir, path).takeIf { it.exists() }
} catch (_: Exception) {
Timber.d("can't check whether $path exists.")
null
}

@NeedsTest("seeking audio - 16513")
private fun handlePartialContent(
file: File,
Expand Down