Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Migrate Downloaded Chapters #1725

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# prevent user settings outside of the project from propagating into this project
root = true

[*.{kt,kts}]
max_line_length = 120
indent_style = space
indent_size = 4
insert_final_newline = true
ij_kotlin_allow_trailing_comma = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package eu.kanade.tachiyomi.data.download

import android.content.Context
import android.net.Uri
import android.provider.DocumentsContract
import eu.kanade.tachiyomi.data.download.model.Download
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.model.Page
Expand All @@ -16,10 +18,12 @@ import kotlinx.coroutines.runBlocking
import logcat.LogPriority
import tachiyomi.core.common.i18n.stringResource
import tachiyomi.core.common.storage.extension
import tachiyomi.core.common.storage.nameWithoutExtension
import tachiyomi.core.common.util.lang.launchIO
import tachiyomi.core.common.util.system.ImageUtil
import tachiyomi.core.common.util.system.logcat
import tachiyomi.domain.category.interactor.GetCategories
import tachiyomi.domain.chapter.interactor.GetChaptersByMangaId
import tachiyomi.domain.chapter.model.Chapter
import tachiyomi.domain.download.service.DownloadPreferences
import tachiyomi.domain.manga.model.Manga
Expand All @@ -40,6 +44,7 @@ class DownloadManager(
private val getCategories: GetCategories = Injekt.get(),
private val sourceManager: SourceManager = Injekt.get(),
private val downloadPreferences: DownloadPreferences = Injekt.get(),
private val getChaptersByMangaId: GetChaptersByMangaId = Injekt.get(),
) {

/**
Expand Down Expand Up @@ -239,6 +244,69 @@ class DownloadManager(
}
}

/**
* Migrates manga from one source to another.
*
* @param oldManga the origin manga for migration..
* @param oldSource the origin source for migration..
* @param newManga the target manga for migration.
* @param newSource the target source for migration.
*/
fun migrateManga(oldManga: Manga, oldSource: Source, newManga: Manga, newSource: Source) {
launchIO {
val oldMangaDir = provider.findMangaDir(oldManga.title, oldSource) ?: return@launchIO
val newMangaDir = provider.getMangaDir(newManga.title, newSource)
val downloadedChapters = oldMangaDir.listFiles()
val downloadedChaptersMap =
downloadedChapters?.associateBy({ it.nameWithoutExtension }, { it }) ?: return@launchIO

// map old chapters to new chapters. The names may not be the same.
val oldChapters = getChaptersByMangaId.await(oldManga.id)
val newChapters = getChaptersByMangaId.await(newManga.id)
val oldChaptersByChapterNumber = oldChapters.associateBy { it.chapterNumber }

// If the chapter is downloaded, migrate it.
for (chapter in newChapters) {
val oldChapter = oldChaptersByChapterNumber[chapter.chapterNumber]
if (oldChapter != null) {
val oldChapterName = provider.getChapterDirName(oldChapter.name, oldChapter.scanlator)
val oldChapterFile = downloadedChaptersMap[oldChapterName]
if (oldChapterFile != null) {
val newChapterName = if (oldChapterFile.extension == null) {
provider.getChapterDirName(chapter.name, chapter.scanlator)
} else {
provider.getChapterDirName(chapter.name, chapter.scanlator) + "." + oldChapterFile.extension
}

// move the file to newMangaDir, renaming the file/folder in the process
var chapterUri: Uri? = oldChapterFile.uri

// Rename the file.
chapterUri = chapterUri?.let {
DocumentsContract.renameDocument(
context.contentResolver,
it, newChapterName,
)
}

// Move the file to the new source folder.
chapterUri = chapterUri?.let {
DocumentsContract.moveDocument(
context.contentResolver,
it, oldMangaDir.uri, newMangaDir.uri,
)
}

// Can save chapterUri somewhere here if needed.

cache.removeChapter(oldChapter, oldManga)
cache.addChapter(newChapterName, newMangaDir, newManga)
}
}
}
}
}

/**
* Deletes the directory of a downloaded manga.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,33 @@ object MigrationFlags {
private const val CATEGORIES = 0b00010
private const val CUSTOM_COVER = 0b01000
private const val DELETE_DOWNLOADED = 0b10000
private const val MIGRATE_DOWNLOADED = 0b100000

private val coverCache: CoverCache by injectLazy()
private val downloadCache: DownloadCache by injectLazy()

fun hasChapters(value: Int): Boolean {
return value and CHAPTERS != 0
return isFlagSet(value, CHAPTERS)
}

fun hasCategories(value: Int): Boolean {
return value and CATEGORIES != 0
return isFlagSet(value, CATEGORIES)
}

fun hasCustomCover(value: Int): Boolean {
return value and CUSTOM_COVER != 0
return isFlagSet(value, CUSTOM_COVER)
}

fun hasMigrateDownloaded(value: Int): Boolean {
return isFlagSet(value, MIGRATE_DOWNLOADED)
}

fun hasDeleteDownloaded(value: Int): Boolean {
return value and DELETE_DOWNLOADED != 0
return isFlagSet(value, DELETE_DOWNLOADED)
}

private fun isFlagSet(value: Int, flag: Int): Boolean {
return (value and flag) == flag
}

/** Returns information about applicable flags with default selections. */
Expand All @@ -61,6 +70,7 @@ object MigrationFlags {
flags += MigrationFlag.create(CUSTOM_COVER, defaultSelectedBitMap, MR.strings.custom_cover)
}
if (downloadCache.getDownloadCount(manga) > 0) {
flags += MigrationFlag.create(MIGRATE_DOWNLOADED, defaultSelectedBitMap, MR.strings.migrate_downloaded)
flags += MigrationFlag.create(DELETE_DOWNLOADED, defaultSelectedBitMap, MR.strings.delete_downloaded)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ internal class MigrateDialogScreenModel(
val migrateChapters = MigrationFlags.hasChapters(flags)
val migrateCategories = MigrationFlags.hasCategories(flags)
val migrateCustomCover = MigrationFlags.hasCustomCover(flags)
val migrateDownloaded = MigrationFlags.hasMigrateDownloaded(flags)
val deleteDownloaded = MigrationFlags.hasDeleteDownloaded(flags)

try {
Expand Down Expand Up @@ -276,6 +277,13 @@ internal class MigrateDialogScreenModel(
.takeIf { it.isNotEmpty() }
?.let { insertTrack.awaitAll(it) }

// Migrate downloaded chapters
if (migrateDownloaded) {
if (oldSource != null) {
downloadManager.migrateManga(oldManga, oldSource, newManga, newSource)
}
}

// Delete downloaded
if (deleteDownloaded) {
if (oldSource != null) {
Expand Down
1 change: 1 addition & 0 deletions i18n/src/commonMain/moko-resources/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<string name="manga">Library entries</string>
<string name="chapters">Chapters</string>
<string name="track">Tracking</string>
<string name="migrate_downloaded">Migrate downloaded</string>
<string name="delete_downloaded">Delete downloaded</string>
<string name="history">History</string>
<string name="scanlator">Scanlator</string>
Expand Down