Use image proxy for downloading #897
This commit is contained in:
@@ -92,7 +92,7 @@ class MangaPageFetcher(
|
|||||||
}
|
}
|
||||||
|
|
||||||
else -> {
|
else -> {
|
||||||
val request = PageLoader.createPageRequest(page, pageUrl)
|
val request = PageLoader.createPageRequest(pageUrl, page.source)
|
||||||
imageProxyInterceptor.interceptPageRequest(request, okHttpClient).use { response ->
|
imageProxyInterceptor.interceptPageRequest(request, okHttpClient).use { response ->
|
||||||
if (!response.isSuccessful) {
|
if (!response.isSuccessful) {
|
||||||
throw HttpException(response)
|
throw HttpException(response)
|
||||||
|
|||||||
@@ -35,16 +35,16 @@ import kotlinx.coroutines.sync.Semaphore
|
|||||||
import kotlinx.coroutines.sync.withPermit
|
import kotlinx.coroutines.sync.withPermit
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
|
||||||
import okhttp3.internal.closeQuietly
|
import okhttp3.internal.closeQuietly
|
||||||
import okio.IOException
|
import okio.IOException
|
||||||
import okio.buffer
|
import okio.buffer
|
||||||
import okio.sink
|
import okio.sink
|
||||||
|
import okio.use
|
||||||
import org.koitharu.kotatsu.R
|
import org.koitharu.kotatsu.R
|
||||||
import org.koitharu.kotatsu.core.exceptions.TooManyRequestExceptions
|
import org.koitharu.kotatsu.core.exceptions.TooManyRequestExceptions
|
||||||
import org.koitharu.kotatsu.core.model.ids
|
import org.koitharu.kotatsu.core.model.ids
|
||||||
import org.koitharu.kotatsu.core.network.CommonHeaders
|
|
||||||
import org.koitharu.kotatsu.core.network.MangaHttpClient
|
import org.koitharu.kotatsu.core.network.MangaHttpClient
|
||||||
|
import org.koitharu.kotatsu.core.network.imageproxy.ImageProxyInterceptor
|
||||||
import org.koitharu.kotatsu.core.parser.MangaDataRepository
|
import org.koitharu.kotatsu.core.parser.MangaDataRepository
|
||||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||||
import org.koitharu.kotatsu.core.prefs.AppSettings
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
||||||
@@ -55,6 +55,7 @@ import org.koitharu.kotatsu.core.util.ext.awaitWorkInfosByTag
|
|||||||
import org.koitharu.kotatsu.core.util.ext.deleteAwait
|
import org.koitharu.kotatsu.core.util.ext.deleteAwait
|
||||||
import org.koitharu.kotatsu.core.util.ext.deleteWork
|
import org.koitharu.kotatsu.core.util.ext.deleteWork
|
||||||
import org.koitharu.kotatsu.core.util.ext.deleteWorks
|
import org.koitharu.kotatsu.core.util.ext.deleteWorks
|
||||||
|
import org.koitharu.kotatsu.core.util.ext.ensureSuccess
|
||||||
import org.koitharu.kotatsu.core.util.ext.getDisplayMessage
|
import org.koitharu.kotatsu.core.util.ext.getDisplayMessage
|
||||||
import org.koitharu.kotatsu.core.util.ext.getWorkInputData
|
import org.koitharu.kotatsu.core.util.ext.getWorkInputData
|
||||||
import org.koitharu.kotatsu.core.util.ext.getWorkSpec
|
import org.koitharu.kotatsu.core.util.ext.getWorkSpec
|
||||||
@@ -73,9 +74,9 @@ import org.koitharu.kotatsu.local.domain.model.LocalManga
|
|||||||
import org.koitharu.kotatsu.parsers.model.Manga
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||||
import org.koitharu.kotatsu.parsers.util.await
|
|
||||||
import org.koitharu.kotatsu.parsers.util.mapToSet
|
import org.koitharu.kotatsu.parsers.util.mapToSet
|
||||||
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
|
||||||
|
import org.koitharu.kotatsu.reader.domain.PageLoader
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
@@ -93,6 +94,7 @@ class DownloadWorker @AssistedInject constructor(
|
|||||||
private val mangaRepositoryFactory: MangaRepository.Factory,
|
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||||
private val settings: AppSettings,
|
private val settings: AppSettings,
|
||||||
@LocalStorageChanges private val localStorageChanges: MutableSharedFlow<LocalManga?>,
|
@LocalStorageChanges private val localStorageChanges: MutableSharedFlow<LocalManga?>,
|
||||||
|
private val imageProxyInterceptor: ImageProxyInterceptor,
|
||||||
notificationFactoryFactory: DownloadNotificationFactory.Factory,
|
notificationFactoryFactory: DownloadNotificationFactory.Factory,
|
||||||
) : CoroutineWorker(appContext, params) {
|
) : CoroutineWorker(appContext, params) {
|
||||||
|
|
||||||
@@ -327,28 +329,24 @@ class DownloadWorker @AssistedInject constructor(
|
|||||||
destination: File,
|
destination: File,
|
||||||
source: MangaSource,
|
source: MangaSource,
|
||||||
): File {
|
): File {
|
||||||
val request = Request.Builder()
|
val request = PageLoader.createPageRequest(url, source)
|
||||||
.url(url)
|
|
||||||
.tag(MangaSource::class.java, source)
|
|
||||||
.header(CommonHeaders.ACCEPT, "image/webp,image/png;q=0.9,image/jpeg,*/*;q=0.8")
|
|
||||||
.cacheControl(CommonHeaders.CACHE_CONTROL_NO_STORE)
|
|
||||||
.get()
|
|
||||||
.build()
|
|
||||||
slowdownDispatcher.delay(source)
|
slowdownDispatcher.delay(source)
|
||||||
val call = okHttp.newCall(request)
|
return imageProxyInterceptor.interceptPageRequest(request, okHttp)
|
||||||
val file = File(destination, UUID.randomUUID().toString() + ".tmp")
|
.ensureSuccess()
|
||||||
try {
|
.use { response ->
|
||||||
val response = call.clone().await()
|
val file = File(destination, UUID.randomUUID().toString() + ".tmp")
|
||||||
checkNotNull(response.body).use { body ->
|
try {
|
||||||
file.sink(append = false).buffer().use {
|
checkNotNull(response.body).use { body ->
|
||||||
it.writeAllCancellable(body.source())
|
file.sink(append = false).buffer().use {
|
||||||
|
it.writeAllCancellable(body.source())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: CancellationException) {
|
||||||
|
file.delete()
|
||||||
|
throw e
|
||||||
}
|
}
|
||||||
|
file
|
||||||
}
|
}
|
||||||
} catch (e: CancellationException) {
|
|
||||||
file.delete()
|
|
||||||
throw e
|
|
||||||
}
|
|
||||||
return file
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun publishState(state: DownloadState) {
|
private suspend fun publishState(state: DownloadState) {
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ class DetectReaderModeUseCase @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val request = PageLoader.createPageRequest(page, url)
|
val request = PageLoader.createPageRequest(url, page.source)
|
||||||
imageProxyInterceptor.interceptPageRequest(request, okHttpClient).use {
|
imageProxyInterceptor.interceptPageRequest(request, okHttpClient).use {
|
||||||
runInterruptible(Dispatchers.IO) {
|
runInterruptible(Dispatchers.IO) {
|
||||||
getBitmapSize(it.body?.byteStream())
|
getBitmapSize(it.body?.byteStream())
|
||||||
|
|||||||
@@ -230,7 +230,7 @@ class PageLoader @Inject constructor(
|
|||||||
|
|
||||||
uri.isFileUri() -> uri
|
uri.isFileUri() -> uri
|
||||||
else -> {
|
else -> {
|
||||||
val request = createPageRequest(page, pageUrl)
|
val request = createPageRequest(pageUrl, page.source)
|
||||||
imageProxyInterceptor.interceptPageRequest(request, okHttp).ensureSuccess().use { response ->
|
imageProxyInterceptor.interceptPageRequest(request, okHttp).ensureSuccess().use { response ->
|
||||||
val body = checkNotNull(response.body) { "Null response body" }
|
val body = checkNotNull(response.body) { "Null response body" }
|
||||||
body.withProgress(progress).use {
|
body.withProgress(progress).use {
|
||||||
@@ -265,12 +265,12 @@ class PageLoader @Inject constructor(
|
|||||||
private const val PREFETCH_LIMIT_DEFAULT = 6
|
private const val PREFETCH_LIMIT_DEFAULT = 6
|
||||||
private const val PREFETCH_MIN_RAM_MB = 80L
|
private const val PREFETCH_MIN_RAM_MB = 80L
|
||||||
|
|
||||||
fun createPageRequest(page: MangaPage, pageUrl: String) = Request.Builder()
|
fun createPageRequest(pageUrl: String, mangaSource: MangaSource) = Request.Builder()
|
||||||
.url(pageUrl)
|
.url(pageUrl)
|
||||||
.get()
|
.get()
|
||||||
.header(CommonHeaders.ACCEPT, "image/webp,image/png;q=0.9,image/jpeg,*/*;q=0.8")
|
.header(CommonHeaders.ACCEPT, "image/webp,image/png;q=0.9,image/jpeg,*/*;q=0.8")
|
||||||
.cacheControl(CommonHeaders.CACHE_CONTROL_NO_STORE)
|
.cacheControl(CommonHeaders.CACHE_CONTROL_NO_STORE)
|
||||||
.tag(MangaSource::class.java, page.source)
|
.tag(MangaSource::class.java, mangaSource)
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user