Fix cbz cover fetcher closing, update Kotlin and other small fixes

This commit is contained in:
Koitharu
2021-08-24 18:02:59 +03:00
parent a296c98602
commit 6596dca291
9 changed files with 57 additions and 21 deletions

View File

@@ -20,19 +20,22 @@ class CbzFetcher : Fetcher<Uri> {
pool: BitmapPool,
data: Uri,
size: Size,
options: Options
options: Options,
): FetchResult {
val zip = ZipFile(data.schemeSpecificPart)
val entry = zip.getEntry(data.fragment)
val ext = MimeTypeMap.getFileExtensionFromUrl(entry.name)
return SourceResult(
source = zip.getInputStream(entry).source().buffer(),
source = ExtraCloseableBufferedSource(
zip.getInputStream(entry).source().buffer(),
zip,
),
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext),
dataSource = DataSource.DISK
)
}
override fun key(data: Uri): String? = data.toString()
override fun key(data: Uri) = data.toString()
override fun handles(data: Uri) = data.scheme == "cbz"
}

View File

@@ -7,7 +7,7 @@ import java.util.*
class CbzFilter : FilenameFilter {
override fun accept(dir: File, name: String): Boolean {
val ext = name.substringAfterLast('.', "").toLowerCase(Locale.ROOT)
val ext = name.substringAfterLast('.', "").lowercase(Locale.ROOT)
return ext == "cbz" || ext == "zip"
}
}

View File

@@ -0,0 +1,18 @@
package org.koitharu.kotatsu.local.data
import okhttp3.internal.closeQuietly
import okio.BufferedSource
import okio.Closeable
class ExtraCloseableBufferedSource(
private val delegate: BufferedSource,
vararg closeable: Closeable,
) : BufferedSource by delegate {
private val extraCloseable = closeable
override fun close() {
delegate.close()
extraCloseable.forEach { x -> x.closeQuietly() }
}
}

View File

@@ -15,10 +15,7 @@ import org.koitharu.kotatsu.local.data.CbzFilter
import org.koitharu.kotatsu.local.data.MangaIndex
import org.koitharu.kotatsu.local.data.MangaZip
import org.koitharu.kotatsu.utils.AlphanumComparator
import org.koitharu.kotatsu.utils.ext.longHashCode
import org.koitharu.kotatsu.utils.ext.readText
import org.koitharu.kotatsu.utils.ext.sub
import org.koitharu.kotatsu.utils.ext.toCamelCase
import org.koitharu.kotatsu.utils.ext.*
import java.io.File
import java.util.*
import java.util.zip.ZipEntry
@@ -78,9 +75,9 @@ class LocalMangaRepository(private val context: Context) : MangaRepository {
}
}
fun delete(manga: Manga): Boolean {
suspend fun delete(manga: Manga): Boolean {
val file = Uri.parse(manga.url).toFile()
return file.delete()
return file.deleteAwait()
}
@SuppressLint("DefaultLocale")