LocalMangaUtil
This commit is contained in:
@@ -132,6 +132,20 @@ class MangaIndex(source: String?) {
|
|||||||
json.put("chapters", newJo)
|
json.put("chapters", newJo)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun clear() {
|
||||||
|
val keys = json.keys()
|
||||||
|
while (keys.hasNext()) {
|
||||||
|
json.remove(keys.next())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun setFrom(other: MangaIndex) {
|
||||||
|
clear()
|
||||||
|
other.json.keys().forEach { key ->
|
||||||
|
json.putOpt(key, other.json.opt(key))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun getChapters(json: JSONObject, source: MangaSource): List<MangaChapter> {
|
private fun getChapters(json: JSONObject, source: MangaSource): List<MangaChapter> {
|
||||||
val chapters = ArrayList<MangaChapter>(json.length())
|
val chapters = ArrayList<MangaChapter>(json.length())
|
||||||
for (k in json.keys()) {
|
for (k in json.keys()) {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import org.koitharu.kotatsu.parsers.model.MangaPage
|
|||||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
abstract class LocalMangaInput(
|
sealed class LocalMangaInput(
|
||||||
protected val root: File,
|
protected val root: File,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
|||||||
@@ -92,6 +92,10 @@ class LocalMangaDirOutput(
|
|||||||
index.removeChapter(chapterId)
|
index.removeChapter(chapterId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setIndex(newIndex: MangaIndex) {
|
||||||
|
index.setFrom(newIndex)
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun ZipOutput.flushAndFinish() = runInterruptible(Dispatchers.IO) {
|
private suspend fun ZipOutput.flushAndFinish() = runInterruptible(Dispatchers.IO) {
|
||||||
finish()
|
finish()
|
||||||
close()
|
close()
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import org.koitharu.kotatsu.parsers.model.MangaChapter
|
|||||||
import org.koitharu.kotatsu.parsers.util.toFileNameSafe
|
import org.koitharu.kotatsu.parsers.util.toFileNameSafe
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
abstract class LocalMangaOutput(
|
sealed class LocalMangaOutput(
|
||||||
val rootFile: File,
|
val rootFile: File,
|
||||||
) : Closeable {
|
) : Closeable {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package org.koitharu.kotatsu.local.data.output
|
||||||
|
|
||||||
|
import androidx.core.net.toFile
|
||||||
|
import androidx.core.net.toUri
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.runInterruptible
|
||||||
|
import org.koitharu.kotatsu.local.data.MangaIndex
|
||||||
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||||
|
|
||||||
|
class LocalMangaUtil(
|
||||||
|
private val manga: Manga,
|
||||||
|
) {
|
||||||
|
|
||||||
|
init {
|
||||||
|
require(manga.source == MangaSource.LOCAL) {
|
||||||
|
"Expected LOCAL source but ${manga.source} found"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun deleteChapters(ids: Set<Long>) {
|
||||||
|
newOutput().use { output ->
|
||||||
|
when (output) {
|
||||||
|
is LocalMangaZipOutput -> runInterruptible(Dispatchers.IO) {
|
||||||
|
LocalMangaZipOutput.filterChapters(output, ids)
|
||||||
|
}
|
||||||
|
|
||||||
|
is LocalMangaDirOutput -> {
|
||||||
|
for (id in ids) {
|
||||||
|
output.deleteChapter(id)
|
||||||
|
}
|
||||||
|
output.finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun writeIndex(index: MangaIndex) {
|
||||||
|
newOutput().use { output ->
|
||||||
|
when (output) {
|
||||||
|
is LocalMangaDirOutput -> {
|
||||||
|
TODO()
|
||||||
|
}
|
||||||
|
|
||||||
|
is LocalMangaZipOutput -> TODO()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun newOutput(): LocalMangaOutput = runInterruptible(Dispatchers.IO) {
|
||||||
|
val file = manga.url.toUri().toFile()
|
||||||
|
if (file.isDirectory) {
|
||||||
|
LocalMangaDirOutput(file, manga)
|
||||||
|
} else {
|
||||||
|
LocalMangaZipOutput(file, manga)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,8 +14,7 @@ import org.koitharu.kotatsu.local.data.LocalManga
|
|||||||
import org.koitharu.kotatsu.local.data.LocalStorageManager
|
import org.koitharu.kotatsu.local.data.LocalStorageManager
|
||||||
import org.koitharu.kotatsu.local.data.TempFileFilter
|
import org.koitharu.kotatsu.local.data.TempFileFilter
|
||||||
import org.koitharu.kotatsu.local.data.input.LocalMangaInput
|
import org.koitharu.kotatsu.local.data.input.LocalMangaInput
|
||||||
import org.koitharu.kotatsu.local.data.output.LocalMangaDirOutput
|
import org.koitharu.kotatsu.local.data.output.LocalMangaUtil
|
||||||
import org.koitharu.kotatsu.local.data.output.LocalMangaZipOutput
|
|
||||||
import org.koitharu.kotatsu.parsers.model.Manga
|
import org.koitharu.kotatsu.parsers.model.Manga
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
import org.koitharu.kotatsu.parsers.model.MangaChapter
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaPage
|
import org.koitharu.kotatsu.parsers.model.MangaPage
|
||||||
@@ -90,21 +89,7 @@ class LocalMangaRepository @Inject constructor(private val storageManager: Local
|
|||||||
suspend fun deleteChapters(manga: Manga, ids: Set<Long>) {
|
suspend fun deleteChapters(manga: Manga, ids: Set<Long>) {
|
||||||
lockManga(manga.id)
|
lockManga(manga.id)
|
||||||
try {
|
try {
|
||||||
val uri = Uri.parse(manga.url)
|
LocalMangaUtil(manga).deleteChapters(ids)
|
||||||
val file = uri.toFile()
|
|
||||||
if (file.isDirectory) {
|
|
||||||
LocalMangaDirOutput(file, manga).use { output ->
|
|
||||||
for (id in ids) {
|
|
||||||
output.deleteChapter(id)
|
|
||||||
}
|
|
||||||
output.finish()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
runInterruptible(Dispatchers.IO) {
|
|
||||||
val cbz = LocalMangaZipOutput(file, manga)
|
|
||||||
LocalMangaZipOutput.filterChapters(cbz, ids)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
unlockManga(manga.id)
|
unlockManga(manga.id)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user