Cleanup chapter name when syncing chapter with source

Also fix renaming chapters not working sometimes with archives

Co-Authored-By: AntsyLich <59261191+antsylich@users.noreply.github.com>
Co-Authored-By: Andreas <6576096+ghostbear@users.noreply.github.com>
This commit is contained in:
Jays2Kings 2023-10-25 14:13:38 -07:00
parent ab01f7a23e
commit 55184dce10
4 changed files with 64 additions and 7 deletions

View file

@ -371,16 +371,26 @@ class DownloadManager(val context: Context) {
* @param newChapter the target chapter with the new name.
*/
fun renameChapter(source: Source, manga: Manga, oldChapter: Chapter, newChapter: Chapter) {
val oldName = provider.getChapterDirName(oldChapter)
val newName = provider.getChapterDirName(newChapter)
val oldNames = provider.getValidChapterDirNames(oldChapter).map { listOf(it, "$it.cbz") }.flatten()
var newName = provider.getChapterDirName(newChapter)
val mangaDir = provider.getMangaDir(manga, source)
val oldFolder = mangaDir.findFile(oldName)
if (oldFolder?.renameTo(newName) == true) {
// Assume there's only 1 version of the chapter name formats present
val oldDownload = oldNames.asSequence()
.mapNotNull { mangaDir.findFile(it) }
.firstOrNull() ?: return
if (oldDownload.isFile && oldDownload.name?.endsWith(".cbz") == true) {
newName += ".cbz"
}
if (oldDownload.name == newName) return
if (oldDownload.renameTo(newName)) {
cache.removeChapters(listOf(oldChapter), manga)
cache.addChapter(newName, manga)
} else {
Timber.e("Could not rename downloaded chapter: %s.", oldName)
Timber.e("Could not rename downloaded chapter: ${oldNames.joinToString()}")
}
}

View file

@ -15,7 +15,6 @@ import okhttp3.Headers
import okhttp3.Interceptor
import okhttp3.Request
import okhttp3.Response
import timber.log.Timber
import java.util.Locale
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

View file

@ -0,0 +1,46 @@
package eu.kanade.tachiyomi.util.chapter
object ChapterSanitizer {
fun String.sanitize(title: String): String {
return trim()
.removePrefix(title)
.trim(*CHAPTER_TRIM_CHARS)
}
private val CHAPTER_TRIM_CHARS = arrayOf(
// Whitespace
' ',
'\u0009',
'\u000A',
'\u000B',
'\u000C',
'\u000D',
'\u0020',
'\u0085',
'\u00A0',
'\u1680',
'\u2000',
'\u2001',
'\u2002',
'\u2003',
'\u2004',
'\u2005',
'\u2006',
'\u2007',
'\u2008',
'\u2009',
'\u200A',
'\u2028',
'\u2029',
'\u202F',
'\u205F',
'\u3000',
// Separators
'-',
'_',
',',
':',
).toCharArray()
}

View file

@ -41,6 +41,7 @@ fun syncChaptersWithSource(
.mapIndexed { i, sChapter ->
Chapter.create().apply {
copyFrom(sChapter)
name = with(ChapterSanitizer) { sChapter.name.sanitize(manga.title) }
manga_id = manga.id
source_order = i
}
@ -178,7 +179,8 @@ fun syncChaptersWithSource(
// checks if the chapter in db needs updated
private fun shouldUpdateDbChapter(dbChapter: Chapter, sourceChapter: Chapter): Boolean {
return dbChapter.scanlator != sourceChapter.scanlator || dbChapter.name != sourceChapter.name ||
return dbChapter.scanlator != sourceChapter.scanlator ||
dbChapter.name != sourceChapter.name ||
dbChapter.date_upload != sourceChapter.date_upload ||
dbChapter.chapter_number != sourceChapter.chapter_number ||
dbChapter.source_order != sourceChapter.source_order