2017-01-29 20:48:55 +01:00
|
|
|
package eu.kanade.tachiyomi.source
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.net.Uri
|
|
|
|
import eu.kanade.tachiyomi.R
|
|
|
|
import eu.kanade.tachiyomi.source.model.*
|
|
|
|
import eu.kanade.tachiyomi.util.ChapterRecognition
|
|
|
|
import eu.kanade.tachiyomi.util.DiskUtil
|
|
|
|
import eu.kanade.tachiyomi.util.ZipContentProvider
|
|
|
|
import net.greypanther.natsort.CaseInsensitiveSimpleNaturalComparator
|
|
|
|
import rx.Observable
|
|
|
|
import timber.log.Timber
|
|
|
|
import java.io.File
|
|
|
|
import java.io.FileInputStream
|
|
|
|
import java.io.InputStream
|
|
|
|
import java.util.*
|
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
import java.util.zip.ZipEntry
|
|
|
|
import java.util.zip.ZipFile
|
|
|
|
|
|
|
|
class LocalSource(private val context: Context) : CatalogueSource {
|
|
|
|
companion object {
|
|
|
|
private val COVER_NAME = "cover.jpg"
|
|
|
|
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)
|
|
|
|
val ID = 0L
|
|
|
|
|
|
|
|
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 20:51:11 +01:00
|
|
|
val c = context.getString(R.string.app_name) + File.separator + "local"
|
|
|
|
return DiskUtil.getExternalStorages(context).map { File(it.absolutePath, c) }
|
2017-01-29 20:48:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override val id = ID
|
|
|
|
override val name = "LocalSource"
|
|
|
|
override val lang = "en"
|
|
|
|
override val supportsLatest = true
|
|
|
|
|
|
|
|
override fun toString() = context.getString(R.string.local_source)
|
|
|
|
|
|
|
|
override fun fetchMangaDetails(manga: SManga) = Observable.just(manga)
|
|
|
|
|
|
|
|
override fun fetchChapterList(manga: SManga): Observable<List<SChapter>> {
|
|
|
|
val chapters = getBaseDirectories(context)
|
|
|
|
.mapNotNull { File(it, manga.url).listFiles()?.toList() }
|
|
|
|
.flatten()
|
|
|
|
.filter { it.isDirectory || isSupportedFormat(it.extension) }
|
|
|
|
.map { chapterFile ->
|
|
|
|
SChapter.create().apply {
|
2017-01-29 20:51:11 +01:00
|
|
|
url = "${manga.url}/${chapterFile.name}"
|
2017-01-29 20:48:55 +01:00
|
|
|
val chapName = if (chapterFile.isDirectory) {
|
|
|
|
chapterFile.name
|
|
|
|
} else {
|
|
|
|
chapterFile.nameWithoutExtension
|
|
|
|
}
|
|
|
|
val chapNameCut = chapName.replace(manga.title, "", true)
|
|
|
|
name = if (chapNameCut.isEmpty()) chapName else chapNameCut
|
|
|
|
date_upload = chapterFile.lastModified()
|
|
|
|
ChapterRecognition.parseChapterNumber(this, manga)
|
|
|
|
}
|
|
|
|
}
|
2017-01-29 20:51:11 +01:00
|
|
|
.sortedByDescending { it.chapter_number }
|
2017-01-29 20:48:55 +01:00
|
|
|
|
2017-01-29 20:51:11 +01:00
|
|
|
return Observable.just(chapters)
|
2017-01-29 20:48:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun fetchPageList(chapter: SChapter): Observable<List<Page>> {
|
2017-01-29 20:51:11 +01:00
|
|
|
val baseDirs = getBaseDirectories(context)
|
|
|
|
|
|
|
|
for (dir in baseDirs) {
|
|
|
|
val chapFile = File(dir, chapter.url)
|
|
|
|
if (!chapFile.exists()) continue
|
|
|
|
|
|
|
|
val comparator = CaseInsensitiveSimpleNaturalComparator.getInstance<String>()
|
|
|
|
|
|
|
|
val pageList = if (chapFile.isDirectory) {
|
|
|
|
chapFile.listFiles()
|
|
|
|
.filter { !it.isDirectory && DiskUtil.isImage(it.name, { FileInputStream(it) }) }
|
|
|
|
.sortedWith(Comparator<File> { f1, f2 -> comparator.compare(f1.name, f2.name) })
|
|
|
|
.map { Uri.fromFile(it) }
|
|
|
|
} else {
|
|
|
|
val zip = ZipFile(chapFile)
|
|
|
|
zip.entries().toList()
|
|
|
|
.filter { !it.isDirectory && DiskUtil.isImage(it.name, { zip.getInputStream(it) }) }
|
|
|
|
.sortedWith(Comparator<ZipEntry> { f1, f2 -> comparator.compare(f1.name, f2.name) })
|
|
|
|
.map { Uri.parse("content://${ZipContentProvider.PROVIDER}${chapFile.absolutePath}!/${it.name}") }
|
|
|
|
}.mapIndexed { i, uri -> Page(i, uri = uri).apply { status = Page.READY } }
|
|
|
|
|
|
|
|
return Observable.just(pageList)
|
2017-01-29 20:48:55 +01:00
|
|
|
}
|
2017-01-29 20:51:11 +01:00
|
|
|
|
|
|
|
return Observable.error(Exception("Chapter not found"))
|
2017-01-29 20:48:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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() }
|
|
|
|
.flatten()
|
|
|
|
.filter { it.isDirectory && if (time == 0L) it.name.contains(query, ignoreCase = true) else it.lastModified() >= time }
|
|
|
|
.distinctBy { it.name }
|
|
|
|
|
|
|
|
val state = ((if (filters.isEmpty()) POPULAR_FILTERS else filters)[0] as OrderBy).state
|
|
|
|
when (state?.index) {
|
|
|
|
0 -> {
|
|
|
|
if (state!!.ascending)
|
|
|
|
mangaDirs = mangaDirs.sortedBy { it.name.toLowerCase(Locale.ENGLISH) }
|
|
|
|
else
|
|
|
|
mangaDirs = mangaDirs.sortedByDescending { it.name.toLowerCase(Locale.ENGLISH) }
|
|
|
|
}
|
|
|
|
1 -> {
|
|
|
|
if (state!!.ascending)
|
|
|
|
mangaDirs = mangaDirs.sortedBy(File::lastModified)
|
|
|
|
else
|
|
|
|
mangaDirs = mangaDirs.sortedByDescending(File::lastModified)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 20:51:11 +01:00
|
|
|
thumbnail_url = cover.absolutePath
|
2017-01-29 20:48:55 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the cover from the first chapter found.
|
|
|
|
if (thumbnail_url == null) {
|
|
|
|
val chapters = fetchChapterList(this).toBlocking().first()
|
|
|
|
if (chapters.isNotEmpty()) {
|
2017-01-30 21:04:56 +01:00
|
|
|
val uri = fetchPageList(chapters.last()).toBlocking().first().firstOrNull()?.uri
|
|
|
|
if (uri != null) {
|
|
|
|
val input = context.contentResolver.openInputStream(uri)
|
2017-01-29 20:48:55 +01:00
|
|
|
try {
|
|
|
|
val dest = updateCover(context, this, input)
|
2017-01-29 20:51:11 +01:00
|
|
|
thumbnail_url = dest?.absolutePath
|
2017-01-29 20:48:55 +01:00
|
|
|
} catch (e: Exception) {
|
|
|
|
Timber.e(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
initialized = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Observable.just(MangasPage(mangas, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun fetchLatestUpdates(page: Int) = fetchSearchManga(page, "", LATEST_FILTERS)
|
|
|
|
|
|
|
|
private fun isSupportedFormat(extension: String): Boolean {
|
|
|
|
return extension.equals("zip", true) || extension.equals("cbz", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
private class OrderBy : Filter.Sort("Order by", arrayOf("Title", "Date"), Filter.Sort.Selection(0, true))
|
|
|
|
|
|
|
|
override fun getFilterList() = FilterList(OrderBy())
|
|
|
|
}
|