mirror of
https://github.com/mihonapp/mihon.git
synced 2025-06-25 18:47:51 +02:00
Don't copy archives to temp files when opening (#326)
This commit is contained in:
@ -32,6 +32,7 @@ dependencies {
|
||||
implementation(libs.image.decoder)
|
||||
|
||||
implementation(libs.unifile)
|
||||
implementation(libs.bundles.archive)
|
||||
|
||||
api(kotlinx.coroutines.core)
|
||||
api(kotlinx.serialization.json)
|
||||
|
@ -1,22 +1,23 @@
|
||||
package eu.kanade.tachiyomi.util.storage
|
||||
|
||||
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
|
||||
import org.apache.commons.compress.archivers.zip.ZipFile
|
||||
import org.jsoup.Jsoup
|
||||
import org.jsoup.nodes.Document
|
||||
import java.io.Closeable
|
||||
import java.io.File
|
||||
import java.io.InputStream
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipFile
|
||||
import java.nio.channels.SeekableByteChannel
|
||||
|
||||
/**
|
||||
* Wrapper over ZipFile to load files in epub format.
|
||||
*/
|
||||
class EpubFile(file: File) : Closeable {
|
||||
class EpubFile(channel: SeekableByteChannel) : Closeable {
|
||||
|
||||
/**
|
||||
* Zip file of this epub.
|
||||
*/
|
||||
private val zip = ZipFile(file)
|
||||
private val zip = ZipFile(channel)
|
||||
|
||||
/**
|
||||
* Path separator used by this epub.
|
||||
@ -33,14 +34,14 @@ class EpubFile(file: File) : Closeable {
|
||||
/**
|
||||
* Returns an input stream for reading the contents of the specified zip file entry.
|
||||
*/
|
||||
fun getInputStream(entry: ZipEntry): InputStream {
|
||||
fun getInputStream(entry: ZipArchiveEntry): InputStream {
|
||||
return zip.getInputStream(entry)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the zip file entry for the specified name, or null if not found.
|
||||
*/
|
||||
fun getEntry(name: String): ZipEntry? {
|
||||
fun getEntry(name: String): ZipArchiveEntry? {
|
||||
return zip.getEntry(name)
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,9 @@
|
||||
package tachiyomi.core.common.storage
|
||||
|
||||
import android.content.Context
|
||||
import android.os.ParcelFileDescriptor
|
||||
import com.hippo.unifile.UniFile
|
||||
import java.nio.channels.FileChannel
|
||||
|
||||
val UniFile.extension: String?
|
||||
get() = name?.substringAfterLast('.')
|
||||
@ -10,3 +13,7 @@ val UniFile.nameWithoutExtension: String?
|
||||
|
||||
val UniFile.displayablePath: String
|
||||
get() = filePath ?: uri.toString()
|
||||
|
||||
fun UniFile.openReadOnlyChannel(context: Context): FileChannel {
|
||||
return ParcelFileDescriptor.AutoCloseInputStream(context.contentResolver.openFileDescriptor(uri, "r")).channel
|
||||
}
|
||||
|
@ -1,46 +0,0 @@
|
||||
package tachiyomi.core.common.storage
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build
|
||||
import android.os.FileUtils
|
||||
import com.hippo.unifile.UniFile
|
||||
import java.io.BufferedOutputStream
|
||||
import java.io.File
|
||||
|
||||
class UniFileTempFileManager(
|
||||
private val context: Context,
|
||||
) {
|
||||
|
||||
private val dir = File(context.externalCacheDir, "tmp")
|
||||
|
||||
fun createTempFile(file: UniFile): File {
|
||||
dir.mkdirs()
|
||||
|
||||
val inputStream = context.contentResolver.openInputStream(file.uri)!!
|
||||
val tempFile = File.createTempFile(
|
||||
file.nameWithoutExtension.orEmpty().padEnd(3), // Prefix must be 3+ chars
|
||||
null,
|
||||
dir,
|
||||
)
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
FileUtils.copy(inputStream, tempFile.outputStream())
|
||||
} else {
|
||||
BufferedOutputStream(tempFile.outputStream()).use { tmpOut ->
|
||||
inputStream.use { input ->
|
||||
val buffer = ByteArray(8192)
|
||||
var count: Int
|
||||
while (input.read(buffer).also { count = it } > 0) {
|
||||
tmpOut.write(buffer, 0, count)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tempFile
|
||||
}
|
||||
|
||||
fun deleteTempFiles() {
|
||||
dir.deleteRecursively()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user