Remove storage permissions

Requires adjusting some file reading to first copy to a temporary file
in cache that we have permissions to read from. This is only applicable for things
like ZIP files where we need an actual File rather than just some Android content
URI shenanigans.
This commit is contained in:
arkon
2023-11-28 08:59:45 -05:00
parent e41668862f
commit 4fcdde4913
14 changed files with 51 additions and 60 deletions

View File

@@ -1,9 +1,7 @@
package eu.kanade.tachiyomi.util.storage
import com.hippo.unifile.UniFile
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import tachiyomi.core.storage.toFile
import java.io.Closeable
import java.io.File
import java.io.InputStream
@@ -13,12 +11,12 @@ import java.util.zip.ZipFile
/**
* Wrapper over ZipFile to load files in epub format.
*/
class EpubFile(file: UniFile) : Closeable {
class EpubFile(file: File) : Closeable {
/**
* Zip file of this epub.
*/
private val zip = ZipFile(file.toFile())
private val zip = ZipFile(file)
/**
* Path separator used by this epub.

View File

@@ -1,6 +1,10 @@
package tachiyomi.core.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
val UniFile.extension: String?
@@ -9,4 +13,26 @@ val UniFile.extension: String?
val UniFile.nameWithoutExtension: String?
get() = name?.substringBeforeLast('.')
fun UniFile.toFile(): File? = filePath?.let { File(it) }
fun UniFile.toTempFile(context: Context): File {
val inputStream = context.contentResolver.openInputStream(uri)!!
val tempFile = File.createTempFile(
nameWithoutExtension.orEmpty(),
null,
)
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
}