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
}
override fun getSource(): BaseSource {
return this
}
@delegate:Transient
@delegate:Ignore
@IgnoredOnParcel

@ -48,6 +48,10 @@ data class RssSource(
return sourceUrl
}
override fun getSource(): BaseSource {
return this
}
override fun equals(other: Any?): Boolean {
if (other is RssSource) {
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.entities.Book
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.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.localBook.LocalBook
@ -77,7 +78,12 @@ object BookHelp {
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
//保存文本
FileUtils.createFileIfNotExist(
@ -92,14 +98,14 @@ object BookHelp {
if (matcher.find()) {
matcher.group(1)?.let { src ->
val mSrc = NetworkUtils.getAbsoluteURL(bookChapter.url, src)
saveImage(book, mSrc)
saveImage(bookSource, book, mSrc)
}
}
}
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)) {
delay(100)
}
@ -107,9 +113,9 @@ object BookHelp {
return
}
downloadImages.add(src)
val analyzeUrl = AnalyzeUrl(src)
val analyzeUrl = AnalyzeUrl(src, source = bookSource)
try {
analyzeUrl.getByteArray(book.origin).let {
analyzeUrl.getByteArray().let {
FileUtils.createFileIfNotExist(
downloadDir,
cacheFolderName,

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

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

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

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

@ -167,7 +167,13 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
isImageLine: Boolean
) {
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) {
RectF(textChar.start, lineTop, textChar.end, lineBottom)
} else {

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

@ -2,6 +2,7 @@ package io.legado.app.ui.book.read.page.provider
import android.graphics.Bitmap
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookSource
import io.legado.app.help.BookHelp
import io.legado.app.model.localBook.EpubFile
import io.legado.app.utils.BitmapUtils
@ -29,7 +30,13 @@ object ImageProvider {
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 {
return it
}
@ -44,7 +51,7 @@ object ImageProvider {
}
} else if (!onUi) {
runBlocking {
BookHelp.saveImage(book, src)
BookHelp.saveImage(bookSource, book, src)
}
}
}

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

Loading…
Cancel
Save