Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tom5079 committed Feb 1, 2022
1 parent 961c731 commit eb98424
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 71 deletions.
13 changes: 1 addition & 12 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
minSdkVersion 16
targetSdkVersion 31
versionCode 69
versionName "5.3.1"
versionName "5.3.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
Expand Down
2 changes: 1 addition & 1 deletion app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"filters": [],
"attributes": [],
"versionCode": 69,
"versionName": "5.3.1",
"versionName": "5.3.2",
"outputFile": "app-release.apk"
}
],
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/xyz/quaver/pupil/Pupil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ lateinit var runtime: QuickJs
private set

class Pupil : Application() {

companion object {
lateinit var instance: Pupil
private set
Expand Down
17 changes: 9 additions & 8 deletions app/src/main/java/xyz/quaver/pupil/hitomi/common.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

package xyz.quaver.pupil.hitomi

import kotlinx.coroutines.Job
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import okhttp3.Request
import xyz.quaver.pupil.client
import xyz.quaver.pupil.runtime
import xyz.quaver.pupil.runtimeReady
import java.io.IOException
import java.net.URL
import java.util.concurrent.Executors
Expand Down Expand Up @@ -84,11 +82,11 @@ data class GalleryInfo(
val groups: List<Group>? = null,
val parodys: List<Parody>? = null,
val tags: List<Tag>? = null,
val related: List<Int>,
val languages: List<Language>,
val related: List<Int> = emptyList(),
val languages: List<Language> = emptyList(),
val characters: List<Character>? = null,
val scene_indexes: List<Int>,
val files: List<GalleryFiles>
val scene_indexes: List<Int>? = emptyList(),
val files: List<GalleryFiles> = emptyList()
)

val json = Json {
Expand Down Expand Up @@ -135,13 +133,16 @@ val evaluationContext = Executors.newSingleThreadExecutor().asCoroutineDispatche
object gg {

suspend fun m(g: Int): Int = withContext(evaluationContext) {
while (!runtimeReady) delay(1000)
runtime.evaluate("gg.m($g)").toString().toInt()
}
suspend fun b(): String = withContext(evaluationContext) {
while (!runtimeReady) delay(1000)
runtime.evaluate("gg.b").toString()
}

suspend fun s(h: String): String = withContext(evaluationContext) {
while (!runtimeReady) delay(1000)
runtime.evaluate("gg.s('$h')").toString()
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/xyz/quaver/pupil/hitomi/galleryblock.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ data class GalleryBlock(
val type: String,
val language: String,
val relatedTags: List<String>,
val groups: List<String>
val groups: List<String> = emptyList()
)

suspend fun getGalleryBlock(galleryID: Int) : GalleryBlock {
Expand Down
34 changes: 15 additions & 19 deletions app/src/main/java/xyz/quaver/pupil/services/DownloadService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -220,28 +220,23 @@ class DownloadService : Service() {

CoroutineScope(Dispatchers.IO).launch {
runCatching {
response.also {
if (it.code() != 200) throw IOException(
"$galleryID $index ${response.request().url()} CODE ${it.code()}"
)
}.body()?.use {
val padding = ceil(progress[galleryID]?.size?.let { log10(it.toFloat()) } ?: 0F).toInt()
val image = response.also { if (it.code() != 200) throw IOException( "$galleryID $index ${response.request().url()} CODE ${it.code()}" ) }.body()?.use { it.bytes() } ?: throw Exception("Response null")
val padding = ceil(progress[galleryID]?.size?.let { log10(it.toFloat()) } ?: 0F).toInt()

Cache.getInstance(this@DownloadService, galleryID)
.putImage(index, "${index.toString().padStart(padding, '0')}.$ext", it.byteStream())
Cache.getInstance(this@DownloadService, galleryID)
.putImage(index, "${index.toString().padStart(padding, '0')}.$ext", image)

progress[galleryID]?.set(index, Float.POSITIVE_INFINITY)
notify(galleryID)
progress[galleryID]?.set(index, Float.POSITIVE_INFINITY)
notify(galleryID)

if (isCompleted(galleryID)) {
if (DownloadManager.getInstance(this@DownloadService)
.getDownloadFolder(galleryID) != null
)
Cache.getInstance(this@DownloadService, galleryID).moveToDownload()
if (isCompleted(galleryID)) {
if (DownloadManager.getInstance(this@DownloadService)
.getDownloadFolder(galleryID) != null
)
Cache.getInstance(this@DownloadService, galleryID).moveToDownload()

startId?.let { stopSelf(it) }
}
} ?: throw Exception("Response null")
startId?.let { stopSelf(it) }
}
}.onFailure {
FirebaseCrashlytics.getInstance().recordException(it)
}
Expand Down Expand Up @@ -329,7 +324,8 @@ class DownloadService : Service() {
}

if (isCompleted(galleryID)) {
Cache.getInstance(this@DownloadService, galleryID).moveToDownload()
if (DownloadManager.getInstance(this@DownloadService).getDownloadFolder(galleryID) != null)
Cache.getInstance(this@DownloadService, galleryID).moveToDownload()

notificationManager.cancel(galleryID)
startId?.let { stopSelf(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import xyz.quaver.io.util.readText
import xyz.quaver.io.util.writeText
import xyz.quaver.pupil.R
import xyz.quaver.pupil.histories
import xyz.quaver.pupil.hitomi.json
import xyz.quaver.pupil.util.byteToString
import xyz.quaver.pupil.util.downloader.Cache
import xyz.quaver.pupil.util.downloader.DownloadManager
Expand Down Expand Up @@ -118,7 +119,7 @@ class ManageStorageFragment : PreferenceFragmentCompat(), Preference.OnPreferenc
if (!metadataFile.exists()) return@forEach

val metadata = metadataFile.readText()?.let {
Json.decodeFromString<Metadata>(it)
json.decodeFromString<Metadata>(it)
} ?: return@forEach

val galleryID = metadata.galleryBlock?.id ?: metadata.galleryInfo?.id?.toIntOrNull() ?: return@forEach
Expand Down
63 changes: 38 additions & 25 deletions app/src/main/java/xyz/quaver/pupil/util/downloader/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ package xyz.quaver.pupil.util.downloader
import android.content.Context
import android.content.ContextWrapper
import android.net.Uri
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
Expand All @@ -35,35 +32,50 @@ import okhttp3.Request
import xyz.quaver.io.FileX
import xyz.quaver.io.util.*
import xyz.quaver.pupil.client
import xyz.quaver.pupil.hitomi.GalleryBlock
import xyz.quaver.pupil.hitomi.GalleryInfo
import xyz.quaver.pupil.hitomi.getGalleryBlock
import xyz.quaver.pupil.hitomi.getGalleryInfo
import xyz.quaver.pupil.hitomi.*
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.util.concurrent.ConcurrentHashMap

@Serializable
data class OldGalleryBlock(
val code: String,
val id: Int,
val galleryUrl: String,
val thumbnails: List<String>,
val title: String,
val artists: List<String>,
val series: List<String>,
val type: String,
val language: String,
val relatedTags: List<String>
data class OldGalleryInfo(
val language_localname: String? = null,
val language: String? = null,
val date: String? = null,
val files: List<OldGalleryFiles>,
val id: Int? = null,
val type: String? = null,
val title: String? = null
)

@Serializable
data class OldGalleryFiles(
val width: Int,
val hash: String,
val haswebp: Int = 0,
val name: String,
val height: Int,
val hasavif: Int = 0,
val hasavifsmalltn: Int? = 0
)

@Serializable
data class OldMetadata(
var galleryBlock: GalleryBlock? = null,
var reader: OldGalleryInfo? = null,
var imageList: MutableList<String?>? = null
) {
fun copy(): OldMetadata = OldMetadata(galleryBlock, reader, imageList?.let { MutableList(it.size) { i -> it[i] } })
}

@Serializable
data class Metadata(
var galleryBlock: GalleryBlock? = null,
var galleryInfo: GalleryInfo? = null,
var imageList: MutableList<String?>? = null
) {
constructor(old: OldMetadata) : this(old.galleryBlock, getGalleryInfo(old.galleryBlock?.id ?: throw Exception()), old.imageList)
fun copy(): Metadata = Metadata(galleryBlock, galleryInfo, imageList?.let { MutableList(it.size) { i -> it[i] } })
}

Expand All @@ -90,9 +102,13 @@ class Cache private constructor(context: Context, val galleryID: Int) : ContextW

var metadata = kotlin.runCatching {
findFile(".metadata")?.readText()?.let { metadata ->
Json.decodeFromString<Metadata>(metadata)
kotlin.runCatching {
json.decodeFromString<Metadata>(metadata)
}.getOrElse {
Metadata(json.decodeFromString<OldMetadata>(metadata))
}
}
}.getOrNull() ?: Metadata()
}.onFailure { it.printStackTrace() }.getOrNull() ?: Metadata()

val downloadFolder: FileX?
get() = DownloadManager.getInstance(this).getDownloadFolder(galleryID)
Expand Down Expand Up @@ -179,14 +195,11 @@ class Cache private constructor(context: Context, val galleryID: Int) : ContextW
metadata.imageList?.getOrNull(index)?.let { findFile(it) }

@Suppress("BlockingMethodInNonBlockingContext")
fun putImage(index: Int, fileName: String, data: InputStream) {
suspend fun putImage(index: Int, fileName: String, data: ByteArray) = coroutineScope {
val file = cacheFolder.getChild(fileName)

if (!file.exists())
file.createNewFile()
file.outputStream()?.use {
data.copyTo(it)
}
setMetadata { metadata -> metadata.imageList!![index] = fileName }
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/xyz/quaver/pupil/util/misc.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ fun OkHttpClient.Builder.proxyInfo(proxyInfo: ProxyInfo) = this.apply {
val formatMap = mapOf<String, GalleryBlock.() -> (String)>(
"-id-" to { id.toString() },
"-title-" to { title },
"-artist-" to { artists.joinToString() },
"-group-" to { groups.joinToString() }
"-artist-" to { if (artists.isNotEmpty()) artists.joinToString() else "N/A" },
"-group-" to { if (groups.isNotEmpty()) groups.joinToString() else "N/A" }
// TODO
)
/**
Expand Down

0 comments on commit eb98424

Please sign in to comment.