mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 02:34:39 +00:00
refactor: Move archive related code to core.archive module
This commit is contained in:
parent
54a3059730
commit
b4377a4609
19 changed files with 61 additions and 55 deletions
15
core/archive/build.gradle.kts
Normal file
15
core/archive/build.gradle.kts
Normal file
|
@ -0,0 +1,15 @@
|
|||
plugins {
|
||||
id("yokai.android.library")
|
||||
kotlin("android")
|
||||
alias(kotlinx.plugins.serialization)
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "yokai.core.archive"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(libs.jsoup)
|
||||
implementation(libs.libarchive)
|
||||
implementation(libs.unifile)
|
||||
}
|
2
core/archive/src/main/AndroidManifest.xml
Normal file
2
core/archive/src/main/AndroidManifest.xml
Normal file
|
@ -0,0 +1,2 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest />
|
|
@ -1,12 +1,13 @@
|
|||
package yokai.core.archive
|
||||
|
||||
import java.io.InputStream
|
||||
import java.nio.ByteBuffer
|
||||
import kotlin.concurrent.Volatile
|
||||
import me.zhanghai.android.libarchive.Archive
|
||||
import me.zhanghai.android.libarchive.ArchiveEntry
|
||||
import me.zhanghai.android.libarchive.ArchiveException
|
||||
|
||||
class AndroidArchiveInputStream(buffer: Long, size: Long) : ArchiveInputStream() {
|
||||
class ArchiveInputStream(buffer: Long, size: Long) : InputStream() {
|
||||
private val lock = Any()
|
||||
@Volatile
|
||||
private var isClosed = false
|
|
@ -1,23 +1,22 @@
|
|||
package yokai.core.archive
|
||||
|
||||
import android.content.Context
|
||||
import android.os.ParcelFileDescriptor
|
||||
import android.system.Os
|
||||
import android.system.OsConstants
|
||||
import com.hippo.unifile.UniFile
|
||||
import eu.kanade.tachiyomi.util.system.openFileDescriptor
|
||||
import java.io.Closeable
|
||||
import java.io.InputStream
|
||||
import me.zhanghai.android.libarchive.ArchiveException
|
||||
|
||||
class AndroidArchiveReader(pfd: ParcelFileDescriptor) : ArchiveReader {
|
||||
class ArchiveReader(pfd: ParcelFileDescriptor) : Closeable {
|
||||
val size = pfd.statSize
|
||||
val address = Os.mmap(0, size, OsConstants.PROT_READ, OsConstants.MAP_PRIVATE, pfd.fileDescriptor, 0)
|
||||
|
||||
override fun <T> useEntries(block: (Sequence<ArchiveEntry>) -> T): T =
|
||||
AndroidArchiveInputStream(address, size).use { block(generateSequence { it.getNextEntry() }) }
|
||||
fun <T> useEntries(block: (Sequence<ArchiveEntry>) -> T): T = ArchiveInputStream(address, size).use {
|
||||
block(generateSequence { it.getNextEntry() })
|
||||
}
|
||||
|
||||
override fun getInputStream(entryName: String): InputStream? {
|
||||
val archive = AndroidArchiveInputStream(address, size)
|
||||
fun getInputStream(entryName: String): InputStream? {
|
||||
val archive = ArchiveInputStream(address, size)
|
||||
try {
|
||||
while (true) {
|
||||
val entry = archive.getNextEntry() ?: break
|
||||
|
@ -37,6 +36,3 @@ class AndroidArchiveReader(pfd: ParcelFileDescriptor) : ArchiveReader {
|
|||
Os.munmap(address, size)
|
||||
}
|
||||
}
|
||||
|
||||
fun UniFile.archiveReader(context: Context): ArchiveReader =
|
||||
openFileDescriptor(context, "r").use { AndroidArchiveReader(it) }
|
|
@ -1,16 +1,15 @@
|
|||
package eu.kanade.tachiyomi.util.storage
|
||||
package yokai.core.archive
|
||||
|
||||
import java.io.Closeable
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.nodes.Document
|
||||
import yokai.core.archive.ArchiveReader
|
||||
|
||||
/**
|
||||
* Wrapper over ZipFile to load files in epub format.
|
||||
*/
|
||||
class EpubFile(private val reader: ArchiveReader) : Closeable by reader {
|
||||
class EpubReader(private val reader: ArchiveReader) : Closeable by reader {
|
||||
|
||||
/**
|
||||
* Path separator used by this epub.
|
|
@ -4,12 +4,12 @@ import android.content.Context
|
|||
import android.system.Os
|
||||
import android.system.StructStat
|
||||
import com.hippo.unifile.UniFile
|
||||
import eu.kanade.tachiyomi.util.system.openFileDescriptor
|
||||
import java.io.Closeable
|
||||
import java.nio.ByteBuffer
|
||||
import me.zhanghai.android.libarchive.Archive
|
||||
import me.zhanghai.android.libarchive.ArchiveEntry
|
||||
import me.zhanghai.android.libarchive.ArchiveException
|
||||
import yokai.core.archive.util.openFileDescriptor
|
||||
|
||||
class ZipWriter(val context: Context, file: UniFile) : Closeable {
|
||||
private val pfd = file.openFileDescriptor(context, "wt")
|
|
@ -0,0 +1,14 @@
|
|||
package yokai.core.archive.util
|
||||
|
||||
import android.content.Context
|
||||
import android.os.ParcelFileDescriptor
|
||||
import com.hippo.unifile.UniFile
|
||||
import yokai.core.archive.ArchiveReader
|
||||
import yokai.core.archive.EpubReader
|
||||
|
||||
fun UniFile.openFileDescriptor(context: Context, mode: String): ParcelFileDescriptor =
|
||||
context.contentResolver.openFileDescriptor(uri, mode) ?: error("Failed to open file descriptor: ${filePath ?: uri.toString()}")
|
||||
|
||||
fun UniFile.archiveReader(context: Context): ArchiveReader = openFileDescriptor(context, "r").use { ArchiveReader(it) }
|
||||
|
||||
fun UniFile.epubReader(context: Context): EpubReader = EpubReader(archiveReader(context))
|
|
@ -60,7 +60,7 @@ kotlin {
|
|||
}
|
||||
|
||||
android {
|
||||
namespace = "yokai.core"
|
||||
namespace = "yokai.core.main"
|
||||
}
|
||||
|
||||
tasks {
|
||||
|
|
|
@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.util.system
|
|||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.os.FileUtils
|
||||
import android.os.ParcelFileDescriptor
|
||||
import com.hippo.unifile.UniFile
|
||||
import java.io.BufferedOutputStream
|
||||
import java.io.File
|
||||
|
@ -48,6 +47,3 @@ fun UniFile.writeText(string: String, onComplete: () -> Unit = {}) {
|
|||
onComplete()
|
||||
}
|
||||
}
|
||||
|
||||
fun UniFile.openFileDescriptor(context: Context, mode: String): ParcelFileDescriptor =
|
||||
context.contentResolver.openFileDescriptor(uri, mode) ?: error("Failed to open file descriptor: $displayablePath")
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
package yokai.core.archive
|
||||
|
||||
import java.io.InputStream
|
||||
|
||||
// TODO: Use Okio's Source
|
||||
abstract class ArchiveInputStream : InputStream()
|
|
@ -1,9 +0,0 @@
|
|||
package yokai.core.archive
|
||||
|
||||
import java.io.Closeable
|
||||
import java.io.InputStream
|
||||
|
||||
interface ArchiveReader : Closeable {
|
||||
fun <T> useEntries(block: (Sequence<ArchiveEntry>) -> T): T
|
||||
fun getInputStream(entryName: String): InputStream?
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue