AniTrend / anitrend-app

Track all your favorite Anime & Manga with AniTrend as it offers anime or manga lookup, tracking and reference powered by AniList
https://anitrend.co
MIT License
194 stars 26 forks source link

GlideImagePlugin causes crash when malformed url is received #453

Closed wax911 closed 2 years ago

wax911 commented 2 years ago

AniTrend Issue Guidelines

Before opening a new issue, please take a moment to review our community guidelines to make the contribution process easy and effective for everyone involved.

You may find an answer in already closed issues: https://github.com/AniTrend/anitrend-app/issues?q=is%3Aissue+is%3Aclosed

Description Of Bug

Since GlideImagePlugin.kt is heavily used by our markdown parser which has a lot of user generated content, we cannot always guarantee that the url for images is valid, and as such line 49 throws a java.net.MalformedURLException

https://github.com/AniTrend/anitrend-app/blob/709fdd9f389708a6b59fa804acea6b7052ec8c86/app/src/main/java/com/mxt/anitrend/base/plugin/image/GlideImagePlugin.kt#L47-L53

Expected Behaviour

Quickest solution would be to wrap the URL invocation call inside a try catch such as the following to avoid potential crashes:

val url = runCatching {
    URL(drawable.destination)
}.getOrNull()
when (url?.host) {
    "files.catbox.moe" -> {
        headers.addHeader("User-Agent", USER_AGENT_FEDORA)
    }
    else -> { /* Do nothing */ }
}

Alternatively we could also move the header building logic outside of GlideImagePlugin and into a separate class e.g. GlideHeaderBuilder which would also making testing easier

Additional Context

aused by java.net.MalformedURLException: unknown protocol: data
       at java.net.URL.<init>(URL.java:597)
       at java.net.URL.<init>(URL.java:487)
       at java.net.URL.<init>(URL.java:436)
       at com.mxt.anitrend.base.plugin.image.GlideImagePlugin.load(GlideImagePlugin.kt:49)
       at io.noties.markwon.image.glide.GlideImagesPlugin$GlideAsyncDrawableLoader.load(GlideImagesPlugin.java:114)
       at io.noties.markwon.image.AsyncDrawable.setCallback2(AsyncDrawable.java:152)
       at io.noties.markwon.image.AsyncDrawableScheduler.schedule(AsyncDrawableScheduler.java:67)
       at io.noties.markwon.image.glide.GlideImagesPlugin.afterSetText(GlideImagesPlugin.java:98)
       at io.noties.markwon.MarkwonImpl.setParsedMarkdown(MarkwonImpl.java:140)
       at io.noties.markwon.MarkwonImpl.setMarkdown(MarkwonImpl.java:113)
       at com.mxt.anitrend.base.custom.view.text.RichMarkdownTextView.setMarkDownText(RichMarkdownTextView.kt:50)
       at com.mxt.anitrend.binding.RichMarkdownExtensionsKt.richMarkDown(RichMarkdownExtensions.kt:45)
       at com.mxt.anitrend.databinding.FragmentUserAboutBindingImpl.executeBindings(FragmentUserAboutBindingImpl.java:139)
       at androidx.databinding.ViewDataBinding.executeBindingsInternal(ViewDataBinding.java:473)
       at androidx.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:445)
       at androidx.databinding.ViewDataBinding$7.run(ViewDataBinding.java:197)
       at androidx.databinding.ViewDataBinding$8.doFrame(ViewDataBinding.java:291)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:933)
       at android.view.Choreographer.doCallbacks(Choreographer.java:747)
       at android.view.Choreographer.doFrame(Choreographer.java:674)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:921)
       at android.os.Handler.handleCallback(Handler.java:790)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:192)
       at android.app.ActivityThread.main(ActivityThread.java:6759)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:875)
luk1337 commented 2 years ago

@wax911 close?

wax911 commented 2 years ago

@wax911 close?

Definitely! Go for it! 😀

luk1337 commented 2 years ago

Not like I can.