Skip to content

Commit

Permalink
feat: 新增支持mongodb存放数据
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorqin committed Jul 19, 2022
1 parent f2935bf commit 8222501
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 34 deletions.
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ apply(plugin = "kotlin")
apply(plugin = "io.github.fvarrui.javapackager.plugin")

group = "com.htmake"
version = "2.5.6"
version = "2.5.7"

java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down Expand Up @@ -125,6 +125,9 @@ dependencies {

// 转换繁体
// implementation("com.github.liuyueyi.quick-chinese-transfer:quick-transfer-core:0.2.1")

// database
implementation("org.mongodb:mongodb-driver-sync:3.8.2")
}

// val compileKotlin: KotlinCompile by tasks
Expand Down
5 changes: 4 additions & 1 deletion cli.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ apply plugin: 'io.spring.dependency-management'
apply plugin: 'kotlin'

group = 'com.htmake'
version = '2.5.6'
version = '2.5.7'
sourceCompatibility = '1.8'

repositories {
Expand Down Expand Up @@ -82,6 +82,9 @@ dependencies {

// 转换繁体
// implementation "com.github.liuyueyi.quick-chinese-transfer:quick-transfer-core:0.2.1"

// database
implementation "org.mongodb:mongodb-driver-sync:3.8.2"
}
compileKotlin {
kotlinOptions {
Expand Down
4 changes: 4 additions & 0 deletions doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ reader:
cacheChapterContent: false # 是否缓存章节内容
userLimit: 50 # 用户上限,最大 50
userBookLimit: 200 # 用户书籍上限,默认最大 200
debugLog: false # 是否打开调试日志
autoClearInactiveUser: 0 # 是否自动清理不活跃用户,为0不清理,大于0为清理超过 autoClearInactiveUser 天未登录的用户
mongoUri: "" # mongodb uri 用于备份数据
mongoDbName: "reader" # mongodb 数据库名称

server:
port: 8080 # 监听端口
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/htmake/reader/api/YueduApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import com.htmake.reader.utils.unzip
import com.htmake.reader.utils.zip
import com.htmake.reader.utils.jsonEncode
import com.htmake.reader.utils.getRelativePath
import com.htmake.reader.utils.MongoManager
import com.htmake.reader.verticle.RestVerticle
import com.htmake.reader.SpringEvent
import org.springframework.stereotype.Component
Expand Down Expand Up @@ -87,6 +88,11 @@ class YueduApi : RestVerticle() {
override suspend fun initRouter(router: Router) {
setupPort()

// 连接 mongodDb
if (appConfig.mongoUri.isNotEmpty()) {
MongoManager.connect(appConfig.mongoUri)
}

// 旧版数据迁移
migration()

Expand Down
33 changes: 24 additions & 9 deletions src/main/java/com/htmake/reader/api/controller/BookController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import org.springframework.stereotype.Component
import io.vertx.core.json.JsonObject
import io.vertx.core.json.JsonArray
import io.vertx.core.http.HttpMethod
import io.vertx.core.buffer.Buffer
import com.htmake.reader.api.ReturnData
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.FileUtils
Expand All @@ -54,6 +55,8 @@ import java.net.URL;
import java.nio.charset.Charset
import java.util.UUID;
import io.vertx.ext.web.client.WebClient
import io.vertx.ext.web.client.HttpResponse
import io.vertx.kotlin.coroutines.awaitResult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.core.env.Environment
import java.io.File
Expand Down Expand Up @@ -210,16 +213,28 @@ class BookController(coroutineContext: CoroutineContext): BaseController(corouti
}

launch(Dispatchers.IO) {
webClient.getAbs(coverUrl).timeout(3000).send {
var bodyBytes = it.result()?.bodyAsBuffer()?.getBytes()
if (bodyBytes != null) {
var res = context.response().putHeader("Cache-Control", "86400")
cacheFile.writeBytes(bodyBytes)
res.sendFile(cacheFile.toString())
} else {
context.response().setStatusCode(404).end()
}
val result = awaitResult<HttpResponse<Buffer>> { handler ->
webClient.getAbs(coverUrl).timeout(3000).send(handler)
}
var bodyBytes = result?.bodyAsBuffer()?.getBytes()
if (bodyBytes != null) {
var res = context.response().putHeader("Cache-Control", "86400")
cacheFile.writeBytes(bodyBytes)
res.sendFile(cacheFile.toString())
} else {
context.response().setStatusCode(404).end()
}

// webClient.getAbs(coverUrl).timeout(3000).send {
// var bodyBytes = it.result()?.bodyAsBuffer()?.getBytes()
// if (bodyBytes != null) {
// var res = context.response().putHeader("Cache-Control", "86400")
// cacheFile.writeBytes(bodyBytes)
// res.sendFile(cacheFile.toString())
// } else {
// context.response().setStatusCode(404).end()
// }
// }
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/htmake/reader/config/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ class AppConfig {
var exportCharset = "UTF-8" // 导出字符集
var exportNoChapterName = false // 不添加章节名
var exportPictureFile = false // 导出图片

var mongoUri = "" // mongodb 链接
var mongoDbName = "reader" // mongodb 数据库名称
}
12 changes: 6 additions & 6 deletions src/main/java/com/htmake/reader/entity/BasicError.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.htmake.reader.entity

data class BasicError(
val error: String,
val exception: String,
val message: String,
val path: String,
val status: Int,
val timestamp: Long
val error: String,
val exception: String,
val message: String,
val path: String,
val status: Int,
val timestamp: Long
)
8 changes: 8 additions & 0 deletions src/main/java/com/htmake/reader/entity/MongoFile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.htmake.reader.entity

data class MongoFile(
var path: String = "",
var content: String = "",
var created_at: Long = System.currentTimeMillis(),
var updated_at: Long = System.currentTimeMillis()
)
4 changes: 2 additions & 2 deletions src/main/java/com/htmake/reader/entity/Size.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.htmake.reader.entity

data class Size(
val width: Double,
val height: Double
val width: Double,
val height: Double
)
18 changes: 9 additions & 9 deletions src/main/java/com/htmake/reader/entity/User.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.htmake.reader.entity

data class User(
var username: String="",
var password: String="",
var salt: String="",
var token: String="",
var last_login_at: Long = System.currentTimeMillis(),
var created_at: Long = System.currentTimeMillis(),
var enable_webdav: Boolean = false, // 是否开启 WebDAV 功能
var token_map: Map<String, Long>? = null,
var enable_local_store: Boolean = false // 是否开启本地书仓功能
var username: String="",
var password: String="",
var salt: String="",
var token: String="",
var last_login_at: Long = System.currentTimeMillis(),
var created_at: Long = System.currentTimeMillis(),
var enable_webdav: Boolean = false, // 是否开启 WebDAV 功能
var token_map: Map<String, Long>? = null,
var enable_local_store: Boolean = false // 是否开启本地书仓功能
)
43 changes: 43 additions & 0 deletions src/main/java/com/htmake/reader/utils/MongoManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.htmake.reader.utils;

// import com.mongodb.MongoClientSettings;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoCollection;
import com.htmake.reader.entity.MongoFile
import org.bson.codecs.pojo.PojoCodecProvider
import com.mongodb.MongoClientSettings.getDefaultCodecRegistry;
import org.bson.codecs.configuration.CodecRegistries.fromProviders;
import org.bson.codecs.configuration.CodecRegistries.fromRegistries;

object MongoManager {
private lateinit var mongoClient: MongoClient

fun isInit(): Boolean {
return ::mongoClient.isInitialized
}

fun connect(uri: String) {
try {
mongoClient = MongoClients.create(uri)
} catch (e: MongoException) {
logger.info("mongodb 连接失败,请检查链接({})是否正确", uri)
e.printStackTrace()
}
}

fun db(db: String): MongoDatabase? {
if (!isInit()) {
return null
}
val pojoCodecProvider = PojoCodecProvider.builder().automatic(true).build();
val pojoCodecRegistry = fromRegistries(getDefaultCodecRegistry(), fromProviders(pojoCodecProvider));
return mongoClient.getDatabase(db).withCodecRegistry(pojoCodecRegistry);
}

fun fileStorage(db: String, collection: String): MongoCollection<MongoFile>? {
return db(db)?.getCollection(collection, MongoFile::class.java)
}
}
59 changes: 54 additions & 5 deletions src/main/java/com/htmake/reader/utils/VertExt.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.htmake.reader.utils

import com.mongodb.client.model.Filters.eq;
import com.mongodb.client.model.ReplaceOptions;
import com.mongodb.client.MongoCollection
import com.google.common.base.Throwables
import com.google.gson.Gson
import com.google.gson.GsonBuilder
Expand All @@ -9,6 +12,7 @@ import io.vertx.core.json.JsonArray
import io.vertx.ext.web.RoutingContext
import mu.KotlinLogging
import com.htmake.reader.entity.BasicError
import com.htmake.reader.entity.MongoFile
import java.net.URLDecoder
import java.net.URLEncoder
import java.io.File
Expand Down Expand Up @@ -141,8 +145,8 @@ fun saveStorage(vararg name: String, value: Any, pretty: Boolean = false) {
}

val filename = name.last()
val file = File(getRelativePath(storagePath, *name.copyOfRange(0, name.size - 1), "${filename}.json"))
// val file = File(storagePath + "/${name}.json")
val path = getRelativePath(*name.copyOfRange(0, name.size - 1), "${filename}.json")
val file = File(storagePath + File.separator + path)
logger.info("Save file to storage name: {} path: {}", name, file.absoluteFile)

if (!file.parentFile.exists()) {
Expand All @@ -153,6 +157,7 @@ fun saveStorage(vararg name: String, value: Any, pretty: Boolean = false) {
file.createNewFile()
}
file.writeText(toJson)
saveMongoFile(path, toJson)
}

fun getStorage(vararg name: String): String? {
Expand All @@ -163,12 +168,56 @@ fun getStorage(vararg name: String): String? {
}

val filename = name.last()
val file = File(getRelativePath(storagePath, *name.copyOfRange(0, name.size - 1), "${filename}.json"))
val path = getRelativePath(*name.copyOfRange(0, name.size - 1), "${filename}.json")
val file = File(storagePath + File.separator + path)
logger.info("Read file from storage name: {} path: {}", name, file.absoluteFile)
if (!file.exists()) {
return null
return readMongoFile(path)
}
return file.readText()
val content = file.readText()
if (content.isEmpty()) {
return readMongoFile(path) ?: content
}
return content
}

fun getMongoFileStorage(): MongoCollection<MongoFile>? {
var appConfig = SpringContextUtils.getBean("appConfig", AppConfig::class.java)
return MongoManager.fileStorage(appConfig.mongoDbName, "storage")
}

fun readMongoFile(path: String): String? {
if (MongoManager.isInit()) {
logger.info("Get mongoFile {}", path)
val doc = getMongoFileStorage()?.find(eq("path", path))?.first();
if (doc != null) {
return doc.content
}
}
return null
}

fun saveMongoFile(path: String, content: String): Boolean {
if (MongoManager.isInit()) {
logger.info("Save mongoFile {}", path)
var doc = getMongoFileStorage()?.find(eq("path", path))?.first();
if (doc != null) {
doc.content = content
doc.updated_at = System.currentTimeMillis()
val result = getMongoFileStorage()?.replaceOne(eq("path", path), doc, ReplaceOptions().upsert(true));
return if(result != null && result.getModifiedCount() > 0) true else false
} else {
doc = MongoFile(path, content)
try {
getMongoFileStorage()?.insertOne(doc)
return true
} catch(e: Exception) {
logger.info("Save mongoFile {} failed", path)
e.printStackTrace()
}
}
}
return false
}

fun asJsonArray(value: Any?): JsonArray? {
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ reader:
userBookLimit: 200
debugLog: false
autoClearInactiveUser: 0
mongoUri: ""
mongoDbName: "reader"

server:
port: 8080
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ reader:
userBookLimit: 200
debugLog: false
autoClearInactiveUser: 0
mongoUri: ""
mongoDbName: "reader"

server:
port: 8080
Expand Down
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "reader",
"version": "2.5.6",
"version": "2.5.7",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
Expand Down

0 comments on commit 8222501

Please sign in to comment.