Fix downloading chapters with the same names

This commit is contained in:
Koitharu
2023-05-01 12:52:35 +03:00
parent 3fd6bec433
commit 78f417ebe1
3 changed files with 21 additions and 5 deletions

View File

@@ -84,7 +84,7 @@ class MangaIndex(source: String?) {
fun getCoverEntry(): String? = json.getStringOrNull("cover_entry")
fun addChapter(chapter: MangaChapter) {
fun addChapter(chapter: MangaChapter, filename: String?) {
val chapters = json.getJSONObject("chapters")
if (!chapters.has(chapter.id.toString())) {
val jo = JSONObject()
@@ -95,6 +95,7 @@ class MangaIndex(source: String?) {
jo.put("scanlator", chapter.scanlator)
jo.put("branch", chapter.branch)
jo.put("entries", "%08d_%03d\\d{3}".format(chapter.branch.hashCode(), chapter.number))
jo.put("file", filename)
chapters.put(chapter.id.toString(), jo)
}
}
@@ -103,6 +104,10 @@ class MangaIndex(source: String?) {
return json.getJSONObject("chapters").remove(id.toString()) != null
}
fun getChapterFileName(chapterId: Long): String? {
return json.optJSONObject("chapters")?.optJSONObject(chapterId.toString())?.getStringOrNull("file")
}
fun setCoverEntry(name: String) {
json.put("cover_entry", name)
}

View File

@@ -54,7 +54,7 @@ class LocalMangaDirOutput(
runInterruptible(Dispatchers.IO) {
output.put(name, file)
}
index.addChapter(chapter)
index.addChapter(chapter, chapterFileName(chapter))
}
override suspend fun flushChapter(chapter: MangaChapter): Boolean {
@@ -105,7 +105,18 @@ class LocalMangaDirOutput(
}
private fun chapterFileName(chapter: MangaChapter): String {
return "${chapter.number}_${chapter.name.toFileNameSafe()}".take(18) + ".cbz"
index.getChapterFileName(chapter.id)?.let {
return it
}
val baseName = "${chapter.number}_${chapter.name.toFileNameSafe()}".take(18)
var i = 0
while (true) {
val name = (if (i == 0) baseName else baseName + "_$i") + ".cbz"
if (!File(rootFile, name).exists()) {
return name
}
i++
}
}
private suspend fun flushIndex() = runInterruptible(Dispatchers.IO) {

View File

@@ -57,7 +57,7 @@ class LocalMangaZipOutput(
runInterruptible(Dispatchers.IO) {
output.put(name, file)
}
index.addChapter(chapter)
index.addChapter(chapter, null)
}
override suspend fun flushChapter(chapter: MangaChapter): Boolean = false
@@ -98,7 +98,7 @@ class LocalMangaZipOutput(
}
otherIndex?.getMangaInfo()?.chapters?.let { chapters ->
for (chapter in chapters) {
index.addChapter(chapter)
index.addChapter(chapter, null)
}
}
}