2017-01-29 20:48:55 +01:00
|
|
|
package eu.kanade.tachiyomi.source
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
2020-12-08 04:13:53 +01:00
|
|
|
import com.github.junrar.Archive
|
2022-05-25 00:02:02 +02:00
|
|
|
import com.hippo.unifile.UniFile
|
2017-01-29 20:48:55 +01:00
|
|
|
import eu.kanade.tachiyomi.R
|
2020-09-06 02:13:44 +02:00
|
|
|
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
2020-02-27 00:12:44 +01:00
|
|
|
import eu.kanade.tachiyomi.source.model.Filter
|
|
|
|
|
import eu.kanade.tachiyomi.source.model.FilterList
|
|
|
|
|
import eu.kanade.tachiyomi.source.model.MangasPage
|
|
|
|
|
import eu.kanade.tachiyomi.source.model.SChapter
|
|
|
|
|
import eu.kanade.tachiyomi.source.model.SManga
|
2021-11-19 22:42:19 +01:00
|
|
|
import eu.kanade.tachiyomi.source.model.toChapterInfo
|
|
|
|
|
import eu.kanade.tachiyomi.source.model.toMangaInfo
|
|
|
|
|
import eu.kanade.tachiyomi.source.model.toSChapter
|
|
|
|
|
import eu.kanade.tachiyomi.source.model.toSManga
|
2020-02-03 04:22:54 +01:00
|
|
|
import eu.kanade.tachiyomi.util.chapter.ChapterRecognition
|
|
|
|
|
import eu.kanade.tachiyomi.util.lang.compareToCaseInsensitiveNaturalOrder
|
2020-02-03 04:04:11 +01:00
|
|
|
import eu.kanade.tachiyomi.util.storage.DiskUtil
|
|
|
|
|
import eu.kanade.tachiyomi.util.storage.EpubFile
|
2020-02-03 04:22:54 +01:00
|
|
|
import eu.kanade.tachiyomi.util.system.ImageUtil
|
2022-07-23 17:14:34 +02:00
|
|
|
import eu.kanade.tachiyomi.util.system.logcat
|
2021-11-19 22:42:19 +01:00
|
|
|
import kotlinx.coroutines.runBlocking
|
2020-10-12 20:20:54 +02:00
|
|
|
import kotlinx.serialization.Serializable
|
|
|
|
|
import kotlinx.serialization.json.Json
|
2021-09-06 17:54:00 +02:00
|
|
|
import kotlinx.serialization.json.decodeFromStream
|
|
|
|
|
import kotlinx.serialization.json.encodeToStream
|
2022-07-23 17:14:34 +02:00
|
|
|
import logcat.LogPriority
|
2020-09-14 00:48:20 +02:00
|
|
|
import rx.Observable
|
2021-11-19 22:42:19 +01:00
|
|
|
import tachiyomi.source.model.ChapterInfo
|
|
|
|
|
import tachiyomi.source.model.MangaInfo
|
2021-09-06 17:54:00 +02:00
|
|
|
import uy.kohesive.injekt.injectLazy
|
2017-01-29 20:48:55 +01:00
|
|
|
import java.io.File
|
|
|
|
|
import java.io.FileInputStream
|
|
|
|
|
import java.io.InputStream
|
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
import java.util.zip.ZipFile
|
|
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
class LocalSource(
|
|
|
|
|
private val context: Context,
|
|
|
|
|
) : CatalogueSource, UnmeteredSource {
|
2017-01-29 20:48:55 +01:00
|
|
|
|
2021-09-06 17:54:00 +02:00
|
|
|
private val json: Json by injectLazy()
|
2021-09-11 17:48:32 +02:00
|
|
|
|
2021-09-06 17:54:00 +02:00
|
|
|
// SY -->
|
|
|
|
|
private val preferences: PreferencesHelper by injectLazy()
|
|
|
|
|
// SY <--
|
|
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
override val name: String = context.getString(R.string.local_source)
|
|
|
|
|
|
|
|
|
|
override val id: Long = ID
|
|
|
|
|
|
|
|
|
|
override val lang: String = "other"
|
2017-01-29 20:48:55 +01:00
|
|
|
|
2021-10-31 00:36:23 +02:00
|
|
|
override fun toString() = name
|
2017-01-29 20:48:55 +01:00
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
override val supportsLatest: Boolean = true
|
|
|
|
|
|
|
|
|
|
// Browse related
|
2017-01-29 20:48:55 +01:00
|
|
|
override fun fetchPopularManga(page: Int) = fetchSearchManga(page, "", POPULAR_FILTERS)
|
|
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
override fun fetchLatestUpdates(page: Int) = fetchSearchManga(page, "", LATEST_FILTERS)
|
|
|
|
|
|
2017-01-29 20:48:55 +01:00
|
|
|
override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable<MangasPage> {
|
2022-05-25 00:02:02 +02:00
|
|
|
val baseDirsFiles = getBaseDirectoriesFiles(context)
|
2020-09-06 02:13:44 +02:00
|
|
|
// SY -->
|
2021-09-06 17:54:00 +02:00
|
|
|
val allowLocalSourceHiddenFolders = preferences.allowLocalSourceHiddenFolders().get()
|
2020-09-06 02:13:44 +02:00
|
|
|
// SY <--
|
2017-01-29 20:48:55 +01:00
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
var mangaDirs = baseDirsFiles
|
|
|
|
|
// Filter out files that are hidden and is not a folder
|
|
|
|
|
.filter { it.isDirectory && /* SY --> */ (!it.name.startsWith('.') || allowLocalSourceHiddenFolders) /* SY <-- */ }
|
2020-04-25 20:24:45 +02:00
|
|
|
.distinctBy { it.name }
|
2017-01-29 20:48:55 +01:00
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
val lastModifiedLimit = if (filters === LATEST_FILTERS) System.currentTimeMillis() - LATEST_THRESHOLD else 0L
|
|
|
|
|
// Filter by query or last modified
|
|
|
|
|
mangaDirs = mangaDirs.filter {
|
|
|
|
|
if (lastModifiedLimit == 0L) {
|
|
|
|
|
it.name.contains(query, ignoreCase = true)
|
|
|
|
|
} else {
|
|
|
|
|
it.lastModified() >= lastModifiedLimit
|
2017-01-29 20:48:55 +01:00
|
|
|
}
|
2022-05-25 00:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
filters.forEach { filter ->
|
|
|
|
|
when (filter) {
|
|
|
|
|
is OrderBy -> {
|
|
|
|
|
when (filter.state!!.index) {
|
|
|
|
|
0 -> {
|
|
|
|
|
mangaDirs = if (filter.state!!.ascending) {
|
|
|
|
|
mangaDirs.sortedWith(compareBy(String.CASE_INSENSITIVE_ORDER) { it.name })
|
|
|
|
|
} else {
|
|
|
|
|
mangaDirs.sortedWith(compareByDescending(String.CASE_INSENSITIVE_ORDER) { it.name })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
1 -> {
|
|
|
|
|
mangaDirs = if (filter.state!!.ascending) {
|
|
|
|
|
mangaDirs.sortedBy(File::lastModified)
|
|
|
|
|
} else {
|
|
|
|
|
mangaDirs.sortedByDescending(File::lastModified)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-25 20:24:45 +02:00
|
|
|
}
|
2022-05-25 00:02:02 +02:00
|
|
|
|
2022-08-18 20:06:32 +02:00
|
|
|
else -> {
|
|
|
|
|
/* Do nothing */
|
|
|
|
|
}
|
2017-01-29 20:48:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
// Transform mangaDirs to list of SManga
|
2017-01-29 20:48:55 +01:00
|
|
|
val mangas = mangaDirs.map { mangaDir ->
|
|
|
|
|
SManga.create().apply {
|
|
|
|
|
title = mangaDir.name
|
|
|
|
|
url = mangaDir.name
|
|
|
|
|
|
|
|
|
|
// Try to find the cover
|
2022-05-25 00:02:02 +02:00
|
|
|
val cover = getCoverFile(mangaDir.name, baseDirsFiles)
|
|
|
|
|
if (cover != null && cover.exists()) {
|
|
|
|
|
thumbnail_url = cover.absolutePath
|
2017-01-29 20:48:55 +01:00
|
|
|
}
|
2022-05-25 00:02:02 +02:00
|
|
|
}
|
|
|
|
|
}
|
2017-01-29 20:48:55 +01:00
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
// Fetch chapters of all the manga
|
|
|
|
|
mangas.forEach { manga ->
|
|
|
|
|
val mangaInfo = manga.toMangaInfo()
|
|
|
|
|
runBlocking {
|
|
|
|
|
val chapters = getChapterList(mangaInfo)
|
|
|
|
|
if (chapters.isNotEmpty()) {
|
|
|
|
|
val chapter = chapters.last().toSChapter()
|
|
|
|
|
val format = getFormat(chapter)
|
2020-04-28 04:49:06 +02:00
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
if (format is Format.Epub) {
|
|
|
|
|
EpubFile(format.file).use { epub ->
|
|
|
|
|
epub.fillMangaMetadata(manga)
|
2017-01-29 20:48:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-05-25 00:02:02 +02:00
|
|
|
|
|
|
|
|
// Copy the cover from the first chapter found if not available
|
|
|
|
|
if (manga.thumbnail_url == null) {
|
|
|
|
|
updateCover(chapter, manga)
|
|
|
|
|
}
|
2017-01-29 20:48:55 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-25 20:20:47 +02:00
|
|
|
|
|
|
|
|
return Observable.just(MangasPage(mangas.toList(), false))
|
2017-01-29 20:48:55 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-13 01:21:29 +02:00
|
|
|
// SY -->
|
2020-07-11 20:53:59 +02:00
|
|
|
fun updateMangaInfo(manga: SManga) {
|
2021-02-24 22:15:19 +01:00
|
|
|
val directory = getBaseDirectories(context).map { File(it, manga.url) }.find {
|
2020-07-11 20:53:59 +02:00
|
|
|
it.exists()
|
|
|
|
|
} ?: return
|
|
|
|
|
val existingFileName = directory.listFiles()?.find { it.extension == "json" }?.name
|
|
|
|
|
val file = File(directory, existingFileName ?: "info.json")
|
2021-09-06 17:54:00 +02:00
|
|
|
file.outputStream().use {
|
|
|
|
|
json.encodeToStream(manga.toJson(), it)
|
|
|
|
|
}
|
2020-07-11 20:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-12 20:20:54 +02:00
|
|
|
private fun SManga.toJson(): MangaJson {
|
2021-02-24 22:15:19 +01:00
|
|
|
return MangaJson(title, author, artist, description, genre?.split(", "), status)
|
2020-07-11 20:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-10-12 20:20:54 +02:00
|
|
|
@Serializable
|
2020-07-11 20:53:59 +02:00
|
|
|
data class MangaJson(
|
2021-07-04 23:14:07 +02:00
|
|
|
val title: String? = null,
|
|
|
|
|
val author: String? = null,
|
|
|
|
|
val artist: String? = null,
|
|
|
|
|
val description: String? = null,
|
|
|
|
|
val genre: List<String>? = null,
|
2022-04-08 21:30:30 +02:00
|
|
|
val status: Int? = null,
|
2021-07-04 23:14:07 +02:00
|
|
|
)
|
2020-07-13 01:21:29 +02:00
|
|
|
// SY <--
|
2020-07-11 20:53:59 +02:00
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
// Manga details related
|
2021-11-19 22:42:19 +01:00
|
|
|
override suspend fun getMangaDetails(manga: MangaInfo): MangaInfo {
|
2022-05-25 00:02:02 +02:00
|
|
|
var mangaInfo = manga
|
|
|
|
|
|
|
|
|
|
val baseDirsFile = getBaseDirectoriesFiles(context)
|
|
|
|
|
|
|
|
|
|
val coverFile = getCoverFile(manga.key, baseDirsFile)
|
|
|
|
|
|
|
|
|
|
coverFile?.let {
|
|
|
|
|
mangaInfo = mangaInfo.copy(cover = it.absolutePath)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val localDetails = getMangaDirsFiles(manga.key, baseDirsFile)
|
2021-12-24 16:26:24 +01:00
|
|
|
.firstOrNull { it.extension.equals("json", ignoreCase = true) }
|
2020-07-25 20:20:47 +02:00
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
if (localDetails != null) {
|
2021-11-19 22:42:19 +01:00
|
|
|
val mangaJson = json.decodeFromStream<MangaJson>(localDetails.inputStream())
|
|
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
mangaInfo = mangaInfo.copy(
|
|
|
|
|
title = mangaJson.title ?: mangaInfo.title,
|
|
|
|
|
author = mangaJson.author ?: mangaInfo.author,
|
|
|
|
|
artist = mangaJson.artist ?: mangaInfo.artist,
|
|
|
|
|
description = mangaJson.description ?: mangaInfo.description,
|
|
|
|
|
genres = mangaJson.genre ?: mangaInfo.genres,
|
|
|
|
|
status = mangaJson.status ?: mangaInfo.status,
|
2021-11-19 22:42:19 +01:00
|
|
|
)
|
|
|
|
|
}
|
2022-05-25 00:02:02 +02:00
|
|
|
|
|
|
|
|
return mangaInfo
|
2020-01-11 21:59:43 +01:00
|
|
|
}
|
2017-02-05 12:01:58 +01:00
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
// Chapters
|
2021-11-19 22:42:19 +01:00
|
|
|
override suspend fun getChapterList(manga: MangaInfo): List<ChapterInfo> {
|
|
|
|
|
val sManga = manga.toSManga()
|
|
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
val baseDirsFile = getBaseDirectoriesFiles(context)
|
|
|
|
|
return getMangaDirsFiles(manga.key, baseDirsFile)
|
|
|
|
|
// Only keep supported formats
|
2020-04-25 20:24:45 +02:00
|
|
|
.filter { it.isDirectory || isSupportedFile(it.extension) }
|
|
|
|
|
.map { chapterFile ->
|
|
|
|
|
SChapter.create().apply {
|
2021-11-19 22:42:19 +01:00
|
|
|
url = "${manga.key}/${chapterFile.name}"
|
2020-04-28 04:49:06 +02:00
|
|
|
name = if (chapterFile.isDirectory) {
|
2020-04-25 20:24:45 +02:00
|
|
|
chapterFile.name
|
|
|
|
|
} else {
|
|
|
|
|
chapterFile.nameWithoutExtension
|
2017-02-05 12:01:58 +01:00
|
|
|
}
|
2020-04-25 20:24:45 +02:00
|
|
|
date_upload = chapterFile.lastModified()
|
2020-04-28 04:49:06 +02:00
|
|
|
|
2022-06-10 15:26:56 +02:00
|
|
|
chapter_number = ChapterRecognition.parseChapterNumber(sManga.title, this.name, this.chapter_number)
|
2022-05-25 00:02:02 +02:00
|
|
|
|
2021-10-31 00:36:23 +02:00
|
|
|
val format = getFormat(chapterFile)
|
2020-04-28 04:49:06 +02:00
|
|
|
if (format is Format.Epub) {
|
|
|
|
|
EpubFile(format.file).use { epub ->
|
|
|
|
|
epub.fillChapterMetadata(this)
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-05 12:01:58 +01:00
|
|
|
}
|
2020-04-25 20:24:45 +02:00
|
|
|
}
|
2021-11-19 22:42:19 +01:00
|
|
|
.map { it.toChapterInfo() }
|
2021-05-24 22:50:07 +02:00
|
|
|
.sortedWith { c1, c2 ->
|
2021-11-19 22:42:19 +01:00
|
|
|
val c = c2.number.compareTo(c1.number)
|
2021-05-24 22:50:07 +02:00
|
|
|
if (c == 0) c2.name.compareToCaseInsensitiveNaturalOrder(c1.name) else c
|
|
|
|
|
}
|
2020-04-25 20:24:45 +02:00
|
|
|
.toList()
|
2017-02-05 12:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
// Filters
|
|
|
|
|
override fun getFilterList() = FilterList(OrderBy(context))
|
|
|
|
|
|
|
|
|
|
private val POPULAR_FILTERS = FilterList(OrderBy(context))
|
|
|
|
|
private val LATEST_FILTERS = FilterList(OrderBy(context).apply { state = Filter.Sort.Selection(1, false) })
|
|
|
|
|
|
|
|
|
|
private class OrderBy(context: Context) : Filter.Sort(
|
|
|
|
|
context.getString(R.string.local_filter_order_by),
|
|
|
|
|
arrayOf(context.getString(R.string.title), context.getString(R.string.date)),
|
|
|
|
|
Selection(0, true),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Unused stuff
|
|
|
|
|
override suspend fun getPageList(chapter: ChapterInfo) = throw UnsupportedOperationException("Unused")
|
2021-11-19 22:42:19 +01:00
|
|
|
|
2022-05-25 00:02:02 +02:00
|
|
|
// Miscellaneous
|
2018-09-01 17:12:59 +02:00
|
|
|
private fun isSupportedFile(extension: String): Boolean {
|
2021-06-01 23:53:51 +02:00
|
|
|
return extension.lowercase() in SUPPORTED_ARCHIVE_TYPES
|
2018-09-01 17:12:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun getFormat(chapter: SChapter): Format {
|
2017-02-05 12:01:58 +01:00
|
|
|
val baseDirs = getBaseDirectories(context)
|
|
|
|
|
|
|
|
|
|
for (dir in baseDirs) {
|
|
|
|
|
val chapFile = File(dir, chapter.url)
|
|
|
|
|
if (!chapFile.exists()) continue
|
|
|
|
|
|
2018-09-01 17:12:59 +02:00
|
|
|
return getFormat(chapFile)
|
2017-02-05 12:01:58 +01:00
|
|
|
}
|
2021-05-24 22:50:07 +02:00
|
|
|
throw Exception(context.getString(R.string.chapter_not_found))
|
2017-02-05 12:01:58 +01:00
|
|
|
}
|
|
|
|
|
|
2021-09-18 22:13:14 +02:00
|
|
|
private fun getFormat(file: File) = with(file) {
|
|
|
|
|
when {
|
|
|
|
|
isDirectory -> Format.Directory(this)
|
|
|
|
|
extension.equals("zip", true) || extension.equals("cbz", true) -> Format.Zip(this)
|
|
|
|
|
extension.equals("rar", true) || extension.equals("cbr", true) -> Format.Rar(this)
|
|
|
|
|
extension.equals("epub", true) -> Format.Epub(this)
|
|
|
|
|
else -> throw Exception(context.getString(R.string.local_invalid_format))
|
2017-02-05 12:01:58 +01:00
|
|
|
}
|
2017-01-29 20:48:55 +01:00
|
|
|
}
|
|
|
|
|
|
2018-09-01 17:12:59 +02:00
|
|
|
private fun updateCover(chapter: SChapter, manga: SManga): File? {
|
2022-07-23 17:14:34 +02:00
|
|
|
return try {
|
|
|
|
|
when (val format = getFormat(chapter)) {
|
|
|
|
|
is Format.Directory -> {
|
|
|
|
|
val entry = format.file.listFiles()
|
|
|
|
|
?.sortedWith { f1, f2 -> f1.name.compareToCaseInsensitiveNaturalOrder(f2.name) }
|
|
|
|
|
?.find { !it.isDirectory && ImageUtil.isImage(it.name) { FileInputStream(it) } }
|
|
|
|
|
|
|
|
|
|
entry?.let { updateCover(context, manga, it.inputStream()) }
|
|
|
|
|
}
|
|
|
|
|
is Format.Zip -> {
|
|
|
|
|
ZipFile(format.file).use { zip ->
|
|
|
|
|
val entry = zip.entries().toList()
|
|
|
|
|
.sortedWith { f1, f2 -> f1.name.compareToCaseInsensitiveNaturalOrder(f2.name) }
|
|
|
|
|
.find { !it.isDirectory && ImageUtil.isImage(it.name) { zip.getInputStream(it) } }
|
2017-02-05 12:01:58 +01:00
|
|
|
|
2022-07-23 17:14:34 +02:00
|
|
|
entry?.let { updateCover(context, manga, zip.getInputStream(it)) }
|
|
|
|
|
}
|
2018-09-01 17:12:59 +02:00
|
|
|
}
|
2022-07-23 17:14:34 +02:00
|
|
|
is Format.Rar -> {
|
|
|
|
|
Archive(format.file).use { archive ->
|
|
|
|
|
val entry = archive.fileHeaders
|
|
|
|
|
.sortedWith { f1, f2 -> f1.fileName.compareToCaseInsensitiveNaturalOrder(f2.fileName) }
|
|
|
|
|
.find { !it.isDirectory && ImageUtil.isImage(it.fileName) { archive.getInputStream(it) } }
|
2017-02-08 22:12:00 +01:00
|
|
|
|
2022-07-23 17:14:34 +02:00
|
|
|
entry?.let { updateCover(context, manga, archive.getInputStream(it)) }
|
|
|
|
|
}
|
2018-09-01 17:12:59 +02:00
|
|
|
}
|
2022-07-23 17:14:34 +02:00
|
|
|
is Format.Epub -> {
|
|
|
|
|
EpubFile(format.file).use { epub ->
|
|
|
|
|
val entry = epub.getImagesFromPages()
|
|
|
|
|
.firstOrNull()
|
|
|
|
|
?.let { epub.getEntry(it) }
|
2017-02-05 12:01:58 +01:00
|
|
|
|
2022-07-23 17:14:34 +02:00
|
|
|
entry?.let { updateCover(context, manga, epub.getInputStream(it)) }
|
|
|
|
|
}
|
2017-02-05 12:01:58 +01:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-23 17:14:34 +02:00
|
|
|
} catch (e: Throwable) {
|
|
|
|
|
logcat(LogPriority.ERROR, e) { "Error updating cover for ${manga.title}" }
|
|
|
|
|
null
|
2017-02-05 12:01:58 +01:00
|
|
|
}
|
2018-09-01 17:12:59 +02:00
|
|
|
}
|
2017-02-05 12:01:58 +01:00
|
|
|
|
2018-09-01 17:12:59 +02:00
|
|
|
sealed class Format {
|
|
|
|
|
data class Directory(val file: File) : Format()
|
|
|
|
|
data class Zip(val file: File) : Format()
|
2020-02-17 23:23:37 +01:00
|
|
|
data class Rar(val file: File) : Format()
|
2018-09-01 17:12:59 +02:00
|
|
|
data class Epub(val file: File) : Format()
|
2017-02-05 12:01:58 +01:00
|
|
|
}
|
2022-05-25 00:02:02 +02:00
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
const val ID = 0L
|
|
|
|
|
const val HELP_URL = "https://tachiyomi.org/help/guides/local-manga/"
|
|
|
|
|
|
|
|
|
|
private const val DEFAULT_COVER_NAME = "cover.jpg"
|
|
|
|
|
private val LATEST_THRESHOLD = TimeUnit.MILLISECONDS.convert(7, TimeUnit.DAYS)
|
|
|
|
|
|
|
|
|
|
private fun getBaseDirectories(context: Context): Sequence<File> {
|
|
|
|
|
val localFolder = context.getString(R.string.app_name) + File.separator + "local"
|
|
|
|
|
return DiskUtil.getExternalStorages(context)
|
|
|
|
|
.map { File(it.absolutePath, localFolder) }
|
|
|
|
|
.asSequence()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getBaseDirectoriesFiles(context: Context): Sequence<File> {
|
|
|
|
|
return getBaseDirectories(context)
|
|
|
|
|
// Get all the files inside all baseDir
|
|
|
|
|
.flatMap { it.listFiles().orEmpty().toList() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getMangaDir(mangaUrl: String, baseDirsFile: Sequence<File>): File? {
|
|
|
|
|
return baseDirsFile
|
|
|
|
|
// Get the first mangaDir or null
|
|
|
|
|
.firstOrNull { it.isDirectory && it.name == mangaUrl }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getMangaDirsFiles(mangaUrl: String, baseDirsFile: Sequence<File>): Sequence<File> {
|
|
|
|
|
return baseDirsFile
|
|
|
|
|
// Filter out ones that are not related to the manga and is not a directory
|
|
|
|
|
.filter { it.isDirectory && it.name == mangaUrl }
|
|
|
|
|
// Get all the files inside the filtered folders
|
|
|
|
|
.flatMap { it.listFiles().orEmpty().toList() }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun getCoverFile(mangaUrl: String, baseDirsFile: Sequence<File>): File? {
|
|
|
|
|
return getMangaDirsFiles(mangaUrl, baseDirsFile)
|
|
|
|
|
// Get all file whose names start with 'cover'
|
|
|
|
|
.filter { it.isFile && it.nameWithoutExtension.equals("cover", ignoreCase = true) }
|
|
|
|
|
// Get the first actual image
|
|
|
|
|
.firstOrNull {
|
|
|
|
|
ImageUtil.isImage(it.name) { it.inputStream() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun updateCover(context: Context, manga: SManga, inputStream: InputStream): File? {
|
|
|
|
|
val baseDirsFiles = getBaseDirectoriesFiles(context)
|
|
|
|
|
|
|
|
|
|
val mangaDir = getMangaDir(manga.url, baseDirsFiles)
|
|
|
|
|
if (mangaDir == null) {
|
|
|
|
|
inputStream.close()
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var coverFile = getCoverFile(manga.url, baseDirsFiles)
|
|
|
|
|
if (coverFile == null) {
|
|
|
|
|
coverFile = File(mangaDir.absolutePath, DEFAULT_COVER_NAME)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// It might not exist at this point
|
|
|
|
|
coverFile.parentFile?.mkdirs()
|
|
|
|
|
inputStream.use { input ->
|
|
|
|
|
coverFile.outputStream().use { output ->
|
|
|
|
|
input.copyTo(output)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DiskUtil.createNoMediaFile(UniFile.fromFile(mangaDir), context)
|
|
|
|
|
|
|
|
|
|
manga.thumbnail_url = coverFile.absolutePath
|
|
|
|
|
return coverFile
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-01 17:12:59 +02:00
|
|
|
}
|
2021-10-31 00:36:23 +02:00
|
|
|
|
|
|
|
|
private val SUPPORTED_ARCHIVE_TYPES = listOf("zip", "cbz", "rar", "cbr", "epub")
|