Merge branch 'gedoor:master' into master

pull/1121/head
bushixuanqi 3 years ago committed by GitHub
commit a78193f66c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      app/src/main/java/io/legado/app/help/ImageLoader.kt
  2. 19
      app/src/main/java/io/legado/app/web/HttpServer.kt

@ -43,6 +43,19 @@ object ImageLoader {
}
}
fun loadFile(context: Context, path: String?): RequestBuilder<File> {
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<Drawable> {
return Glide.with(context).load(resId)
}

@ -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

Loading…
Cancel
Save