Automatically prepend instance domain name, when domain name is omitted for images. Fixes very edge case when proxy base url is NULL

This commit is contained in:
Alibek Omarov 2020-12-02 20:02:19 +03:00
parent 9d41527168
commit cba7b2b122
4 changed files with 75 additions and 1 deletions

View File

@ -162,6 +162,7 @@ dependencies {
implementation "com.github.bumptech.glide:glide:$glideVersion" implementation "com.github.bumptech.glide:glide:$glideVersion"
implementation "com.github.bumptech.glide:okhttp3-integration:$glideVersion" implementation "com.github.bumptech.glide:okhttp3-integration:$glideVersion"
kapt "com.github.bumptech.glide:compiler:$glideVersion"
implementation "io.reactivex.rxjava2:rxjava:2.2.20" implementation "io.reactivex.rxjava2:rxjava:2.2.20"
implementation "io.reactivex.rxjava2:rxandroid:2.1.1" implementation "io.reactivex.rxjava2:rxandroid:2.1.1"

View File

@ -36,7 +36,8 @@ import javax.inject.Singleton
BroadcastReceiverModule::class, BroadcastReceiverModule::class,
ViewModelModule::class, ViewModelModule::class,
RepositoryModule::class, RepositoryModule::class,
MediaUploaderModule::class MediaUploaderModule::class,
GlideModule::class
]) ])
interface AppComponent { interface AppComponent {
@Component.Builder @Component.Builder

View File

@ -0,0 +1,12 @@
package com.keylesspalace.tusky.di
import com.keylesspalace.tusky.util.OmittedDomainAppModule
import dagger.Module
import dagger.android.ContributesAndroidInjector
@Module
abstract class GlideModule {
@ContributesAndroidInjector
abstract fun provideOmittedDomainAppModule() : OmittedDomainAppModule
}

View File

@ -0,0 +1,60 @@
package com.keylesspalace.tusky.util
import android.content.Context
import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.load.Options
import com.bumptech.glide.load.data.HttpUrlFetcher
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.load.model.ModelLoader
import com.bumptech.glide.load.model.ModelLoaderFactory
import com.bumptech.glide.load.model.MultiModelLoaderFactory
import com.bumptech.glide.load.model.stream.HttpGlideUrlLoader
import com.bumptech.glide.module.AppGlideModule
import com.bumptech.glide.signature.ObjectKey
import com.keylesspalace.tusky.TuskyApplication
import com.keylesspalace.tusky.db.AccountManager
import java.io.File
import java.io.InputStream
import javax.inject.Inject
@GlideModule
class OmittedDomainAppModule : AppGlideModule() {
@Inject
lateinit var accountManager : AccountManager
override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
Log.d("OmittedDomainAppModule", "registering loader factory")
(context.applicationContext as TuskyApplication).androidInjector.inject(this)
registry.append(String::class.java, InputStream::class.java, OmittedDomainLoaderFactory(accountManager))
}
}
class OmittedDomainLoaderFactory(val accountManager: AccountManager) : ModelLoaderFactory<String, InputStream> {
override fun teardown() = Unit
override fun build(factory: MultiModelLoaderFactory): ModelLoader<String, InputStream> = OmittedDomainLoader(accountManager)
}
class OmittedDomainLoader(val accountManager: AccountManager) : ModelLoader<String, InputStream> {
override fun buildLoadData(model: String, width: Int, height: Int, options: Options): ModelLoader.LoadData<InputStream>?
{
val trueUrl = if(accountManager.activeAccount != null)
"https://" + accountManager.activeAccount!!.domain + model
else model
val timeout = options.get(HttpGlideUrlLoader.TIMEOUT) ?: 100
return ModelLoader.LoadData(ObjectKey(model), HttpUrlFetcher(GlideUrl(trueUrl), timeout))
}
override fun handles(model: String): Boolean {
val file = File(model)
return !file.exists()
}
}