Shamrock: fix #248

Signed-off-by: 白池 <whitechi73@outlook.com>
This commit is contained in:
白池 2024-02-21 21:31:59 +08:00
parent c940aea153
commit 75633f78c4
10 changed files with 77 additions and 12 deletions

View File

@ -678,7 +678,7 @@ internal object MsgElementMaker {
val file = data["file"].asString.let {
val md5 = it.replace(regex = "[{}\\-]".toRegex(), replacement = "").split(".")[0].lowercase()
var file = if (md5.length == 32) {
FileUtils.getFile(it)
FileUtils.getFileByMd5(it)
} else {
FileUtils.parseAndSave(it)
}
@ -909,9 +909,11 @@ internal object MsgElementMaker {
val url = data["url"].asStringOrNull
var file: File? = null
if (filePath != null) {
val md5 = filePath.replace(regex = "[{}\\-]".toRegex(), replacement = "").split(".")[0].lowercase()
val md5 = filePath
.replace(regex = "[{}\\-]".toRegex(), replacement = "")
.split(".")[0].lowercase()
file = if (md5.length == 32) {
FileUtils.getFile(md5)
FileUtils.getFileByMd5(md5)
} else {
FileUtils.parseAndSave(filePath)
}

View File

@ -17,7 +17,7 @@ internal object LocalCacheHelper: BaseSvc() {
}
fun getCachePttFile(md5: String): File {
val file = FileUtils.getFile(md5)
val file = FileUtils.getFileByMd5(md5)
return if (file.exists()) file else getCurrentPttPath().resolve("$md5.amr")
}
}

View File

@ -43,7 +43,7 @@ internal abstract class IActionHandler {
return resultToString(true, Status.Ok, EmptyObject, msg, echo = echo)
}
protected inline fun <reified T> ok(data: T, echo: JsonElement = EmptyJsonString, msg: String = ""): String {
protected inline fun <reified T> ok(data: T, echo: JsonElement, msg: String = ""): String {
return resultToString(true, Status.Ok, data!!, msg, echo = echo)
}

View File

@ -40,7 +40,7 @@ internal object FavAddImageMsg: IActionHandler() {
val image = fileText.let {
val md5 = it.replace(regex = "[{}\\-]".toRegex(), replacement = "").split(".")[0].lowercase()
if (md5.length == 32) {
FileUtils.getFile(it)
FileUtils.getFileByMd5(it)
} else {
FileUtils.parseAndSave(it)
}

View File

@ -52,7 +52,7 @@ internal object FavGetItemContent: IActionHandler() {
resp.getFavContentResp!!.content!!.joinToString("") {
String(it.richMedia!!.rawData!!)
}
))
), echo)
}
override val requiredParams: Array<String> = arrayOf("id")

View File

@ -63,7 +63,7 @@ internal object GetGuildMemberList: IActionHandler() {
members = members,
finish = nextToken.finish,
nextToken = ProtoBuf.encodeToByteArray(nextToken).toHexString(),
))
), echo)
}
override val requiredParams: Array<String> = arrayOf("guild_id")

View File

@ -0,0 +1,59 @@
package moe.fuqiuluo.shamrock.remote.action.handlers
import android.util.Base64
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import moe.fuqiuluo.shamrock.remote.action.ActionSession
import moe.fuqiuluo.shamrock.remote.action.IActionHandler
import moe.fuqiuluo.shamrock.tools.EmptyJsonString
import moe.fuqiuluo.shamrock.tools.hex2ByteArray
import moe.fuqiuluo.shamrock.tools.toHexString
import moe.fuqiuluo.shamrock.utils.FileUtils
import moe.fuqiuluo.symbols.OneBotHandler
import java.io.RandomAccessFile
@OneBotHandler("upload_file_to_shamrock")
internal object UploadFileToShamrock: IActionHandler() {
override suspend fun internalHandle(session: ActionSession): String {
val md5 = session.getString("md5").hex2ByteArray()
val offset = session.getStringOrNull("offset")?.toULong() ?: 0uL
val chunk = Base64.decode(session.getString("chunk"), Base64.DEFAULT)
val fileSize = session.getStringOrNull("file_size")?.toULong() ?: chunk.size.toULong()
return invoke(md5, fileSize, offset, chunk, session.echo)
}
operator fun invoke(
md5: ByteArray,
fileSize: ULong,
offset: ULong,
chunk: ByteArray,
echo: JsonElement = EmptyJsonString
): String {
val file = FileUtils.getFileByMd5(md5.toHexString())
runCatching {
if (!file.exists()) {
file.createNewFile()
}
val rd = RandomAccessFile(file, "rw")
rd.setLength(fileSize.toLong())
rd.seek(offset.toLong())
rd.write(chunk, 0, chunk.size)
rd.close()
}.onFailure {
return error(it.message ?: it.toString(), echo)
}
return ok(UploadFileResult(
fileSize = fileSize,
isFinish = fileSize <= offset + chunk.size.toULong(),
filePath = file.absolutePath
), echo = echo)
}
@Serializable
data class UploadFileResult(
@SerialName("file_size") val fileSize: ULong,
@SerialName("finish") val isFinish: Boolean,
@SerialName("path") val filePath: String
)
}

View File

@ -81,7 +81,7 @@ internal object UploadGroupFile : IActionHandler() {
fileElement.picThumbPath[750] = srcFile.absolutePath
}
2 -> {
val thumbPic = FileUtils.getFile(MD5.genFileMd5Hex(srcFile.absolutePath))
val thumbPic = FileUtils.getFileByMd5(MD5.genFileMd5Hex(srcFile.absolutePath))
withContext(Dispatchers.IO) {
val fileOutputStream = FileOutputStream(thumbPic)
val retriever = MediaMetadataRetriever()

View File

@ -82,7 +82,7 @@ internal object UploadPrivateFile : IActionHandler() {
fileElement.picThumbPath[750] = srcFile.absolutePath
}
2 -> {
val thumbPic = FileUtils.getFile(MD5.genFileMd5Hex(srcFile.absolutePath))
val thumbPic = FileUtils.getFileByMd5(MD5.genFileMd5Hex(srcFile.absolutePath))
withContext(Dispatchers.IO) {
val fileOutputStream = FileOutputStream(thumbPic)
val retriever = MediaMetadataRetriever()

View File

@ -94,8 +94,7 @@ internal object FileUtils {
fun getFile(name: String): File {
if (name.length == 32) {
// 使用md5获取值
val dirName = name.substring(name.length - 4)
val file = CacheDir.resolve("$dirName/$name")
val file = getFileByMd5(name)
if (file.exists()) {
return file
}
@ -103,6 +102,11 @@ internal object FileUtils {
return CacheDir.resolve(name)
}
fun getFileByMd5(md5: String): File {
val dirName = md5.substring(md5.length - 4)
return CacheDir.resolve("$dirName/$md5")
}
fun getFileType(file: File): String {
val bytes = ByteArray(2)
file.inputStream().use {