Skip to content

Commit

Permalink
notify user of slow compression methods
Browse files Browse the repository at this point in the history
  • Loading branch information
abdallahmehiz committed Jan 22, 2024
1 parent 9bfb349 commit 463e6f8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import eu.kanade.tachiyomi.data.download.DownloadProvider
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import org.apache.commons.compress.archivers.dump.UnsupportedCompressionAlgorithmException
import tachiyomi.core.i18n.stringResource
import tachiyomi.core.storage.UniFileTempFileManager
import tachiyomi.core.util.lang.launchUI
import tachiyomi.core.util.lang.withIOContext
import tachiyomi.core.util.system.logcat
import tachiyomi.domain.manga.model.Manga
Expand Down Expand Up @@ -77,6 +81,7 @@ class ChapterLoader(
/**
* Returns the page loader to use for this [chapter].
*/
@OptIn(DelicateCoroutinesApi::class)
private fun getPageLoader(chapter: ReaderChapter): PageLoader {
val dbChapter = chapter.chapter
val isDownloaded = downloadManager.isChapterDownloaded(
Expand All @@ -101,8 +106,13 @@ class ChapterLoader(
is Format.Zip -> ZipPageLoader(tempFileManager.createTempFile(format.file))
is Format.SevenZip -> try {
SevenZipPageLoader(tempFileManager.createTempFile(format.file))
{
GlobalScope.launchUI{
context.toast(context.stringResource(MR.strings.loader_7zip_slow_archives, it))
}
}
} catch (e: UnsupportedCompressionAlgorithmException) {
error(context.stringResource(MR.strings.loader_ppmd_error))
error(context.stringResource(MR.strings.loader_7zip_ppmd_error))
}
is Format.Rar -> try {
RarPageLoader(tempFileManager.createTempFile(format.file))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ import java.io.File
/**
* Loader used to load a chapter from a .7z or .cb7 file.
*/
internal class SevenZipPageLoader(file: File) : PageLoader() {
internal class SevenZipPageLoader(
private val file: File,
private val notifySlowArchive: (method: String) -> Unit
) : PageLoader() {

private val zip by lazy { SevenZFile(file) }

override var isLocal: Boolean = true

override suspend fun getPages(): List<ReaderPage> {
return zip.getImages()
return zip.getImages(notifySlowArchive)
.mapIndexed { i, entry ->
ReaderPage(i).apply {
stream = { entry.copyOf().inputStream() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,31 @@ package eu.kanade.tachiyomi.util.storage
import eu.kanade.tachiyomi.util.lang.compareToCaseInsensitiveNaturalOrder
import org.apache.commons.compress.archivers.dump.UnsupportedCompressionAlgorithmException
import org.apache.commons.compress.archivers.sevenz.SevenZFile
import org.apache.commons.compress.archivers.sevenz.SevenZMethod
import tachiyomi.core.util.system.ImageUtil
import java.io.IOException
import java.io.InputStream

object SevenZUtil {
fun SevenZFile.getImages(): Sequence<ByteArray> {
private val GoodMethods = arrayOf(
SevenZMethod.LZMA2,
SevenZMethod.DEFLATE,
SevenZMethod.COPY,
)

fun SevenZFile.getImages(notifySlowArchives: (method: String) -> Unit): Sequence<ByteArray> {
return generateSequence {
try {
getNextEntry()
} catch (e: IOException) {
throw UnsupportedCompressionAlgorithmException()
}
}.filter { !it.isDirectory && ImageUtil.isImage(it.name) { getInputStream(it) } }
.onEachIndexed { i, it ->
if (i > 0) return@onEachIndexed
val method = it.contentMethods.first().method
if(method !in GoodMethods) notifySlowArchives(method.name)
}
.sortedWith { f1, f2 -> f1.name.compareToCaseInsensitiveNaturalOrder(f2.name) }
.map(::getInputStream)
.map { it.use(InputStream::readBytes) } // ByteArray
Expand Down
3 changes: 2 additions & 1 deletion i18n/src/commonMain/resources/MR/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@
<string name="page_list_empty_error">No pages found</string>
<string name="loader_not_implemented_error">Source not found</string>
<string name="loader_rar5_error">RARv5 format is not supported</string>
<string name="loader_ppmd_error">PPMd compression is not supported</string>
<string name="loader_7zip_ppmd_error">PPMd compression is not supported</string>
<string name="loader_7zip_slow_archives">%s Archive, expect slow loading times.</string>

<!-- Updates -->
<string name="updating_library">Updating library</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ actual class LocalSource(
}
is Format.SevenZip -> {
SevenZFile(tempFileManager.createTempFile(format.file)).use { archive ->
val entry = archive.getImages().firstOrNull()
val entry = archive.getImages {} .firstOrNull()

entry?.let { coverManager.update(manga, it.inputStream()) }
}
Expand Down

0 comments on commit 463e6f8

Please sign in to comment.