From 0dff14fdb7881b65e3efcc65f03f1d3b1b079c1a Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 16 Jul 2021 14:01:48 +0800 Subject: [PATCH] =?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/ImageLoader.kt | 13 +++++++++++++ .../main/java/io/legado/app/web/HttpServer.kt | 19 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) 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 fa2411d38..15ff2b4cb 100644 --- a/app/src/main/java/io/legado/app/help/ImageLoader.kt +++ b/app/src/main/java/io/legado/app/help/ImageLoader.kt @@ -43,6 +43,19 @@ object ImageLoader { } } + fun loadFile(context: Context, path: String?): RequestBuilder { + return when { + path.isNullOrEmpty() -> Glide.with(context).asFile().load(path) + path.isAbsUrl() -> Glide.with(context).asFile().load(AnalyzeUrl(path).getGlideUrl()) + path.isContentScheme() -> Glide.with(context).asFile().load(Uri.parse(path)) + else -> kotlin.runCatching { + Glide.with(context).asFile().load(File(path)) + }.getOrElse { + Glide.with(context).asFile().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/web/HttpServer.kt b/app/src/main/java/io/legado/app/web/HttpServer.kt index 687383179..c3a5b125e 100644 --- a/app/src/main/java/io/legado/app/web/HttpServer.kt +++ b/app/src/main/java/io/legado/app/web/HttpServer.kt @@ -1,13 +1,17 @@ package io.legado.app.web +import android.graphics.Bitmap import com.google.gson.Gson import fi.iki.elonen.NanoHTTPD import io.legado.app.api.ReturnData import io.legado.app.api.controller.BookController import io.legado.app.api.controller.SourceController import io.legado.app.web.utils.AssetsWeb +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream import java.util.* + class HttpServer(port: Int) : NanoHTTPD(port) { private val assetsWeb = AssetsWeb("web") @@ -63,7 +67,20 @@ class HttpServer(port: Int) : NanoHTTPD(port) { return assetsWeb.getResponse(uri) } - val response = newFixedLengthResponse(Gson().toJson(returnData)) + val response = if (returnData.data is Bitmap) { + val outputStream = ByteArrayOutputStream() + (returnData.data as Bitmap).compress(Bitmap.CompressFormat.PNG, 100, outputStream) + val byteArray = outputStream.toByteArray() + val inputStream = ByteArrayInputStream(byteArray) + newFixedLengthResponse( + Response.Status.OK, + "image/png", + inputStream, + byteArray.size.toLong() + ) + } else { + newFixedLengthResponse(Gson().toJson(returnData)) + } response.addHeader("Access-Control-Allow-Methods", "GET, POST") response.addHeader("Access-Control-Allow-Origin", session.headers["origin"]) return response