From 6ef1752d5eb761490d294910bfd65e09a262fab0 Mon Sep 17 00:00:00 2001 From: AndyBernie Date: Thu, 20 Aug 2020 13:56:06 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=9B=BE=E7=89=87=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E6=94=AF=E6=8C=81,{=20charset:=20,=20method:=20POST,=20body:?= =?UTF-8?q?=20,=20headers:=20{User-Agent:=20}=20}?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/http/HttpHelper.kt | 6 ++--- .../app/model/analyzeRule/AnalyzeUrl.kt | 27 +++++++++++++++++++ .../app/ui/book/read/page/ContentTextView.kt | 1 - .../read/page/provider/ChapterProvider.kt | 4 +-- .../book/read/page/provider/ImageProvider.kt | 10 ++++--- .../app/ui/widget/dialog/PhotoDialog.kt | 2 +- 6 files changed, 38 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index 8879e19b9..51103b52e 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -49,13 +49,11 @@ object HttpHelper { return null } - fun getBytes(url: String, referer: String): ByteArray? { + fun getBytes(url: String, queryMap: Map, headers: Map): ByteArray? { NetworkUtils.getBaseUrl(url)?.let { baseUrl -> - val headers = - mapOf(Pair(AppConst.UA_NAME, AppConst.userAgent), Pair("Referer", referer)) return getByteRetrofit(baseUrl) .create(HttpGetApi::class.java) - .getMapByte(url, mapOf(), headers) + .getMapByte(url, queryMap, headers) .execute() .body() } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 0e6031755..c3dbd2d6b 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -321,6 +321,33 @@ class AnalyzeUrl( return Res(NetworkUtils.getUrl(res), res.body()) } + @Throws(Exception::class) + fun getImageBytes( + tag: String + ): ByteArray? { + //资源为本站的资源,保留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 + } + } + } + //图片盗链的不保留当前的cookie,可由js生成图片源站的cookie + if(fieldMap.isEmpty()) { + return HttpHelper.getBytes(url, mapOf(), headerMap) + } else { + return HttpHelper.getBytes(url, fieldMap, headerMap) + } + } + data class UrlOption( val method: String?, val charset: String?, diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt index bc7e8c5b6..d21988163 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt @@ -163,7 +163,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at ReadBook.book!!, textPage.chapterIndex, textChar.charData, - null, true )?.let { canvas.drawBitmap(it, null, rectF, null) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt index c1e4bdf1e..3dbc0a5e1 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ChapterProvider.kt @@ -39,7 +39,7 @@ object ChapterProvider { lateinit var titlePaint: TextPaint lateinit var contentPaint: TextPaint private val srcPattern = - Pattern.compile("", Pattern.CASE_INSENSITIVE) + Pattern.compile("", Pattern.CASE_INSENSITIVE) init { upStyle() @@ -122,7 +122,7 @@ object ChapterProvider { imageStyle: String? ): Float { var durY = y - ImageProvider.getImage(book, chapter.index, src, chapter.url)?.let { + ImageProvider.getImage(book, chapter.index, src)?.let { if (durY > visibleHeight) { textPages.last().height = durY textPages.add(TextPage()) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt index b6109e9d3..3a6b80d8e 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt @@ -11,6 +11,7 @@ import io.legado.app.utils.MD5Utils import io.legado.app.utils.externalFilesDir import java.io.FileOutputStream import java.util.concurrent.ConcurrentHashMap +import io.legado.app.model.analyzeRule.AnalyzeUrl object ImageProvider { @@ -31,13 +32,13 @@ object ImageProvider { 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 { return it } val vFile = FileUtils.getFile( App.INSTANCE.externalFilesDir, - "${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".")}", + "${MD5Utils.md5Encode16(src)}${src.substringAfterLast(".").substringBefore(",")}", "images", book.name ) if (!vFile.exists()) { @@ -48,8 +49,9 @@ object ImageProvider { out.flush() out.close() } - } else if (!onUi && referrer != null) { - HttpHelper.getBytes(src, referrer)?.let { + } else if (!onUi) { + val analyzeUrl = AnalyzeUrl(src, null, null, null, null) + analyzeUrl.getImageBytes(book.origin)?.let { FileUtils.createFileIfNotExist(vFile.absolutePath).writeBytes(it) } } diff --git a/app/src/main/java/io/legado/app/ui/widget/dialog/PhotoDialog.kt b/app/src/main/java/io/legado/app/ui/widget/dialog/PhotoDialog.kt index 69c062295..74dfe23cd 100644 --- a/app/src/main/java/io/legado/app/ui/widget/dialog/PhotoDialog.kt +++ b/app/src/main/java/io/legado/app/ui/widget/dialog/PhotoDialog.kt @@ -56,7 +56,7 @@ class PhotoDialog : BaseDialogFragment() { val src = it.getString("src") ReadBook.book?.let { book -> src?.let { - ImageProvider.getImage(book, chapterIndex, src, null)?.let { bitmap -> + ImageProvider.getImage(book, chapterIndex, src)?.let { bitmap -> photo_view.setImageBitmap(bitmap) } } From 435827babd1b03791ce1aa9b3b8f1f720a51a4ae Mon Sep 17 00:00:00 2001 From: AndyBernie Date: Thu, 20 Aug 2020 17:33:02 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=9B=BE=E7=89=87=E9=93=BE=E6=8E=A5?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=BB=98=E8=AE=A4ua?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index c3dbd2d6b..9cbf64f8f 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -4,6 +4,8 @@ import android.annotation.SuppressLint import android.text.TextUtils import androidx.annotation.Keep 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.JS_PATTERN import io.legado.app.data.entities.BaseBook @@ -326,6 +328,7 @@ class AnalyzeUrl( tag: String ): ByteArray? { //资源为本站的资源,保留cookie + //图片盗链的不保留当前的cookie,可由js生成图片源站的cookie val cookie = CookieStore.getCookie(tag) NetworkUtils.getBaseUrl(url)?.let { val regex: Any @@ -336,11 +339,11 @@ class AnalyzeUrl( } if(regex.containsMatchIn(tag)) { if (cookie.isNotEmpty()) { - headerMap["Cookie"] = cookie + headerMap["Cookie"] += cookie } } } - //图片盗链的不保留当前的cookie,可由js生成图片源站的cookie + headerMap[UA_NAME] = headerMap[UA_NAME] ?:userAgent if(fieldMap.isEmpty()) { return HttpHelper.getBytes(url, mapOf(), headerMap) } else { From e8230f0bef21d3a08e4170ac0eed4d76bda37c47 Mon Sep 17 00:00:00 2001 From: AndyBernie Date: Thu, 20 Aug 2020 17:55:55 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index e0f13a0c8..00e9f11ac 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -3,6 +3,9 @@ * 关注合作公众号 **[小说拾遗]()** 获取好看的小说。 - 旧版数据导入教程:先在旧版阅读(2.x)中进行备份,然后在新版阅读(3.x)【我的】->【备份与恢复】,选择【导入旧版本数据】。 +**2020/08/20** +* 正文图片(漫画源)链接支持修改headers + **2020/08/19** * 选择文本替换时带入书名和书源