Shamrock: 修复download_file指定名称失败

Signed-off-by: 白池 <whitechi73@outlook.com>
This commit is contained in:
白池 2024-02-25 17:31:57 +08:00
parent eb807a0332
commit ea4cf06edf
3 changed files with 45 additions and 5 deletions

View File

@ -49,7 +49,7 @@ internal object DownloadFile: IActionHandler() {
headerMap[k] = v headerMap[k] = v
} }
} }
return invoke(url, threadCnt, headerMap, echo) return invoke(url, threadCnt, headerMap, name, echo)
} else if (base64 != null) { } else if (base64 != null) {
return invoke(base64, name, echo) return invoke(base64, name, echo)
} else { } else {
@ -88,6 +88,7 @@ internal object DownloadFile: IActionHandler() {
url: String, url: String,
threadCnt: Int, threadCnt: Int,
headers: Map<String, String>, headers: Map<String, String>,
name: String?,
echo: JsonElement = EmptyJsonString echo: JsonElement = EmptyJsonString
): String { ): String {
return kotlin.runCatching { return kotlin.runCatching {
@ -100,7 +101,13 @@ internal object DownloadFile: IActionHandler() {
)) { )) {
return error("下载失败 (0x1)", echo) return error("下载失败 (0x1)", echo)
} }
tmp = FileUtils.renameByMd5(tmp) tmp = if (name == null) {
FileUtils.renameByMd5(tmp)
} else {
val newFile = tmp.parentFile!!.resolve(name)
tmp.renameTo(newFile)
newFile
}
ok(data = DownloadResult( ok(data = DownloadResult(
file = tmp.absolutePath, file = tmp.absolutePath,
md5 = MD5.genFileMd5Hex(tmp.absolutePath) md5 = MD5.genFileMd5Hex(tmp.absolutePath)

View File

@ -17,12 +17,12 @@ import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.serialization.SerialName import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement import kotlinx.serialization.json.JsonElement
import moe.fuqiuluo.qqinterface.servlet.transfile.RichMediaUploadHandler
import moe.fuqiuluo.shamrock.helper.LogCenter import moe.fuqiuluo.shamrock.helper.LogCenter
import moe.fuqiuluo.shamrock.helper.MessageHelper import moe.fuqiuluo.shamrock.helper.MessageHelper
import moe.fuqiuluo.shamrock.helper.TransfileHelper import moe.fuqiuluo.shamrock.helper.TransfileHelper
import moe.fuqiuluo.shamrock.remote.action.ActionSession import moe.fuqiuluo.shamrock.remote.action.ActionSession
import moe.fuqiuluo.shamrock.remote.action.IActionHandler import moe.fuqiuluo.shamrock.remote.action.IActionHandler
import moe.fuqiuluo.qqinterface.servlet.transfile.RichMediaUploadHandler
import moe.fuqiuluo.shamrock.tools.EmptyJsonString import moe.fuqiuluo.shamrock.tools.EmptyJsonString
import moe.fuqiuluo.shamrock.utils.FileUtils import moe.fuqiuluo.shamrock.utils.FileUtils
import moe.fuqiuluo.shamrock.utils.MD5 import moe.fuqiuluo.shamrock.utils.MD5
@ -55,6 +55,20 @@ internal object UploadGroupFile : IActionHandler() {
if (!srcFile.exists()) { if (!srcFile.exists()) {
srcFile = FileUtils.getFile(file) srcFile = FileUtils.getFile(file)
} }
if (!srcFile.exists()) {
srcFile = file.let {
val md5 = it.replace(
regex = "[{}\\-]".toRegex(),
replacement = ""
).split(".")[0].lowercase()
if (md5.length == 32) {
FileUtils.getFileByMd5(it)
} else {
FileUtils.parseAndSave(it)
}
}
}
if (!srcFile.exists()) { if (!srcFile.exists()) {
return badParam("文件不存在", echo) return badParam("文件不存在", echo)
} }
@ -64,6 +78,7 @@ internal object UploadGroupFile : IActionHandler() {
fileElement.fileName = name fileElement.fileName = name
fileElement.filePath = srcFile.absolutePath fileElement.filePath = srcFile.absolutePath
fileElement.fileSize = srcFile.length() fileElement.fileSize = srcFile.length()
fileElement.folderId = srcFile.parent ?: ""
fileElement.picWidth = 0 fileElement.picWidth = 0
fileElement.picHeight = 0 fileElement.picHeight = 0
fileElement.videoDuration = 0 fileElement.videoDuration = 0

View File

@ -53,6 +53,21 @@ internal object UploadPrivateFile : IActionHandler() {
if (!srcFile.exists()) { if (!srcFile.exists()) {
srcFile = FileUtils.getFile(file) srcFile = FileUtils.getFile(file)
} }
if (!srcFile.exists()) {
srcFile = file.let {
val md5 = it.replace(
regex = "[{}\\-]".toRegex(),
replacement = ""
).split(".")[0].lowercase()
if (md5.length == 32) {
FileUtils.getFileByMd5(it)
} else {
FileUtils.parseAndSave(it)
}
}
}
if (!srcFile.exists()) { if (!srcFile.exists()) {
return badParam("文件不存在", echo) return badParam("文件不存在", echo)
} }
@ -62,6 +77,7 @@ internal object UploadPrivateFile : IActionHandler() {
fileElement.fileName = name fileElement.fileName = name
fileElement.filePath = srcFile.absolutePath fileElement.filePath = srcFile.absolutePath
fileElement.fileSize = srcFile.length() fileElement.fileSize = srcFile.length()
fileElement.folderId = srcFile.parent ?: ""
fileElement.picWidth = 0 fileElement.picWidth = 0
fileElement.picHeight = 0 fileElement.picHeight = 0
fileElement.videoDuration = 0 fileElement.videoDuration = 0
@ -108,9 +124,11 @@ internal object UploadPrivateFile : IActionHandler() {
msgService.sendMsgWithMsgId( msgService.sendMsgWithMsgId(
contact, msgIdPair.qqMsgId, arrayListOf(msgElement) contact, msgIdPair.qqMsgId, arrayListOf(msgElement)
) { code, reason -> ) { code, reason ->
if (code != 0) {
LogCenter.log("私聊文件消息发送异常(code = $code, reason = $reason)") LogCenter.log("私聊文件消息发送异常(code = $code, reason = $reason)")
it.resume(null) it.resume(null)
} }
}
RichMediaUploadHandler.registerListener(msgIdPair.qqMsgId) { RichMediaUploadHandler.registerListener(msgIdPair.qqMsgId) {
it.resume(this) it.resume(this)
return@registerListener true return@registerListener true