Skip to content

Commit

Permalink
Merge pull request #5 from omar908/android-15-fix
Browse files Browse the repository at this point in the history
Android 15 fix
  • Loading branch information
omar908 authored Dec 9, 2024
2 parents 1a48a9a + 06a48f3 commit fa236a4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ plugins {

android {
namespace = "com.puchunguita.cbzconverter"
compileSdk = 34
compileSdk = 35

defaultConfig {
applicationId = "com.puchunguita.cbzconverter"
minSdk = 24
targetSdk = 34
targetSdk = 35
versionCode = 1
versionName = "1.0"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import com.itextpdf.kernel.pdf.PdfWriter
import com.itextpdf.layout.Document
import com.itextpdf.layout.element.Image
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.util.logging.Logger
import java.util.stream.Collectors
import java.util.stream.IntStream
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
import kotlin.math.ceil
import kotlin.streams.asStream
Expand Down Expand Up @@ -73,21 +72,21 @@ private fun applyEachFileAndCreatePdf(
fileUri.forEachIndexed { index, uri ->
val outputFileName = outputFileNames[index]

val inputStream = contextHelper.openInputStream(uri) ?: run {
subStepStatusAction("Could not copy CBZ file to cache: $outputFileName"); return@forEachIndexed
try {
val tempFile = copyCbzToCacheAndCloseInputStream(contextHelper, subStepStatusAction, uri)

createPdfEitherSingleOrMultiple(
tempFile = tempFile,
subStepStatusAction = subStepStatusAction,
overrideSortOrderToUseOffset = overrideSortOrderToUseOffset,
outputFileName = outputFileName,
outputDirectory = outputDirectory,
maxNumberOfPages = maxNumberOfPages,
outputFiles = outputFiles
)
} catch (ioException: IOException) {
return@forEachIndexed
}

val tempFile = copyCbzToCacheAndCloseInputStream(contextHelper, inputStream)

createPdfEitherSingleOrMultiple(
tempFile = tempFile,
subStepStatusAction = subStepStatusAction,
overrideSortOrderToUseOffset = overrideSortOrderToUseOffset,
outputFileName = outputFileName,
outputDirectory = outputDirectory,
maxNumberOfPages = maxNumberOfPages,
outputFiles = outputFiles
)
}
return outputFiles
}
Expand All @@ -105,14 +104,9 @@ private fun mergeFilesAndCreatePdf(
subStepStatusAction("Creating combined_temp.cbz in Cache")
val combinedTempFile = File(contextHelper.getCacheDir(), "combined_temp.cbz")

ZipOutputStream(FileOutputStream(combinedTempFile)).use { zipOutputStream ->
ZipOutputStream(combinedTempFile.outputStream()).use { zipOutputStream ->
fileUri.forEachIndexed() { index, uri ->
val inputStream = contextHelper.openInputStream(uri) ?: run {
subStepStatusAction("Could not copy CBZ file to cache: ${uri.path}")
return@forEachIndexed
}
addEntriesToZip(inputStream, zipOutputStream, outputFileNames[index], index, subStepStatusAction)
inputStream.close()
addEntriesToZip(zipOutputStream, outputFileNames[index], index, subStepStatusAction, contextHelper, uri)
}
}

Expand Down Expand Up @@ -150,26 +144,48 @@ private fun orderZipEntriesToList(
}

private fun addEntriesToZip(
inputStream: InputStream,
zipOutputStream: ZipOutputStream,
fileName: String,
index: Int,
subStepStatusAction: (String) -> Unit,
contextHelper: ContextHelper,
uri: Uri
) {
ZipInputStream(inputStream).use { zipInputStream ->
var zipEntry = zipInputStream.nextEntry
while (zipEntry != null) {
// Using index for ordering by name to continue functioning correctly.
// Adding padding to start, without it passing 10_ is between 0_ and 1_.
// filename is added as prefix to ensure unique naming per file, otherwise duplication error.
val formattedIndex = index.toString().padStart(4, '0')
val currentFileUniqueName = "${formattedIndex}_"+fileName+"_"+zipEntry.name
subStepStatusAction("Adding ZipEntry into combined_temp.cbz: $currentFileUniqueName")
zipOutputStream.putNextEntry(ZipEntry(currentFileUniqueName))
zipInputStream.copyTo(zipOutputStream)
zipOutputStream.closeEntry()
zipEntry = zipInputStream.nextEntry
try {
val tempFile = copyCbzToCacheAndCloseInputStream(contextHelper, subStepStatusAction, uri)
val zipFile = ZipFile(tempFile)
subStepStatusAction("Adding ${zipFile.size()} entries from $fileName")

zipFile.entries().asSequence().forEach { zipEntry ->
try {
// Using index for ordering by name to continue functioning correctly.
// Adding padding to start, without it passing 10_ is between 0_ and 1_.
// Padding length being 4, allows correct order when merging up to 9999 files.
// filename is added as prefix to ensure unique naming per file, otherwise duplication error.
val formattedIndex = index.toString().padStart(4, '0')
val currentFileUniqueName = "${formattedIndex}_${fileName}_${zipEntry.name}"
subStepStatusAction("Adding ZipEntry into combined_temp.cbz: $currentFileUniqueName")

// Add entry to the output ZIP
zipOutputStream.putNextEntry(ZipEntry(currentFileUniqueName))

// Stream the entry's data into the output stream
zipFile.getInputStream(zipEntry).copyTo(zipOutputStream)

// Close the current entry
zipOutputStream.closeEntry()

} catch (e: Exception) {
subStepStatusAction("Error processing file ${zipEntry.name}")
logger.warning("Exception message: ${e.message}")
logger.warning("Exception stacktrace: ${e.stackTrace.contentToString()}")
}
}

zipFile.close()
tempFile.delete()
} catch (ioException: IOException) {
return
}
}

Expand Down Expand Up @@ -221,8 +237,13 @@ fun createPdfEitherSingleOrMultiple(
return outputFiles
}

private fun copyCbzToCacheAndCloseInputStream(contextHelper: ContextHelper, inputStream: InputStream): File {
@Throws(IOException::class)
private fun copyCbzToCacheAndCloseInputStream(contextHelper: ContextHelper, subStepStatusAction: (String) -> Unit, uri: Uri): File {
val tempFile = File(contextHelper.getCacheDir(), "temp.cbz")
val inputStream = contextHelper.openInputStream(uri) ?: run {
subStepStatusAction("Could not copy CBZ file to cache: ${uri.path}")
throw IOException("Could not copy CBZ file to cache: ${uri.path}")
}
tempFile.outputStream().use { outputStream ->
inputStream.copyTo(outputStream)
}
Expand Down

0 comments on commit fa236a4

Please sign in to comment.