pull/1296/head
gedoor 4 years ago
parent b9ec61989d
commit f18c4f7cd4
  1. 4
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  2. 4
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  3. 16
      app/src/main/java/io/legado/app/help/BookHelp.kt
  4. 13
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  5. 4
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  6. 4
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  7. 2
      app/src/main/java/io/legado/app/model/webBook/BookContent.kt
  8. 8
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  9. 5
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
  10. 11
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt
  11. 2
      app/src/main/java/io/legado/app/ui/widget/dialog/PhotoDialog.kt

@ -48,6 +48,10 @@ data class BookSource(
return bookSourceUrl return bookSourceUrl
} }
override fun getSource(): BaseSource {
return this
}
@delegate:Transient @delegate:Transient
@delegate:Ignore @delegate:Ignore
@IgnoredOnParcel @IgnoredOnParcel

@ -48,6 +48,10 @@ data class RssSource(
return sourceUrl return sourceUrl
} }
override fun getSource(): BaseSource {
return this
}
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (other is RssSource) { if (other is RssSource) {
return other.sourceUrl == sourceUrl return other.sourceUrl == sourceUrl

@ -6,6 +6,7 @@ import io.legado.app.constant.EventBus
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookSource
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.localBook.LocalBook import io.legado.app.model.localBook.LocalBook
@ -77,7 +78,12 @@ object BookHelp {
return file return file
} }
suspend fun saveContent(book: Book, bookChapter: BookChapter, content: String) { suspend fun saveContent(
bookSource: BookSource,
book: Book,
bookChapter: BookChapter,
content: String
) {
if (content.isEmpty()) return if (content.isEmpty()) return
//保存文本 //保存文本
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
@ -92,14 +98,14 @@ object BookHelp {
if (matcher.find()) { if (matcher.find()) {
matcher.group(1)?.let { src -> matcher.group(1)?.let { src ->
val mSrc = NetworkUtils.getAbsoluteURL(bookChapter.url, src) val mSrc = NetworkUtils.getAbsoluteURL(bookChapter.url, src)
saveImage(book, mSrc) saveImage(bookSource, book, mSrc)
} }
} }
} }
postEvent(EventBus.SAVE_CONTENT, bookChapter) postEvent(EventBus.SAVE_CONTENT, bookChapter)
} }
suspend fun saveImage(book: Book, src: String) { suspend fun saveImage(bookSource: BookSource?, book: Book, src: String) {
while (downloadImages.contains(src)) { while (downloadImages.contains(src)) {
delay(100) delay(100)
} }
@ -107,9 +113,9 @@ object BookHelp {
return return
} }
downloadImages.add(src) downloadImages.add(src)
val analyzeUrl = AnalyzeUrl(src) val analyzeUrl = AnalyzeUrl(src, source = bookSource)
try { try {
analyzeUrl.getByteArray(book.origin).let { analyzeUrl.getByteArray().let {
FileUtils.createFileIfNotExist( FileUtils.createFileIfNotExist(
downloadDir, downloadDir,
cacheFolderName, cacheFolderName,

@ -4,6 +4,7 @@ import android.net.Uri
import android.util.Base64 import android.util.Base64
import androidx.annotation.Keep import androidx.annotation.Keep
import io.legado.app.constant.AppConst.dateFormat import io.legado.app.constant.AppConst.dateFormat
import io.legado.app.data.entities.BaseSource
import io.legado.app.help.http.* import io.legado.app.help.http.*
import io.legado.app.model.Debug import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
@ -33,13 +34,15 @@ import java.util.zip.ZipInputStream
@Suppress("unused") @Suppress("unused")
interface JsExtensions { interface JsExtensions {
fun getSource(): BaseSource?
/** /**
* 访问网络,返回String * 访问网络,返回String
*/ */
fun ajax(urlStr: String): String? { fun ajax(urlStr: String): String? {
return runBlocking { return runBlocking {
kotlin.runCatching { kotlin.runCatching {
val analyzeUrl = AnalyzeUrl(urlStr) val analyzeUrl = AnalyzeUrl(urlStr, source = getSource())
analyzeUrl.getStrResponse().body analyzeUrl.getStrResponse().body
}.onFailure { }.onFailure {
it.printStackTrace() it.printStackTrace()
@ -57,7 +60,7 @@ interface JsExtensions {
val asyncArray = Array(urlList.size) { val asyncArray = Array(urlList.size) {
async(IO) { async(IO) {
val url = urlList[it] val url = urlList[it]
val analyzeUrl = AnalyzeUrl(url) val analyzeUrl = AnalyzeUrl(url, source = getSource())
analyzeUrl.getStrResponse() analyzeUrl.getStrResponse()
} }
} }
@ -73,7 +76,7 @@ interface JsExtensions {
*/ */
fun connect(urlStr: String): StrResponse { fun connect(urlStr: String): StrResponse {
return runBlocking { return runBlocking {
val analyzeUrl = AnalyzeUrl(urlStr) val analyzeUrl = AnalyzeUrl(urlStr, source = getSource())
kotlin.runCatching { kotlin.runCatching {
analyzeUrl.getStrResponse() analyzeUrl.getStrResponse()
}.onFailure { }.onFailure {
@ -87,7 +90,7 @@ interface JsExtensions {
fun connect(urlStr: String, header: String?): StrResponse { fun connect(urlStr: String, header: String?): StrResponse {
return runBlocking { return runBlocking {
val headerMap = GSON.fromJsonObject<Map<String, String>>(header) val headerMap = GSON.fromJsonObject<Map<String, String>>(header)
val analyzeUrl = AnalyzeUrl(urlStr, headerMapF = headerMap) val analyzeUrl = AnalyzeUrl(urlStr, headerMapF = headerMap, source = getSource())
kotlin.runCatching { kotlin.runCatching {
analyzeUrl.getStrResponse() analyzeUrl.getStrResponse()
}.onFailure { }.onFailure {
@ -105,7 +108,7 @@ interface JsExtensions {
* @return 相对路径 * @return 相对路径
*/ */
fun downloadFile(content: String, url: String): String { fun downloadFile(content: String, url: String): String {
val type = AnalyzeUrl(url).type ?: return "" val type = AnalyzeUrl(url, source = getSource()).type ?: return ""
val zipPath = FileUtils.getPath( val zipPath = FileUtils.getPath(
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
"${MD5Utils.md5Encode16(url)}.${type}" "${MD5Utils.md5Encode16(url)}.${type}"

@ -652,6 +652,10 @@ class AnalyzeRule(
return SCRIPT_ENGINE.eval(jsStr, bindings) return SCRIPT_ENGINE.eval(jsStr, bindings)
} }
override fun getSource(): BaseSource? {
return source
}
/** /**
* js实现跨域访问,不能删 * js实现跨域访问,不能删
*/ */

@ -356,6 +356,10 @@ class AnalyzeUrl(
return GlideUrl(urlHasQuery, headers.build()) return GlideUrl(urlHasQuery, headers.build())
} }
override fun getSource(): BaseSource? {
return source
}
data class UrlOption( data class UrlOption(
val method: String?, val method: String?,
val charset: String?, val charset: String?,

@ -115,7 +115,7 @@ object BookContent {
Debug.log(bookSource.bookSourceUrl, "┌获取正文内容") Debug.log(bookSource.bookSourceUrl, "┌获取正文内容")
Debug.log(bookSource.bookSourceUrl, "\n$contentStr") Debug.log(bookSource.bookSourceUrl, "\n$contentStr")
if (contentStr.isNotBlank()) { if (contentStr.isNotBlank()) {
BookHelp.saveContent(book, bookChapter, contentStr) BookHelp.saveContent(bookSource, book, bookChapter, contentStr)
} }
return contentStr return contentStr
} }

@ -167,7 +167,13 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
isImageLine: Boolean isImageLine: Boolean
) { ) {
val book = ReadBook.book ?: return val book = ReadBook.book ?: return
ImageProvider.getImage(book, textPage.chapterIndex, textChar.charData, true)?.let { ImageProvider.getImage(
book,
textPage.chapterIndex,
textChar.charData,
ReadBook.bookSource,
true
)?.let {
val rectF = if (isImageLine) { val rectF = if (isImageLine) {
RectF(textChar.start, lineTop, textChar.end, lineBottom) RectF(textChar.start, lineTop, textChar.end, lineBottom)
} else { } else {

@ -12,6 +12,7 @@ import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.model.ReadBook
import io.legado.app.ui.book.read.page.entities.TextChapter import io.legado.app.ui.book.read.page.entities.TextChapter
import io.legado.app.ui.book.read.page.entities.TextChar import io.legado.app.ui.book.read.page.entities.TextChar
import io.legado.app.ui.book.read.page.entities.TextLine import io.legado.app.ui.book.read.page.entities.TextLine
@ -96,7 +97,7 @@ object ChapterProvider {
while (matcher.find()) { while (matcher.find()) {
matcher.group(1)?.let { src -> matcher.group(1)?.let { src ->
srcList.add(src) srcList.add(src)
ImageProvider.getImage(book, bookChapter.index, src) ImageProvider.getImage(book, bookChapter.index, src, ReadBook.bookSource)
matcher.appendReplacement(sb, srcReplaceChar) matcher.appendReplacement(sb, srcReplaceChar)
} }
} }
@ -173,7 +174,7 @@ object ChapterProvider {
imageStyle: String?, imageStyle: String?,
): Float { ): Float {
var durY = y var durY = y
ImageProvider.getImage(book, chapter.index, src)?.let { ImageProvider.getImage(book, chapter.index, src, ReadBook.bookSource)?.let {
if (durY > visibleHeight) { if (durY > visibleHeight) {
textPages.last().height = durY textPages.last().height = durY
textPages.add(TextPage()) textPages.add(TextPage())

@ -2,6 +2,7 @@ package io.legado.app.ui.book.read.page.provider
import android.graphics.Bitmap import android.graphics.Bitmap
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.model.localBook.EpubFile import io.legado.app.model.localBook.EpubFile
import io.legado.app.utils.BitmapUtils import io.legado.app.utils.BitmapUtils
@ -29,7 +30,13 @@ object ImageProvider {
indexCache[src] = bitmap indexCache[src] = bitmap
} }
fun getImage(book: Book, chapterIndex: Int, src: String, onUi: Boolean = false): Bitmap? { fun getImage(
book: Book,
chapterIndex: Int,
src: String,
bookSource: BookSource?,
onUi: Boolean = false,
): Bitmap? {
getCache(chapterIndex, src)?.let { getCache(chapterIndex, src)?.let {
return it return it
} }
@ -44,7 +51,7 @@ object ImageProvider {
} }
} else if (!onUi) { } else if (!onUi) {
runBlocking { runBlocking {
BookHelp.saveImage(book, src) BookHelp.saveImage(bookSource, book, src)
} }
} }
} }

@ -57,7 +57,7 @@ class PhotoDialog : BaseDialogFragment() {
ReadBook.book?.let { book -> ReadBook.book?.let { book ->
src?.let { src?.let {
execute { execute {
ImageProvider.getImage(book, chapterIndex, src) ImageProvider.getImage(book, chapterIndex, src, ReadBook.bookSource)
}.onSuccess { bitmap -> }.onSuccess { bitmap ->
if (bitmap != null) { if (bitmap != null) {
binding.photoView.setImageBitmap(bitmap) binding.photoView.setImageBitmap(bitmap)

Loading…
Cancel
Save