Merge pull request #323 from AndyBernie/master

图片链接支持修改headers
pull/326/head
kunfei 4 years ago committed by GitHub
commit a590f0f11a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/src/main/assets/updateLog.md
  2. 6
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  3. 30
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  4. 1
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  5. 4
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt
  6. 10
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt
  7. 2
      app/src/main/java/io/legado/app/ui/widget/dialog/PhotoDialog.kt

@ -3,6 +3,9 @@
* 关注合作公众号 **[小说拾遗]()** 获取好看的小说。 * 关注合作公众号 **[小说拾遗]()** 获取好看的小说。
- 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 - 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。
**2020/08/20**
* 正文图片(漫画源)链接支持修改headers
**2020/08/19** **2020/08/19**
* 选择文本替换时带入书名和书源 * 选择文本替换时带入书名和书源

@ -49,13 +49,11 @@ object HttpHelper {
return null return null
} }
fun getBytes(url: String, referer: String): ByteArray? { fun getBytes(url: String, queryMap: Map<String, String>, headers: Map<String, String>): ByteArray? {
NetworkUtils.getBaseUrl(url)?.let { baseUrl -> NetworkUtils.getBaseUrl(url)?.let { baseUrl ->
val headers =
mapOf(Pair(AppConst.UA_NAME, AppConst.userAgent), Pair("Referer", referer))
return getByteRetrofit(baseUrl) return getByteRetrofit(baseUrl)
.create(HttpGetApi::class.java) .create(HttpGetApi::class.java)
.getMapByte(url, mapOf(), headers) .getMapByte(url, queryMap, headers)
.execute() .execute()
.body() .body()
} }

@ -4,6 +4,8 @@ import android.annotation.SuppressLint
import android.text.TextUtils import android.text.TextUtils
import androidx.annotation.Keep import androidx.annotation.Keep
import io.legado.app.constant.AppConst.SCRIPT_ENGINE import io.legado.app.constant.AppConst.SCRIPT_ENGINE
import io.legado.app.constant.AppConst.UA_NAME
import io.legado.app.constant.AppConst.userAgent
import io.legado.app.constant.AppPattern.EXP_PATTERN import io.legado.app.constant.AppPattern.EXP_PATTERN
import io.legado.app.constant.AppPattern.JS_PATTERN import io.legado.app.constant.AppPattern.JS_PATTERN
import io.legado.app.data.entities.BaseBook import io.legado.app.data.entities.BaseBook
@ -321,6 +323,34 @@ class AnalyzeUrl(
return Res(NetworkUtils.getUrl(res), res.body()) return Res(NetworkUtils.getUrl(res), res.body())
} }
@Throws(Exception::class)
fun getImageBytes(
tag: String
): ByteArray? {
//资源为本站的资源,保留cookie
//图片盗链的不保留当前的cookie,可由js生成图片源站的cookie
val cookie = CookieStore.getCookie(tag)
NetworkUtils.getBaseUrl(url)?.let {
val regex: Any
if(it.lastIndexOf(".") != it.indexOf(".")) {
regex = it.substring(it.indexOf(".")+1).toRegex()
} else {
regex = it.substring(it.lastIndexOf("/")+1).toRegex()
}
if(regex.containsMatchIn(tag)) {
if (cookie.isNotEmpty()) {
headerMap["Cookie"] += cookie
}
}
}
headerMap[UA_NAME] = headerMap[UA_NAME] ?:userAgent
if(fieldMap.isEmpty()) {
return HttpHelper.getBytes(url, mapOf(), headerMap)
} else {
return HttpHelper.getBytes(url, fieldMap, headerMap)
}
}
data class UrlOption( data class UrlOption(
val method: String?, val method: String?,
val charset: String?, val charset: String?,

@ -163,7 +163,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
ReadBook.book!!, ReadBook.book!!,
textPage.chapterIndex, textPage.chapterIndex,
textChar.charData, textChar.charData,
null,
true true
)?.let { )?.let {
canvas.drawBitmap(it, null, rectF, null) canvas.drawBitmap(it, null, rectF, null)

@ -39,7 +39,7 @@ object ChapterProvider {
lateinit var titlePaint: TextPaint lateinit var titlePaint: TextPaint
lateinit var contentPaint: TextPaint lateinit var contentPaint: TextPaint
private val srcPattern = private val srcPattern =
Pattern.compile("<img .*?src.*?=.*?\"(.*?)\".*?>", Pattern.CASE_INSENSITIVE) Pattern.compile("<img .*?src.*?=.*?\"(.*?(?:,\\{.*\\})?)\".*?>", Pattern.CASE_INSENSITIVE)
init { init {
upStyle() upStyle()
@ -122,7 +122,7 @@ object ChapterProvider {
imageStyle: String? imageStyle: String?
): Float { ): Float {
var durY = y var durY = y
ImageProvider.getImage(book, chapter.index, src, chapter.url)?.let { ImageProvider.getImage(book, chapter.index, src)?.let {
if (durY > visibleHeight) { if (durY > visibleHeight) {
textPages.last().height = durY textPages.last().height = durY
textPages.add(TextPage()) textPages.add(TextPage())

@ -11,6 +11,7 @@ import io.legado.app.utils.MD5Utils
import io.legado.app.utils.externalFilesDir import io.legado.app.utils.externalFilesDir
import java.io.FileOutputStream import java.io.FileOutputStream
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import io.legado.app.model.analyzeRule.AnalyzeUrl
object ImageProvider { object ImageProvider {
@ -31,13 +32,13 @@ object ImageProvider {
indexCache[src] = bitmap indexCache[src] = bitmap
} }
fun getImage(book: Book, chapterIndex: Int, src: String, referrer: String?, onUi: Boolean = false): Bitmap? { fun getImage(book: Book, chapterIndex: Int, src: String, onUi: Boolean = false): Bitmap? {
getCache(chapterIndex, src)?.let { getCache(chapterIndex, src)?.let {
return it return it
} }
val vFile = FileUtils.getFile( val vFile = FileUtils.getFile(
App.INSTANCE.externalFilesDir, App.INSTANCE.externalFilesDir,
"${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".")}", "${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".").substringBefore(",")}",
"images", book.name "images", book.name
) )
if (!vFile.exists()) { if (!vFile.exists()) {
@ -48,8 +49,9 @@ object ImageProvider {
out.flush() out.flush()
out.close() out.close()
} }
} else if (!onUi && referrer != null) { } else if (!onUi) {
HttpHelper.getBytes(src, referrer)?.let { val analyzeUrl = AnalyzeUrl(src, null, null, null, null)
analyzeUrl.getImageBytes(book.origin)?.let {
FileUtils.createFileIfNotExist(vFile.absolutePath).writeBytes(it) FileUtils.createFileIfNotExist(vFile.absolutePath).writeBytes(it)
} }
} }

@ -56,7 +56,7 @@ class PhotoDialog : BaseDialogFragment() {
val src = it.getString("src") val src = it.getString("src")
ReadBook.book?.let { book -> ReadBook.book?.let { book ->
src?.let { src?.let {
ImageProvider.getImage(book, chapterIndex, src, null)?.let { bitmap -> ImageProvider.getImage(book, chapterIndex, src)?.let { bitmap ->
photo_view.setImageBitmap(bitmap) photo_view.setImageBitmap(bitmap)
} }
} }

Loading…
Cancel
Save