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
}
}
return invoke(url, threadCnt, headerMap, echo)
return invoke(url, threadCnt, headerMap, name, echo)
} else if (base64 != null) {
return invoke(base64, name, echo)
} else {
@ -88,6 +88,7 @@ internal object DownloadFile: IActionHandler() {
url: String,
threadCnt: Int,
headers: Map<String, String>,
name: String?,
echo: JsonElement = EmptyJsonString
): String {
return kotlin.runCatching {
@ -100,7 +101,13 @@ internal object DownloadFile: IActionHandler() {
)) {
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(
file = tmp.absolutePath,
md5 = MD5.genFileMd5Hex(tmp.absolutePath)

View File

@ -17,12 +17,12 @@ import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import moe.fuqiuluo.qqinterface.servlet.transfile.RichMediaUploadHandler
import moe.fuqiuluo.shamrock.helper.LogCenter
import moe.fuqiuluo.shamrock.helper.MessageHelper
import moe.fuqiuluo.shamrock.helper.TransfileHelper
import moe.fuqiuluo.shamrock.remote.action.ActionSession
import moe.fuqiuluo.shamrock.remote.action.IActionHandler
import moe.fuqiuluo.qqinterface.servlet.transfile.RichMediaUploadHandler
import moe.fuqiuluo.shamrock.tools.EmptyJsonString
import moe.fuqiuluo.shamrock.utils.FileUtils
import moe.fuqiuluo.shamrock.utils.MD5
@ -55,6 +55,20 @@ internal object UploadGroupFile : IActionHandler() {
if (!srcFile.exists()) {
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()) {
return badParam("文件不存在", echo)
}
@ -64,6 +78,7 @@ internal object UploadGroupFile : IActionHandler() {
fileElement.fileName = name
fileElement.filePath = srcFile.absolutePath
fileElement.fileSize = srcFile.length()
fileElement.folderId = srcFile.parent ?: ""
fileElement.picWidth = 0
fileElement.picHeight = 0
fileElement.videoDuration = 0

View File

@ -53,6 +53,21 @@ internal object UploadPrivateFile : IActionHandler() {
if (!srcFile.exists()) {
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()) {
return badParam("文件不存在", echo)
}
@ -62,6 +77,7 @@ internal object UploadPrivateFile : IActionHandler() {
fileElement.fileName = name
fileElement.filePath = srcFile.absolutePath
fileElement.fileSize = srcFile.length()
fileElement.folderId = srcFile.parent ?: ""
fileElement.picWidth = 0
fileElement.picHeight = 0
fileElement.videoDuration = 0
@ -108,8 +124,10 @@ internal object UploadPrivateFile : IActionHandler() {
msgService.sendMsgWithMsgId(
contact, msgIdPair.qqMsgId, arrayListOf(msgElement)
) { code, reason ->
LogCenter.log("私聊文件消息发送异常(code = $code, reason = $reason)")
it.resume(null)
if (code != 0) {
LogCenter.log("私聊文件消息发送异常(code = $code, reason = $reason)")
it.resume(null)
}
}
RichMediaUploadHandler.registerListener(msgIdPair.qqMsgId) {
it.resume(this)