pull/32/head
kunfei 5 years ago
parent 59b68e45d4
commit b2401f60e0
  1. 75
      app/src/main/java/io/legado/app/web/HttpServer.kt
  2. 2
      app/src/main/java/io/legado/app/web/controller/BookshelfController.kt
  3. 6
      app/src/main/java/io/legado/app/web/controller/SourceController.kt

@ -0,0 +1,75 @@
package io.legado.app.web
import com.google.gson.Gson
import fi.iki.elonen.NanoHTTPD
import io.legado.app.web.controller.BookshelfController
import io.legado.app.web.controller.SourceController
import io.legado.app.web.utils.AssetsWeb
import io.legado.app.web.utils.ReturnData
import java.util.*
class HttpServer(port: Int) : NanoHTTPD(port) {
private val assetsWeb = AssetsWeb("web")
override fun serve(session: NanoHTTPD.IHTTPSession): NanoHTTPD.Response {
var returnData: ReturnData? = null
var uri = session.uri
try {
when (session.method.name) {
"OPTIONS" -> {
val response = NanoHTTPD.newFixedLengthResponse("")
response.addHeader("Access-Control-Allow-Methods", "POST")
response.addHeader("Access-Control-Allow-Headers", "content-type")
response.addHeader("Access-Control-Allow-Origin", session.headers["origin"])
//response.addHeader("Access-Control-Max-Age", "3600");
return response
}
"POST" -> {
val files = HashMap<String, String>()
session.parseBody(files)
val postData = files["postData"]
when (uri) {
"/saveSource" -> returnData = SourceController().saveSource(postData)
"/saveSources" -> returnData = SourceController().saveSources(postData)
"/saveBook" -> returnData = BookshelfController().saveBook(postData)
"/deleteSources" -> returnData = SourceController().deleteSources(postData)
}
}
"GET" -> {
val parameters = session.parameters
when (uri) {
"/getSource" -> returnData = SourceController().getSource(parameters)
"/getSources" -> returnData = SourceController().sources
"/getBookshelf" -> returnData = BookshelfController().bookshelf
"/getChapterList" -> returnData =
BookshelfController().getChapterList(parameters)
"/getBookContent" -> returnData =
BookshelfController().getBookContent(parameters)
}
}
}
if (returnData == null) {
if (uri.endsWith("/")) {
uri += "index.html"
}
return assetsWeb.getResponse(uri)
}
val response = NanoHTTPD.newFixedLengthResponse(Gson().toJson(returnData))
response.addHeader("Access-Control-Allow-Methods", "GET, POST")
response.addHeader("Access-Control-Allow-Origin", session.headers["origin"])
return response
} catch (e: Exception) {
return NanoHTTPD.newFixedLengthResponse(e.message)
}
}
}

@ -43,7 +43,7 @@ class BookshelfController {
return returnData
}
fun saveBook(postData: String): ReturnData {
fun saveBook(postData: String?): ReturnData {
val book = GSON.fromJsonObject<Book>(postData)
val returnData = ReturnData()
if (book != null) {

@ -20,7 +20,7 @@ class SourceController {
} else returnData.setData(bookSources)
}
fun saveSource(postData: String): ReturnData {
fun saveSource(postData: String?): ReturnData {
val returnData = ReturnData()
try {
val bookSource = GSON.fromJsonObject<BookSource>(postData)
@ -40,7 +40,7 @@ class SourceController {
return returnData
}
fun saveSources(postData: String): ReturnData {
fun saveSources(postData: String?): ReturnData {
val okSources = arrayListOf<BookSource>()
kotlin.runCatching {
val bookSources = GSON.fromJsonArray<BookSource>(postData)
@ -68,7 +68,7 @@ class SourceController {
return returnData.setData(bookSource)
}
fun deleteSources(postData: String): ReturnData {
fun deleteSources(postData: String?): ReturnData {
kotlin.runCatching {
GSON.fromJsonArray<BookSource>(postData)?.let {
App.db.bookSourceDao().delete(*it.toTypedArray())

Loading…
Cancel
Save