2017-01-29 14:48:55 -05:00
|
|
|
package eu.kanade.tachiyomi.source
|
|
|
|
|
|
|
|
import android.content.Context
|
2020-05-06 22:59:54 -04:00
|
|
|
import com.google.gson.JsonParser
|
2017-01-29 14:48:55 -05:00
|
|
|
import eu.kanade.tachiyomi.R
|
2020-02-26 18:12:44 -05: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.Page
|
|
|
|
import eu.kanade.tachiyomi.source.model.SChapter
|
|
|
|
import eu.kanade.tachiyomi.source.model.SManga
|
2020-02-02 22:22:54 -05:00
|
|
|
import eu.kanade.tachiyomi.util.chapter.ChapterRecognition
|
|
|
|
import eu.kanade.tachiyomi.util.lang.compareToCaseInsensitiveNaturalOrder
|
2020-02-02 22:04:11 -05:00
|
|
|
import eu.kanade.tachiyomi.util.storage.DiskUtil
|
|
|
|
import eu.kanade.tachiyomi.util.storage.EpubFile
|
2020-02-02 22:22:54 -05:00
|
|
|
import eu.kanade.tachiyomi.util.system.ImageUtil
|
2017-01-29 14:48:55 -05:00
|
|
|
import java.io.File
|
|
|
|
import java.io.FileInputStream
|
|
|
|
import java.io.InputStream
|
2018-09-01 11:12:59 -04:00
|
|
|
import java.util.Locale
|
2017-01-29 14:48:55 -05:00
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
import java.util.zip.ZipEntry
|
|
|
|
import java.util.zip.ZipFile
|
2020-02-26 18:03:34 -05:00
|
|
|
import junrar.Archive
|
|
|
|
import junrar.rarfile.FileHeader
|
|
|
|
import rx.Observable
|
|
|
|
import timber.log.Timber
|
2017-01-29 14:48:55 -05:00
|
|
|
|
|
|
|
class LocalSource(private val context: Context) : CatalogueSource {
|
|
|
|
companion object {
|
2020-04-25 11:56:24 -04:00
|
|
|
const val HELP_URL = "https://tachiyomi.org/help/guides/reading-local-manga/"
|
|
|
|
|
|
|
|
private const val COVER_NAME = "cover.jpg"
|
2017-01-29 14:48:55 -05:00
|
|
|
private val POPULAR_FILTERS = FilterList(OrderBy())
|
|
|
|
private val LATEST_FILTERS = FilterList(OrderBy().apply { state = Filter.Sort.Selection(1, false) })
|
|
|
|
private val LATEST_THRESHOLD = TimeUnit.MILLISECONDS.convert(7, TimeUnit.DAYS)
|
2020-04-25 11:56:24 -04:00
|
|
|
const val ID = 0L
|
2017-01-29 14:48:55 -05:00
|
|
|
|
|
|
|
fun updateCover(context: Context, manga: SManga, input: InputStream): File? {
|
|
|
|
val dir = getBaseDirectories(context).firstOrNull()
|
|
|
|
if (dir == null) {
|
|
|
|
input.close()
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
val cover = File("${dir.absolutePath}/${manga.url}", COVER_NAME)
|
|
|
|
|
|
|
|
// It might not exist if using the external SD card
|
|
|
|
cover.parentFile.mkdirs()
|
|
|
|
input.use {
|
|
|
|
cover.outputStream().use {
|
|
|
|
input.copyTo(it)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cover
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getBaseDirectories(context: Context): List<File> {
|
2017-01-29 14:51:11 -05:00
|
|
|
val c = context.getString(R.string.app_name) + File.separator + "local"
|
|
|
|
return DiskUtil.getExternalStorages(context).map { File(it.absolutePath, c) }
|
2017-01-29 14:48:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override val id = ID
|
2017-11-18 08:09:28 -05:00
|
|
|
override val name = context.getString(R.string.local_source)
|
2017-06-17 06:46:34 -04:00
|
|
|
override val lang = ""
|
2017-01-29 14:48:55 -05:00
|
|
|
override val supportsLatest = true
|
|
|
|
|
|
|
|
override fun toString() = context.getString(R.string.local_source)
|
|
|
|
|
|
|
|
override fun fetchPopularManga(page: Int) = fetchSearchManga(page, "", POPULAR_FILTERS)
|
|
|
|
|
|
|
|
override fun fetchSearchManga(page: Int, query: String, filters: FilterList): Observable<MangasPage> {
|
|
|
|
val baseDirs = getBaseDirectories(context)
|
|
|
|
|
|
|
|
val time = if (filters === LATEST_FILTERS) System.currentTimeMillis() - LATEST_THRESHOLD else 0L
|
|
|
|
var mangaDirs = baseDirs.mapNotNull { it.listFiles()?.toList() }
|
2020-04-25 14:24:45 -04:00
|
|
|
.flatten()
|
|
|
|
.filter { it.isDirectory && if (time == 0L) it.name.contains(query, ignoreCase = true) else it.lastModified() >= time }
|
|
|
|
.distinctBy { it.name }
|
2017-01-29 14:48:55 -05:00
|
|
|
|
|
|
|
val state = ((if (filters.isEmpty()) POPULAR_FILTERS else filters)[0] as OrderBy).state
|
|
|
|
when (state?.index) {
|
|
|
|
0 -> {
|
2020-04-25 14:24:45 -04:00
|
|
|
mangaDirs = if (state.ascending) {
|
2020-04-18 14:47:22 -04:00
|
|
|
mangaDirs.sortedBy { it.name.toLowerCase(Locale.ENGLISH) }
|
2020-04-25 14:24:45 -04:00
|
|
|
} else {
|
2020-04-18 14:47:22 -04:00
|
|
|
mangaDirs.sortedByDescending { it.name.toLowerCase(Locale.ENGLISH) }
|
2020-04-25 14:24:45 -04:00
|
|
|
}
|
2017-01-29 14:48:55 -05:00
|
|
|
}
|
|
|
|
1 -> {
|
2020-04-25 14:24:45 -04:00
|
|
|
mangaDirs = if (state.ascending) {
|
2020-04-18 14:47:22 -04:00
|
|
|
mangaDirs.sortedBy(File::lastModified)
|
2020-04-25 14:24:45 -04:00
|
|
|
} else {
|
2020-04-18 14:47:22 -04:00
|
|
|
mangaDirs.sortedByDescending(File::lastModified)
|
2020-04-25 14:24:45 -04:00
|
|
|
}
|
2017-01-29 14:48:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val mangas = mangaDirs.map { mangaDir ->
|
|
|
|
SManga.create().apply {
|
|
|
|
title = mangaDir.name
|
|
|
|
url = mangaDir.name
|
|
|
|
|
|
|
|
// Try to find the cover
|
|
|
|
for (dir in baseDirs) {
|
|
|
|
val cover = File("${dir.absolutePath}/$url", COVER_NAME)
|
|
|
|
if (cover.exists()) {
|
2017-01-29 14:51:11 -05:00
|
|
|
thumbnail_url = cover.absolutePath
|
2017-01-29 14:48:55 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 22:49:06 -04:00
|
|
|
val chapters = fetchChapterList(this).toBlocking().first()
|
|
|
|
if (chapters.isNotEmpty()) {
|
|
|
|
val chapter = chapters.last()
|
|
|
|
val format = getFormat(chapter)
|
|
|
|
if (format is Format.Epub) {
|
|
|
|
EpubFile(format.file).use { epub ->
|
|
|
|
epub.fillMangaMetadata(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the cover from the first chapter found.
|
|
|
|
if (thumbnail_url == null) {
|
2018-09-01 11:12:59 -04:00
|
|
|
try {
|
2020-04-27 22:49:06 -04:00
|
|
|
val dest = updateCover(chapter, this)
|
2018-09-01 11:12:59 -04:00
|
|
|
thumbnail_url = dest?.absolutePath
|
|
|
|
} catch (e: Exception) {
|
|
|
|
Timber.e(e)
|
2017-01-29 14:48:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Observable.just(MangasPage(mangas, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun fetchLatestUpdates(page: Int) = fetchSearchManga(page, "", LATEST_FILTERS)
|
|
|
|
|
2020-01-11 15:59:43 -05:00
|
|
|
override fun fetchMangaDetails(manga: SManga): Observable<SManga> {
|
|
|
|
getBaseDirectories(context)
|
2020-04-25 14:24:45 -04:00
|
|
|
.mapNotNull { File(it, manga.url).listFiles()?.toList() }
|
|
|
|
.flatten()
|
|
|
|
.firstOrNull { it.extension == "json" }
|
|
|
|
?.apply {
|
2020-05-06 22:59:54 -04:00
|
|
|
val reader = this.inputStream().bufferedReader()
|
|
|
|
val json = JsonParser.parseReader(reader).asJsonObject
|
|
|
|
|
2020-04-25 14:24:45 -04:00
|
|
|
manga.title = json["title"]?.asString ?: manga.title
|
|
|
|
manga.author = json["author"]?.asString ?: manga.author
|
|
|
|
manga.artist = json["artist"]?.asString ?: manga.artist
|
|
|
|
manga.description = json["description"]?.asString ?: manga.description
|
|
|
|
manga.genre = json["genre"]?.asJsonArray?.joinToString(", ") { it.asString }
|
|
|
|
?: manga.genre
|
|
|
|
manga.status = json["status"]?.asInt ?: manga.status
|
|
|
|
}
|
2020-01-11 15:59:43 -05:00
|
|
|
return Observable.just(manga)
|
|
|
|
}
|
2017-02-05 06:01:58 -05:00
|
|
|
|
|
|
|
override fun fetchChapterList(manga: SManga): Observable<List<SChapter>> {
|
|
|
|
val chapters = getBaseDirectories(context)
|
2020-04-25 14:24:45 -04:00
|
|
|
.asSequence()
|
|
|
|
.mapNotNull { File(it, manga.url).listFiles()?.toList() }
|
|
|
|
.flatten()
|
|
|
|
.filter { it.isDirectory || isSupportedFile(it.extension) }
|
|
|
|
.map { chapterFile ->
|
|
|
|
SChapter.create().apply {
|
|
|
|
url = "${manga.url}/${chapterFile.name}"
|
2020-04-27 22:49:06 -04:00
|
|
|
name = if (chapterFile.isDirectory) {
|
2020-04-25 14:24:45 -04:00
|
|
|
chapterFile.name
|
|
|
|
} else {
|
|
|
|
chapterFile.nameWithoutExtension
|
2017-02-05 06:01:58 -05:00
|
|
|
}
|
2020-04-25 14:24:45 -04:00
|
|
|
date_upload = chapterFile.lastModified()
|
2020-04-27 22:49:06 -04:00
|
|
|
|
|
|
|
val format = getFormat(this)
|
|
|
|
if (format is Format.Epub) {
|
|
|
|
EpubFile(format.file).use { epub ->
|
|
|
|
epub.fillChapterMetadata(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
val chapNameCut = stripMangaTitle(name, manga.title)
|
|
|
|
if (chapNameCut.isNotEmpty()) name = chapNameCut
|
2020-04-25 14:24:45 -04:00
|
|
|
ChapterRecognition.parseChapterNumber(this, manga)
|
2017-02-05 06:01:58 -05:00
|
|
|
}
|
2020-04-25 14:24:45 -04:00
|
|
|
}
|
|
|
|
.sortedWith(
|
|
|
|
Comparator { c1, c2 ->
|
2017-02-05 06:01:58 -05:00
|
|
|
val c = c2.chapter_number.compareTo(c1.chapter_number)
|
2020-02-02 22:22:54 -05:00
|
|
|
if (c == 0) c2.name.compareToCaseInsensitiveNaturalOrder(c1.name) else c
|
2020-04-25 14:24:45 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
.toList()
|
2017-02-05 06:01:58 -05:00
|
|
|
|
|
|
|
return Observable.just(chapters)
|
|
|
|
}
|
|
|
|
|
2020-04-27 22:49:06 -04:00
|
|
|
/**
|
|
|
|
* Strips the manga title from a chapter name, matching only based on alphanumeric and whitespace
|
|
|
|
* characters.
|
|
|
|
*/
|
|
|
|
private fun stripMangaTitle(chapterName: String, mangaTitle: String): String {
|
|
|
|
var chapterNameIndex = 0
|
|
|
|
var mangaTitleIndex = 0
|
|
|
|
while (chapterNameIndex < chapterName.length && mangaTitleIndex < mangaTitle.length) {
|
|
|
|
val chapterChar = chapterName.get(chapterNameIndex)
|
|
|
|
val mangaChar = mangaTitle.get(mangaTitleIndex)
|
|
|
|
if (!chapterChar.equals(mangaChar, true)) {
|
|
|
|
val invalidChapterChar = !chapterChar.isLetterOrDigit() && !chapterChar.isWhitespace()
|
|
|
|
val invalidMangaChar = !mangaChar.isLetterOrDigit() && !mangaChar.isWhitespace()
|
|
|
|
|
|
|
|
if (!invalidChapterChar && !invalidMangaChar) {
|
|
|
|
return chapterName
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invalidChapterChar) {
|
|
|
|
chapterNameIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
if (invalidMangaChar) {
|
|
|
|
mangaTitleIndex++
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chapterNameIndex++
|
|
|
|
mangaTitleIndex++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return chapterName.substring(chapterNameIndex).trimStart(' ', '-', '_', ',', ':')
|
|
|
|
}
|
|
|
|
|
2017-02-05 06:01:58 -05:00
|
|
|
override fun fetchPageList(chapter: SChapter): Observable<List<Page>> {
|
2018-09-01 11:12:59 -04:00
|
|
|
return Observable.error(Exception("Unused"))
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun isSupportedFile(extension: String): Boolean {
|
|
|
|
return extension.toLowerCase() in setOf("zip", "rar", "cbr", "cbz", "epub")
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getFormat(chapter: SChapter): Format {
|
2017-02-05 06:01:58 -05:00
|
|
|
val baseDirs = getBaseDirectories(context)
|
|
|
|
|
|
|
|
for (dir in baseDirs) {
|
|
|
|
val chapFile = File(dir, chapter.url)
|
|
|
|
if (!chapFile.exists()) continue
|
|
|
|
|
2018-09-01 11:12:59 -04:00
|
|
|
return getFormat(chapFile)
|
2017-02-05 06:01:58 -05:00
|
|
|
}
|
2018-09-01 11:12:59 -04:00
|
|
|
throw Exception("Chapter not found")
|
2017-02-05 06:01:58 -05:00
|
|
|
}
|
|
|
|
|
2018-09-01 11:12:59 -04:00
|
|
|
private fun getFormat(file: File): Format {
|
2017-02-05 06:01:58 -05:00
|
|
|
val extension = file.extension
|
|
|
|
return if (file.isDirectory) {
|
2018-09-01 11:12:59 -04:00
|
|
|
Format.Directory(file)
|
2017-02-05 06:01:58 -05:00
|
|
|
} else if (extension.equals("zip", true) || extension.equals("cbz", true)) {
|
2018-09-01 11:12:59 -04:00
|
|
|
Format.Zip(file)
|
2017-02-08 16:12:00 -05:00
|
|
|
} else if (extension.equals("rar", true) || extension.equals("cbr", true)) {
|
2018-09-01 11:12:59 -04:00
|
|
|
Format.Rar(file)
|
|
|
|
} else if (extension.equals("epub", true)) {
|
|
|
|
Format.Epub(file)
|
2017-02-05 06:01:58 -05:00
|
|
|
} else {
|
|
|
|
throw Exception("Invalid chapter format")
|
|
|
|
}
|
2017-01-29 14:48:55 -05:00
|
|
|
}
|
|
|
|
|
2018-09-01 11:12:59 -04:00
|
|
|
private fun updateCover(chapter: SChapter, manga: SManga): File? {
|
2020-04-18 14:47:22 -04:00
|
|
|
return when (val format = getFormat(chapter)) {
|
2018-09-01 11:12:59 -04:00
|
|
|
is Format.Directory -> {
|
|
|
|
val entry = format.file.listFiles()
|
2020-04-25 14:24:45 -04:00
|
|
|
.sortedWith(Comparator<File> { f1, f2 -> f1.name.compareToCaseInsensitiveNaturalOrder(f2.name) })
|
|
|
|
.find { !it.isDirectory && ImageUtil.isImage(it.name) { FileInputStream(it) } }
|
2017-02-05 06:01:58 -05:00
|
|
|
|
2020-02-17 17:23:37 -05:00
|
|
|
entry?.let { updateCover(context, manga, it.inputStream()) }
|
2017-02-05 06:01:58 -05:00
|
|
|
}
|
2018-09-01 11:12:59 -04:00
|
|
|
is Format.Zip -> {
|
|
|
|
ZipFile(format.file).use { zip ->
|
|
|
|
val entry = zip.entries().toList()
|
2020-04-25 14:24:45 -04:00
|
|
|
.sortedWith(Comparator<ZipEntry> { f1, f2 -> f1.name.compareToCaseInsensitiveNaturalOrder(f2.name) })
|
|
|
|
.find { !it.isDirectory && ImageUtil.isImage(it.name) { zip.getInputStream(it) } }
|
2017-02-05 06:01:58 -05:00
|
|
|
|
2020-02-17 17:23:37 -05:00
|
|
|
entry?.let { updateCover(context, manga, zip.getInputStream(it)) }
|
2018-09-01 11:12:59 -04:00
|
|
|
}
|
2017-02-08 16:12:00 -05:00
|
|
|
}
|
2018-09-01 11:12:59 -04:00
|
|
|
is Format.Rar -> {
|
|
|
|
Archive(format.file).use { archive ->
|
|
|
|
val entry = archive.fileHeaders
|
2020-04-25 14:24:45 -04:00
|
|
|
.sortedWith(Comparator<FileHeader> { f1, f2 -> f1.fileNameString.compareToCaseInsensitiveNaturalOrder(f2.fileNameString) })
|
|
|
|
.find { !it.isDirectory && ImageUtil.isImage(it.fileNameString) { archive.getInputStream(it) } }
|
2017-02-08 16:12:00 -05:00
|
|
|
|
2020-02-17 17:23:37 -05:00
|
|
|
entry?.let { updateCover(context, manga, archive.getInputStream(it)) }
|
2018-09-01 11:12:59 -04:00
|
|
|
}
|
2017-02-05 06:01:58 -05:00
|
|
|
}
|
2018-09-01 11:12:59 -04:00
|
|
|
is Format.Epub -> {
|
|
|
|
EpubFile(format.file).use { epub ->
|
|
|
|
val entry = epub.getImagesFromPages()
|
2020-04-25 14:24:45 -04:00
|
|
|
.firstOrNull()
|
|
|
|
?.let { epub.getEntry(it) }
|
2017-02-05 06:01:58 -05:00
|
|
|
|
2018-09-01 11:12:59 -04:00
|
|
|
entry?.let { updateCover(context, manga, epub.getInputStream(it)) }
|
2017-02-05 06:01:58 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-01 11:12:59 -04:00
|
|
|
}
|
2017-02-05 06:01:58 -05:00
|
|
|
|
2020-04-18 14:47:22 -04:00
|
|
|
private class OrderBy : Filter.Sort("Order by", arrayOf("Title", "Date"), Selection(0, true))
|
2017-02-05 06:01:58 -05:00
|
|
|
|
2018-09-01 11:12:59 -04:00
|
|
|
override fun getFilterList() = FilterList(OrderBy())
|
2017-02-05 06:01:58 -05:00
|
|
|
|
2018-09-01 11:12:59 -04:00
|
|
|
sealed class Format {
|
|
|
|
data class Directory(val file: File) : Format()
|
|
|
|
data class Zip(val file: File) : Format()
|
2020-02-17 17:23:37 -05:00
|
|
|
data class Rar(val file: File) : Format()
|
2018-09-01 11:12:59 -04:00
|
|
|
data class Epub(val file: File) : Format()
|
2017-02-05 06:01:58 -05:00
|
|
|
}
|
2018-09-01 11:12:59 -04:00
|
|
|
}
|