mirror of
https://github.com/whitechi73/OpenShamrock.git
synced 2024-08-14 13:12:17 +08:00
ShamrockPublic
: キャッシュディレクトリへのファイルのアップロードをサポートする
Signed-off-by: WhiteChi <whitechi73@outlook.com>
This commit is contained in:
parent
e5cca58198
commit
d347bd0a41
@ -1,5 +1,6 @@
|
||||
package moe.fuqiuluo.shamrock.remote.action.handlers
|
||||
|
||||
import android.util.Base64
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import moe.fuqiuluo.shamrock.remote.action.ActionSession
|
||||
@ -13,7 +14,8 @@ import moe.fuqiuluo.shamrock.utils.MD5
|
||||
|
||||
internal object DownloadFile: IActionHandler() {
|
||||
override suspend fun internalHandle(session: ActionSession): String {
|
||||
val url = session.getString("url")
|
||||
val url = session.getStringOrNull("url")
|
||||
val b64 = session.getStringOrNull("base64")
|
||||
val threadCnt = session.getIntOrNull("thread_cnt") ?: 3
|
||||
val headers = if (session.isArray("headers")) {
|
||||
session.getArray("headers").map {
|
||||
@ -22,15 +24,17 @@ internal object DownloadFile: IActionHandler() {
|
||||
} else {
|
||||
session.getString("headers").split("\r\n")
|
||||
}
|
||||
return invoke(url, threadCnt, headers, session.echo)
|
||||
return invoke(url, b64, threadCnt, headers, session.echo)
|
||||
}
|
||||
|
||||
suspend operator fun invoke(
|
||||
url: String,
|
||||
url: String?,
|
||||
base64: String?,
|
||||
threadCnt: Int,
|
||||
headers: List<String>,
|
||||
echo: JsonElement = EmptyJsonString
|
||||
): String {
|
||||
if (url != null) {
|
||||
val headerMap = mutableMapOf(
|
||||
"User-Agent" to "Shamrock"
|
||||
)
|
||||
@ -42,6 +46,32 @@ internal object DownloadFile: IActionHandler() {
|
||||
}
|
||||
}
|
||||
return invoke(url, threadCnt, headerMap, echo)
|
||||
} else if (base64 != null) {
|
||||
return invoke(base64, echo)
|
||||
} else {
|
||||
return noParam("url/base64", echo)
|
||||
}
|
||||
}
|
||||
|
||||
operator fun invoke(
|
||||
base64: String,
|
||||
echo: JsonElement
|
||||
): String {
|
||||
kotlin.runCatching {
|
||||
val bytes = Base64.decode(base64, Base64.DEFAULT)
|
||||
FileUtils.getTmpFile("cache").also {
|
||||
it.writeBytes(bytes)
|
||||
}
|
||||
}.onSuccess {
|
||||
val tmp = FileUtils.renameByMd5(it)
|
||||
return ok(data = DownloadResult(
|
||||
file = tmp.absolutePath,
|
||||
md5 = MD5.genFileMd5Hex(tmp.absolutePath)
|
||||
), msg = "成功", echo = echo)
|
||||
}.onFailure {
|
||||
return logic("Base64格式错误", echo)
|
||||
}
|
||||
return logic("未知错误", echo)
|
||||
}
|
||||
|
||||
suspend operator fun invoke(
|
||||
@ -70,8 +100,6 @@ internal object DownloadFile: IActionHandler() {
|
||||
}
|
||||
}
|
||||
|
||||
override val requiredParams: Array<String> = arrayOf("url")
|
||||
|
||||
override fun path(): String = "download_file"
|
||||
|
||||
@Serializable
|
||||
|
@ -1,8 +1,13 @@
|
||||
package moe.fuqiuluo.shamrock.remote.api
|
||||
|
||||
import io.ktor.http.content.PartData
|
||||
import io.ktor.http.content.forEachPart
|
||||
import io.ktor.http.content.streamProvider
|
||||
import io.ktor.server.application.call
|
||||
import io.ktor.server.request.receiveMultipart
|
||||
import io.ktor.server.response.respondText
|
||||
import io.ktor.server.routing.Routing
|
||||
import io.ktor.server.routing.post
|
||||
import moe.fuqiuluo.shamrock.remote.action.handlers.CleanCache
|
||||
import moe.fuqiuluo.shamrock.remote.action.handlers.DownloadFile
|
||||
import moe.fuqiuluo.shamrock.remote.action.handlers.GetDeviceBattery
|
||||
@ -14,6 +19,8 @@ import moe.fuqiuluo.shamrock.tools.fetchOrNull
|
||||
import moe.fuqiuluo.shamrock.tools.fetchOrThrow
|
||||
import moe.fuqiuluo.shamrock.tools.getOrPost
|
||||
import moe.fuqiuluo.shamrock.tools.respond
|
||||
import moe.fuqiuluo.shamrock.utils.FileUtils
|
||||
import moe.fuqiuluo.shamrock.utils.MD5
|
||||
|
||||
fun Routing.otherAction() {
|
||||
|
||||
@ -34,10 +41,29 @@ fun Routing.otherAction() {
|
||||
}
|
||||
|
||||
getOrPost("/download_file") {
|
||||
val url = fetchOrThrow("url")
|
||||
val url = fetchOrNull("url")
|
||||
val b64 = fetchOrNull("base64")
|
||||
val threadCnt = fetchOrNull("thread_cnt")?.toInt() ?: 0
|
||||
val headers = fetchOrNull("headers") ?: ""
|
||||
call.respondText(DownloadFile(url, threadCnt, headers.split("\r\n")))
|
||||
call.respondText(DownloadFile(url, b64, threadCnt, headers.split("\r\n")))
|
||||
}
|
||||
|
||||
post("/upload_file") {
|
||||
val partData = call.receiveMultipart()
|
||||
partData.forEachPart { part ->
|
||||
if (part.name == "file") {
|
||||
val bytes = (part as PartData.FileItem).streamProvider().readBytes()
|
||||
val tmp = FileUtils.renameByMd5(FileUtils.getTmpFile("cache").also {
|
||||
it.writeBytes(bytes)
|
||||
})
|
||||
respond(true, Status.Ok, DownloadFile.DownloadResult(
|
||||
file = tmp.absolutePath,
|
||||
md5 = MD5.genFileMd5Hex(tmp.absolutePath)
|
||||
), "成功")
|
||||
return@forEachPart
|
||||
}
|
||||
}
|
||||
respond(false, Status.BadRequest, "没有上传文件信息")
|
||||
}
|
||||
|
||||
getOrPost("/config/set_boolean") {
|
||||
|
Loading…
x
Reference in New Issue
Block a user