Proxy applied to thumbnails

This commit is contained in:
Pupil
2020-02-22 20:30:42 +09:00
parent 4a8bff0b98
commit 8204a15276
2 changed files with 44 additions and 23 deletions

View File

@@ -30,6 +30,7 @@ import kotlinx.coroutines.withContext
import xyz.quaver.Code import xyz.quaver.Code
import xyz.quaver.hitomi.GalleryBlock import xyz.quaver.hitomi.GalleryBlock
import xyz.quaver.hitomi.Reader import xyz.quaver.hitomi.Reader
import xyz.quaver.proxy
import xyz.quaver.pupil.util.getCachedGallery import xyz.quaver.pupil.util.getCachedGallery
import xyz.quaver.pupil.util.getDownloadDirectory import xyz.quaver.pupil.util.getDownloadDirectory
import xyz.quaver.pupil.util.json import xyz.quaver.pupil.util.json
@@ -78,7 +79,9 @@ class Cache(context: Context) : ContextWrapper(context) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
val thumbnails = getGalleryBlock(galleryID)?.thumbnails val thumbnails = getGalleryBlock(galleryID)?.thumbnails
try { try {
Base64.encodeToString(URL(thumbnails?.firstOrNull()).readBytes(), Base64.DEFAULT) Base64.encodeToString(URL(thumbnails?.firstOrNull()).openConnection(proxy).getInputStream().use {
it.readBytes()
}, Base64.DEFAULT)
} catch (e: Exception) { } catch (e: Exception) {
null null
} }

View File

@@ -23,6 +23,7 @@ import android.content.Context
import android.content.ContextWrapper import android.content.ContextWrapper
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.util.Log
import android.util.SparseArray import android.util.SparseArray
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
@@ -159,7 +160,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
fun buildClient() = fun buildClient() =
OkHttpClient.Builder() OkHttpClient.Builder()
.addInterceptor(interceptor) .addInterceptor(interceptor)
.dispatcher(Dispatcher(Executors.newFixedThreadPool(4))) .dispatcher(Dispatcher(Executors.newSingleThreadExecutor()))
.proxy(proxy) .proxy(proxy)
.build() .build()
@@ -279,6 +280,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
for (i in reader.galleryInfo.files.indices) { for (i in reader.galleryInfo.files.indices) {
val callback = object : Callback { val callback = object : Callback {
override fun onFailure(call: Call, e: IOException) { override fun onFailure(call: Call, e: IOException) {
Log.i("PUPILD", "FAIL ${call.request().tag()} (${e.message})")
if (Fabric.isInitialized() && e.message != "Canceled") if (Fabric.isInitialized() && e.message != "Canceled")
Crashlytics.logException(e) Crashlytics.logException(e)
@@ -287,43 +289,59 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont
notify(galleryID) notify(galleryID)
if (isCompleted(galleryID)) { CoroutineScope(Dispatchers.IO).launch {
with(Cache(this@DownloadWorker)) { if (isCompleted(galleryID) && clients.indexOfKey(galleryID) >= 0) {
if (isDownloading(galleryID)) { clients.remove(galleryID)
moveToDownload(galleryID) with(Cache(this@DownloadWorker)) {
setDownloading(galleryID, false) if (isDownloading(galleryID)) {
moveToDownload(galleryID)
setDownloading(galleryID, false)
}
} }
} }
clients.remove(galleryID)
} }
} }
override fun onResponse(call: Call, response: Response) { override fun onResponse(call: Call, response: Response) {
response.body().use { Log.i("PUPILD", "OK ${call.request().tag()}")
val res = it.bytes()
val ext =
call.request().url().encodedPath().split('.').last()
Cache(this@DownloadWorker).putImage(galleryID, "%05d.%s".format(i, ext), res) try {
progress[galleryID]?.set(i, Float.POSITIVE_INFINITY) response.body().use {
} val res = it.bytes()
val ext =
call.request().url().encodedPath().split('.').last()
notify(galleryID) Cache(this@DownloadWorker).putImage(galleryID, "%05d.%s".format(i, ext), res)
progress[galleryID]?.set(i, Float.POSITIVE_INFINITY)
}
if (isCompleted(galleryID)) { notify(galleryID)
with(Cache(this@DownloadWorker)) {
if (isDownloading(galleryID)) { CoroutineScope(Dispatchers.IO).launch {
moveToDownload(galleryID) if (isCompleted(galleryID) && clients.indexOfKey(galleryID) >= 0) {
setDownloading(galleryID, false) clients.remove(galleryID)
with(Cache(this@DownloadWorker)) {
if (isDownloading(galleryID)) {
moveToDownload(galleryID)
setDownloading(galleryID, false)
}
}
} }
} }
clients.remove(galleryID)
Log.i("PUPILD", "SUCCESS ${call.request().tag()}")
} catch (e: Exception) {
Log.i("PUPILD", "FAIL ON OK ${call.request().tag()} (${e.message})")
} }
} }
} }
if (progress[galleryID]?.get(i)?.isFinite() == true) if (progress[galleryID]?.get(i)?.isFinite() == true) {
queueDownload(galleryID, reader, i, callback) queueDownload(galleryID, reader, i, callback)
Log.i("PUPILD", "$galleryID QUEUED $i")
} else {
Log.i("PUPILD", "$galleryID SKIPPED $i (${progress[galleryID]?.get(i)})")
}
} }
} }