Compare commits

...

2 Commits

Author SHA1 Message Date
Horis
5eaf2f034f 优化 2024-05-13 10:51:20 +08:00
Horis
2aed021e08 优化
#3929
2024-05-11 19:26:30 +08:00
5 changed files with 20 additions and 34 deletions

View File

@ -71,6 +71,8 @@ open class WebDav(
<resourcetype />
</prop>
</propfind>"""
private const val DEFAULT_CONTENT_TYPE = "application/octet-stream"
}
@ -304,18 +306,12 @@ open class WebDav(
* 上传文件
*/
@Throws(WebDavException::class)
suspend fun upload(
localPath: String,
contentType: String = "application/octet-stream"
) {
suspend fun upload(localPath: String, contentType: String = DEFAULT_CONTENT_TYPE) {
upload(File(localPath), contentType)
}
@Throws(WebDavException::class)
suspend fun upload(
file: File,
contentType: String = "application/octet-stream"
) {
suspend fun upload(file: File, contentType: String = DEFAULT_CONTENT_TYPE) {
kotlin.runCatching {
withContext(IO) {
if (!file.exists()) throw WebDavException("文件不存在")
@ -336,7 +332,7 @@ open class WebDav(
}
@Throws(WebDavException::class)
suspend fun upload(byteArray: ByteArray, contentType: String) {
suspend fun upload(byteArray: ByteArray, contentType: String = DEFAULT_CONTENT_TYPE) {
// 务必注意RequestBody不要嵌套不然上传时内容可能会被追加多余的文件信息
kotlin.runCatching {
withContext(IO) {
@ -356,7 +352,7 @@ open class WebDav(
}
@Throws(WebDavException::class)
suspend fun upload(uri: Uri, contentType: String) {
suspend fun upload(uri: Uri, contentType: String = DEFAULT_CONTENT_TYPE) {
// 务必注意RequestBody不要嵌套不然上传时内容可能会被追加多余的文件信息
kotlin.runCatching {
withContext(IO) {

View File

@ -14,10 +14,7 @@ import io.legado.app.model.analyzeRule.CustomUrl
import io.legado.app.model.localBook.LocalBook
import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.readBytes
import kotlinx.coroutines.runBlocking
import splitties.init.appCtx
import java.io.File
class RemoteBookWebDav(
val rootBookUrl: String,
@ -71,20 +68,17 @@ class RemoteBookWebDav(
override suspend fun upload(book: Book) {
if (!NetworkUtils.isAvailable()) throw NoStackTraceException("网络不可用")
val localBookUri = Uri.parse(book.bookUrl)
val putUrl = "$rootBookUrl${File.separator}${book.originName}"
val putUrl = "$rootBookUrl${book.originName}"
val webDav = WebDav(putUrl, authorization)
if (localBookUri.isContentScheme()) {
webDav.upload(
byteArray = localBookUri.readBytes(appCtx),
contentType = "application/octet-stream"
)
webDav.upload(localBookUri)
} else {
webDav.upload(localBookUri.path!!)
}
book.origin = BookType.webDavTag + CustomUrl(putUrl)
.putAttribute("serverID", serverID)
.toString()
book.save()
book.update()
}
override suspend fun delete(remoteBookUrl: String) {

View File

@ -96,12 +96,11 @@ class WebService : BaseService() {
if (addressList.any()) {
notificationList.addAll(addressList.map { address -> getString(R.string.http_ip, address.hostAddress, getPort()) })
hostAddress = notificationList.first()
startForegroundNotification()
} else {
hostAddress = getString(R.string.network_connection_unavailable)
notificationList.add(hostAddress)
startForegroundNotification()
}
startForegroundNotification()
postEvent(EventBus.WEB_SERVICE, hostAddress)
}
}
@ -184,6 +183,7 @@ class WebService : BaseService() {
*/
override fun startForegroundNotification() {
val builder = NotificationCompat.Builder(this, AppConst.channelIdWeb)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_web_service_noti)
.setOngoing(true)
.setContentTitle(getString(R.string.web_service))
@ -196,7 +196,6 @@ class WebService : BaseService() {
getString(R.string.cancel),
servicePendingIntent<WebService>(IntentAction.stop)
)
builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
val notification = builder.build()
startForeground(NotificationId.WebService, notification)
}

View File

@ -20,7 +20,7 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import java.util.*
import java.util.Collections
class RemoteBookViewModel(application: Application) : BaseViewModel(application) {
var sortKey = RemoteBookSort.Default
@ -78,6 +78,7 @@ class RemoteBookViewModel(application: Application) : BaseViewModel(application)
}
return@sortedWith compare
}
else -> list.sortedWith { o1, o2 ->
val compare = -compareValues(o1.isDir, o2.isDir)
if (compare == 0) {
@ -132,10 +133,8 @@ class RemoteBookViewModel(application: Application) : BaseViewModel(application)
val downloadBookUri = bookWebDav.downloadRemoteBook(remoteBook)
LocalBook.importFiles(downloadBookUri).forEach { book ->
book.origin = BookType.webDavTag + CustomUrl(remoteBook.path)
.putAttribute(
"serverID",
bookWebDav.serverID
).toString()
.putAttribute("serverID", bookWebDav.serverID)
.toString()
book.save()
}
remoteBook.isOnBookShelf = true
@ -152,7 +151,7 @@ class RemoteBookViewModel(application: Application) : BaseViewModel(application)
}
fun updateCallBackFlow(filterKey: String?) {
dataCallback?.screen(filterKey)
dataCallback?.screen(filterKey)
}
interface DataCallback {

View File

@ -194,10 +194,10 @@ object NetworkUtils {
enumeration = NetworkInterface.getNetworkInterfaces()
} catch (e: SocketException) {
e.printOnDebug()
return mutableListOf()
return emptyList()
}
var fallbackAddress: MutableList<InetAddress> = mutableListOf()
val addressList = mutableListOf<InetAddress>()
while (enumeration.hasMoreElements()) {
val nif = enumeration.nextElement()
@ -205,13 +205,11 @@ object NetworkUtils {
while (addresses.hasMoreElements()) {
val address = addresses.nextElement()
if (!address.isLoopbackAddress && isIPv4Address(address.hostAddress)) {
if (nif.name?.startsWith("wlan") == true) {
fallbackAddress.add(address)
}
addressList.add(address)
}
}
}
return fallbackAddress
return addressList
}
/**