添加搜索书籍添加书籍api
This commit is contained in:
@@ -200,7 +200,7 @@ object BookController {
|
|||||||
fun saveBook(postData: String?): ReturnData {
|
fun saveBook(postData: String?): ReturnData {
|
||||||
val returnData = ReturnData()
|
val returnData = ReturnData()
|
||||||
GSON.fromJsonObject<Book>(postData).getOrNull()?.let { book ->
|
GSON.fromJsonObject<Book>(postData).getOrNull()?.let { book ->
|
||||||
appDb.bookDao.update(book)
|
appDb.bookDao.insert(book)
|
||||||
AppWebDav.uploadBookProgress(book)
|
AppWebDav.uploadBookProgress(book)
|
||||||
return returnData.setData("")
|
return returnData.setData("")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package io.legado.app.web
|
package io.legado.app.web
|
||||||
|
|
||||||
import android.graphics.Bitmap
|
import android.graphics.Bitmap
|
||||||
import com.google.gson.Gson
|
|
||||||
import fi.iki.elonen.NanoHTTPD
|
import fi.iki.elonen.NanoHTTPD
|
||||||
import io.legado.app.api.ReturnData
|
import io.legado.app.api.ReturnData
|
||||||
import io.legado.app.api.controller.BookController
|
import io.legado.app.api.controller.BookController
|
||||||
@@ -9,13 +8,11 @@ import io.legado.app.api.controller.BookSourceController
|
|||||||
import io.legado.app.api.controller.ReplaceRuleController
|
import io.legado.app.api.controller.ReplaceRuleController
|
||||||
import io.legado.app.api.controller.RssSourceController
|
import io.legado.app.api.controller.RssSourceController
|
||||||
import io.legado.app.service.WebService
|
import io.legado.app.service.WebService
|
||||||
import io.legado.app.utils.FileUtils
|
import io.legado.app.utils.*
|
||||||
import io.legado.app.utils.externalFiles
|
|
||||||
import io.legado.app.web.utils.AssetsWeb
|
import io.legado.app.web.utils.AssetsWeb
|
||||||
import splitties.init.appCtx
|
import splitties.init.appCtx
|
||||||
import java.io.*
|
import java.io.*
|
||||||
|
|
||||||
|
|
||||||
class HttpServer(port: Int) : NanoHTTPD(port) {
|
class HttpServer(port: Int) : NanoHTTPD(port) {
|
||||||
private val assetsWeb = AssetsWeb("web")
|
private val assetsWeb = AssetsWeb("web")
|
||||||
|
|
||||||
@@ -100,7 +97,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
|
|||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
newFixedLengthResponse(Gson().toJson(returnData))
|
newFixedLengthResponse(GSON.toJson(returnData))
|
||||||
} catch (e: OutOfMemoryError) {
|
} catch (e: OutOfMemoryError) {
|
||||||
val path = FileUtils.getPath(
|
val path = FileUtils.getPath(
|
||||||
appCtx.externalFiles,
|
appCtx.externalFiles,
|
||||||
@@ -109,7 +106,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
|
|||||||
)
|
)
|
||||||
val file = FileUtils.createFileIfNotExist(path)
|
val file = FileUtils.createFileIfNotExist(path)
|
||||||
BufferedWriter(FileWriter(file)).use {
|
BufferedWriter(FileWriter(file)).use {
|
||||||
Gson().toJson(returnData, it)
|
GSON.toJson(returnData, it)
|
||||||
}
|
}
|
||||||
val fis = FileInputStream(file)
|
val fis = FileInputStream(file)
|
||||||
newFixedLengthResponse(
|
newFixedLengthResponse(
|
||||||
|
|||||||
@@ -16,6 +16,9 @@ class WebSocketServer(port: Int) : NanoWSD(port) {
|
|||||||
"/rssSourceDebug" -> {
|
"/rssSourceDebug" -> {
|
||||||
RssSourceDebugWebSocket(handshake)
|
RssSourceDebugWebSocket(handshake)
|
||||||
}
|
}
|
||||||
|
"/searchBook" -> {
|
||||||
|
BookSearchWebSocket(handshake)
|
||||||
|
}
|
||||||
else -> null
|
else -> null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package io.legado.app.web.socket
|
||||||
|
|
||||||
|
import fi.iki.elonen.NanoHTTPD
|
||||||
|
import fi.iki.elonen.NanoWSD
|
||||||
|
import io.legado.app.R
|
||||||
|
import io.legado.app.data.entities.SearchBook
|
||||||
|
import io.legado.app.help.config.AppConfig
|
||||||
|
import io.legado.app.model.webBook.SearchModel
|
||||||
|
import io.legado.app.ui.book.search.SearchScope
|
||||||
|
import io.legado.app.utils.*
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
|
import splitties.init.appCtx
|
||||||
|
|
||||||
|
import java.io.IOException
|
||||||
|
|
||||||
|
class BookSearchWebSocket(handshakeRequest: NanoHTTPD.IHTTPSession) :
|
||||||
|
NanoWSD.WebSocket(handshakeRequest),
|
||||||
|
CoroutineScope by MainScope(),
|
||||||
|
SearchModel.CallBack {
|
||||||
|
|
||||||
|
private val normalClosure = NanoWSD.WebSocketFrame.CloseCode.NormalClosure
|
||||||
|
private val searchModel = SearchModel(this, this)
|
||||||
|
|
||||||
|
private const val SEARCH_FINISH = "Search finish"
|
||||||
|
|
||||||
|
override fun onOpen() {
|
||||||
|
launch(IO) {
|
||||||
|
kotlin.runCatching {
|
||||||
|
while (isOpen) {
|
||||||
|
ping("ping".toByteArray())
|
||||||
|
delay(30000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onClose(
|
||||||
|
code: NanoWSD.WebSocketFrame.CloseCode,
|
||||||
|
reason: String,
|
||||||
|
initiatedByRemote: Boolean
|
||||||
|
) {
|
||||||
|
cancel()
|
||||||
|
searchModel.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onMessage(message: NanoWSD.WebSocketFrame) {
|
||||||
|
launch(IO) {
|
||||||
|
kotlin.runCatching {
|
||||||
|
if (!message.textPayload.isJson()) {
|
||||||
|
send("数据必须为Json格式")
|
||||||
|
close(normalClosure, SEARCH_FINISH, false)
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
val searchMap =
|
||||||
|
GSON.fromJsonObject<Map<String, String>>(message.textPayload).getOrNull()
|
||||||
|
if (searchMap != null) {
|
||||||
|
val key = searchMap["key"]
|
||||||
|
if (key.isNullOrBlank()) {
|
||||||
|
send(appCtx.getString(R.string.cannot_empty))
|
||||||
|
close(normalClosure, SEARCH_FINISH, false)
|
||||||
|
return@launch
|
||||||
|
}
|
||||||
|
viewModel.search(System.currentTimeMillis(), key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onPong(pong: NanoWSD.WebSocketFrame) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onException(exception: IOException) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getSearchScope(): SearchScope = SearchScope(AppConfig.searchScope)
|
||||||
|
|
||||||
|
override fun onSearchStart() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSearchSuccess(searchBooks: ArrayList<SearchBook>) {
|
||||||
|
send(GSON.toJson(searchBooks))
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSearchFinish(isEmpty: Boolean) = close(normalClosure, SEARCH_FINISH, false)
|
||||||
|
|
||||||
|
override fun onSearchCancel(exception: Exception? = null) = close(normalClosure, exception?.toString() ?: SEARCH_FINISH, false)
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user