This commit is contained in:
Horis
2023-04-14 22:14:04 +08:00
parent dc304d31d9
commit 6932f767c9
23 changed files with 116 additions and 65 deletions
@@ -232,7 +232,7 @@ fun Book.isSameNameAuthor(other: Any?): Boolean {
fun Book.getExportFileName(suffix: String): String {
val jsStr = AppConfig.bookExportFileName
if (jsStr.isNullOrBlank()) {
return "${name} 作者:${getRealAuthor()}.$suffix"
return "$name 作者:${getRealAuthor()}.$suffix"
}
val bindings = SimpleBindings()
bindings["name"] = name
@@ -35,7 +35,15 @@ class OkHttpStreamFetcher(private val url: GlideUrl, private val options: Option
@Volatile
private var call: Call? = null
companion object {
val failUrl = hashSetOf<String>()
}
override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in InputStream>) {
if (failUrl.contains(url.toStringUrl())) {
callback.onLoadFailed(NoStackTraceException("跳过加载失败的图片"))
return
}
val loadOnlyWifi = options.get(OkHttpModelLoader.loadOnlyWifiOption) ?: false
if (loadOnlyWifi && !appCtx.isWifiConnect) {
callback.onLoadFailed(NoStackTraceException("只在wifi加载图片"))
@@ -96,6 +104,7 @@ class OkHttpStreamFetcher(private val url: GlideUrl, private val options: Option
callback?.onDataReady(stream)
}
} else {
failUrl.add(url.toStringUrl())
callback?.onLoadFailed(HttpException(response.message, response.code))
}
}
@@ -22,13 +22,11 @@ data class Authorization(
return "$username:$password"
}
constructor(serverID: Long?): this("","") {
serverID ?: throw WebDavException("Unexpected server ID")
appDb.serverDao.get(serverID)?.getWebDavConfig()?.run {
data = Credentials.basic(username, password, charset)
} ?: throw WebDavException("Unexpected WebDav Authorization")
}
constructor(serverID: Long) : this(
appDb.serverDao.get(serverID)?.getWebDavConfig()
?: throw WebDavException("Unexpected WebDav Authorization")
)
constructor(webDavConfig: WebDavConfig): this(webDavConfig.username, webDavConfig.password)
constructor(webDavConfig: WebDavConfig) : this(webDavConfig.username, webDavConfig.password)
}
@@ -39,7 +39,7 @@ open class WebDav(
companion object {
fun fromPath(path: String): WebDav {
val id = AnalyzeUrl(path).serverID
val id = AnalyzeUrl(path).serverID ?: throw WebDavException("没有serverID")
val authorization = Authorization(id)
return WebDav(path, authorization)
}
@@ -386,6 +386,16 @@ open class WebDav(
private fun checkResult(response: Response) {
if (!response.isSuccessful) {
val body = response.body?.string()
if (response.code == 401) {
val headers = response.headers("WWW-Authenticate")
val supportBasicAuth = headers.any {
it.startsWith("Basic", ignoreCase = true)
}
if (!supportBasicAuth) {
AppLog.put("服务器不支持BasicAuth认证")
}
}
if (response.message.isNotBlank() || body.isNullOrBlank()) {
throw WebDavException("${url}\n${response.code}:${response.message}")
}
@@ -14,10 +14,10 @@ class ServerConfigViewModel(application: Application): BaseViewModel(application
//mServer不为空可能是旋转屏幕界面重新创建,不用更新数据
if (mServer != null) return
execute {
if (id != null) {
mServer = appDb.serverDao.get(id)
mServer = if (id != null) {
appDb.serverDao.get(id)
} else {
mServer = Server()
Server()
}
}.onSuccess {
onSuccess.invoke()
@@ -98,6 +98,10 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
)
} else {
try {
val data = returnData.data
if (data is List<*> && data.size > 3000) {
throw OutOfMemoryError()
}
newFixedLengthResponse(GSON.toJson(returnData))
} catch (e: OutOfMemoryError) {
val path = FileUtils.getPath(
@@ -106,7 +110,7 @@ class HttpServer(port: Int) : NanoHTTPD(port) {
"bookSources.json"
)
val file = FileUtils.createFileIfNotExist(path)
BufferedWriter(FileWriter(file)).use {
BufferedWriter(FileWriter(file), 128 * 1024).use {
GSON.toJson(returnData, it)
}
val fis = FileInputStream(file)