From 749431068ad5505f67f006f54320fba4b3b71b93 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 16 Jul 2021 13:18:40 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=B0=81=E9=9D=A2api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/api/controller/BookController.kt | 20 +++++++++++++++---- .../app/api/controller/SourceController.kt | 2 +- .../java/io/legado/app/help/ImageLoader.kt | 13 ++++++++++++ .../java/io/legado/app/help/JsExtensions.kt | 2 +- .../main/java/io/legado/app/web/HttpServer.kt | 1 + 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/api/controller/BookController.kt b/app/src/main/java/io/legado/app/api/controller/BookController.kt index daccc43a7..496b24ae2 100644 --- a/app/src/main/java/io/legado/app/api/controller/BookController.kt +++ b/app/src/main/java/io/legado/app/api/controller/BookController.kt @@ -6,9 +6,11 @@ import io.legado.app.constant.PreferKey import io.legado.app.data.appDb import io.legado.app.data.entities.Book import io.legado.app.help.BookHelp +import io.legado.app.help.ImageLoader import io.legado.app.model.localBook.LocalBook import io.legado.app.model.webBook.WebBook import io.legado.app.service.help.ReadBook +import io.legado.app.ui.widget.image.CoverImageView import io.legado.app.utils.GSON import io.legado.app.utils.cnCompare import io.legado.app.utils.fromJsonObject @@ -40,13 +42,23 @@ object BookController { } } + fun getCover(parameters: Map>): ReturnData { + val returnData = ReturnData() + val coverPath = parameters["path"]?.firstOrNull() + val ftBitmap = ImageLoader.loadBitmap(appCtx, coverPath) + .placeholder(CoverImageView.defaultDrawable) + .error(CoverImageView.defaultDrawable) + .submit() + return returnData.setData(ftBitmap.get()) + } + /** * 更新目录 */ fun refreshToc(parameters: Map>): ReturnData { val returnData = ReturnData() try { - val bookUrl = parameters["url"]?.getOrNull(0) + val bookUrl = parameters["url"]?.firstOrNull() if (bookUrl.isNullOrEmpty()) { return returnData.setErrorMsg("参数url不能为空,请指定书籍地址") } @@ -90,7 +102,7 @@ object BookController { * 获取目录 */ fun getChapterList(parameters: Map>): ReturnData { - val bookUrl = parameters["url"]?.getOrNull(0) + val bookUrl = parameters["url"]?.firstOrNull() val returnData = ReturnData() if (bookUrl.isNullOrEmpty()) { return returnData.setErrorMsg("参数url不能为空,请指定书籍地址") @@ -103,8 +115,8 @@ object BookController { * 获取正文 */ fun getBookContent(parameters: Map>): ReturnData { - val bookUrl = parameters["url"]?.getOrNull(0) - val index = parameters["index"]?.getOrNull(0)?.toInt() + val bookUrl = parameters["url"]?.firstOrNull() + val index = parameters["index"]?.firstOrNull()?.toInt() val returnData = ReturnData() if (bookUrl.isNullOrEmpty()) { return returnData.setErrorMsg("参数url不能为空,请指定书籍地址") diff --git a/app/src/main/java/io/legado/app/api/controller/SourceController.kt b/app/src/main/java/io/legado/app/api/controller/SourceController.kt index 3972295e5..f4a92cd6d 100644 --- a/app/src/main/java/io/legado/app/api/controller/SourceController.kt +++ b/app/src/main/java/io/legado/app/api/controller/SourceController.kt @@ -59,7 +59,7 @@ object SourceController { } fun getSource(parameters: Map>): ReturnData { - val url = parameters["url"]?.getOrNull(0) + val url = parameters["url"]?.firstOrNull() val returnData = ReturnData() if (url.isNullOrEmpty()) { return returnData.setErrorMsg("参数url不能为空,请指定书源地址") diff --git a/app/src/main/java/io/legado/app/help/ImageLoader.kt b/app/src/main/java/io/legado/app/help/ImageLoader.kt index cc702ff0f..fa2411d38 100644 --- a/app/src/main/java/io/legado/app/help/ImageLoader.kt +++ b/app/src/main/java/io/legado/app/help/ImageLoader.kt @@ -30,6 +30,19 @@ object ImageLoader { } } + fun loadBitmap(context: Context, path: String?): RequestBuilder { + return when { + path.isNullOrEmpty() -> Glide.with(context).asBitmap().load(path) + path.isAbsUrl() -> Glide.with(context).asBitmap().load(AnalyzeUrl(path).getGlideUrl()) + path.isContentScheme() -> Glide.with(context).asBitmap().load(Uri.parse(path)) + else -> kotlin.runCatching { + Glide.with(context).asBitmap().load(File(path)) + }.getOrElse { + Glide.with(context).asBitmap().load(path) + } + } + } + fun load(context: Context, @DrawableRes resId: Int?): RequestBuilder { return Glide.with(context).load(resId) } diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index 802b450c6..ab02185b1 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -285,7 +285,7 @@ interface JsExtensions { val zipFile = getFile(zipPath) val unzipFolder = FileUtils.createFolderIfNotExist(unzipPath) ZipUtils.unzipFile(zipFile, unzipFolder) - FileUtils.deleteFile(zipPath) + FileUtils.deleteFile(zipFile.absolutePath) return unzipPath.substring(FileUtils.getCachePath().length) } diff --git a/app/src/main/java/io/legado/app/web/HttpServer.kt b/app/src/main/java/io/legado/app/web/HttpServer.kt index 9b0d09e0e..687383179 100644 --- a/app/src/main/java/io/legado/app/web/HttpServer.kt +++ b/app/src/main/java/io/legado/app/web/HttpServer.kt @@ -51,6 +51,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) { "/getChapterList" -> BookController.getChapterList(parameters) "/refreshToc" -> BookController.refreshToc(parameters) "/getBookContent" -> BookController.getBookContent(parameters) + "/cover" -> BookController.getCover(parameters) else -> null } } From 5baa083a8e2181a1a3e8b6e424cc15e93f510e7b Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 16 Jul 2021 13:25:14 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/help/JsExtensions.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index ab02185b1..ce27c40c7 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -306,7 +306,7 @@ interface JsExtensions { contents.deleteCharAt(contents.length - 1) } } - FileUtils.deleteFile(unzipPath) + FileUtils.deleteFile(unzipFolder.absolutePath) return contents.toString() } From e82e92ba073b44ca5d4a81efac1aa1ab3fde3ebe Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 16 Jul 2021 13:34:17 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/help/JsExtensions.kt | 32 +++++-------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index ce27c40c7..56326e382 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -443,10 +443,7 @@ interface JsExtensions { * @param iv ECB模式的偏移向量 */ fun aesDecodeToByteArray( - str: String, - key: String, - transformation: String, - iv: String + str: String, key: String, transformation: String, iv: String ): ByteArray? { return EncoderUtils.decryptAES( data = str.encodeToByteArray(), @@ -465,10 +462,7 @@ interface JsExtensions { */ fun aesDecodeToString( - str: String, - key: String, - transformation: String, - iv: String + str: String, key: String, transformation: String, iv: String ): String? { return aesDecodeToByteArray(str, key, transformation, iv)?.let { String(it) } } @@ -482,10 +476,7 @@ interface JsExtensions { */ fun aesBase64DecodeToByteArray( - str: String, - key: String, - transformation: String, - iv: String + str: String, key: String, transformation: String, iv: String ): ByteArray? { return EncoderUtils.decryptBase64AES( data = str.encodeToByteArray(), @@ -504,10 +495,7 @@ interface JsExtensions { */ fun aesBase64DecodeToString( - str: String, - key: String, - transformation: String, - iv: String + str: String, key: String, transformation: String, iv: String ): String? { return aesBase64DecodeToByteArray(str, key, transformation, iv)?.let { String(it) } } @@ -520,8 +508,7 @@ interface JsExtensions { * @param iv ECB模式的偏移向量 */ fun aesEncodeToByteArray( - data: String, key: String, transformation: String, - iv: String + data: String, key: String, transformation: String, iv: String ): ByteArray? { return EncoderUtils.encryptAES( data.encodeToByteArray(), @@ -539,8 +526,7 @@ interface JsExtensions { * @param iv ECB模式的偏移向量 */ fun aesEncodeToString( - data: String, key: String, transformation: String, - iv: String + data: String, key: String, transformation: String, iv: String ): String? { return aesEncodeToByteArray(data, key, transformation, iv)?.let { String(it) } } @@ -553,8 +539,7 @@ interface JsExtensions { * @param iv ECB模式的偏移向量 */ fun aesEncodeToBase64ByteArray( - data: String, key: String, transformation: String, - iv: String + data: String, key: String, transformation: String, iv: String ): ByteArray? { return EncoderUtils.encryptAES2Base64( data.encodeToByteArray(), @@ -572,8 +557,7 @@ interface JsExtensions { * @param iv ECB模式的偏移向量 */ fun aesEncodeToBase64String( - data: String, key: String, transformation: String, - iv: String + data: String, key: String, transformation: String, iv: String ): String? { return aesEncodeToBase64ByteArray(data, key, transformation, iv)?.let { String(it) } }