Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

加入文件操作 #294

Merged
merged 45 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
b302211
加入文件信息识别
Doctor-Yin Mar 26, 2021
56fa422
加入获取指定目录下文件/目录
Doctor-Yin Mar 27, 2021
0268aee
加入获取指定目录下文件/目录
Doctor-Yin Mar 27, 2021
be0682e
Merge remote-tracking branch 'origin/master'
Doctor-Yin Mar 27, 2021
f78e7be
加入获取指定文件信息
Doctor-Yin Mar 27, 2021
4fe3364
Recompile with mirai 2.5.1-dev-1
Doctor-Yin Mar 27, 2021
d677764
还原
Doctor-Yin Mar 27, 2021
4428689
修复
Doctor-Yin Mar 27, 2021
0d99156
整理
Doctor-Yin Mar 27, 2021
982ca47
整理
Doctor-Yin Mar 27, 2021
71044e7
整理
Doctor-Yin Mar 27, 2021
cdebfa2
修改path路径为groupFile...
Doctor-Yin Mar 27, 2021
252e06b
添加群文件名字修改
Doctor-Yin Mar 27, 2021
e87f0aa
添加群文件移动
Doctor-Yin Mar 27, 2021
ec2f1a5
添加群文件/目录删除
Doctor-Yin Mar 27, 2021
304cd62
Merge remote-tracking branch 'upstream/master'
Doctor-Yin Mar 27, 2021
3644373
处理合并
Doctor-Yin Mar 27, 2021
a71158f
添加群文件发送
Doctor-Yin Mar 27, 2021
ae90641
添加群文件发送
Doctor-Yin Mar 27, 2021
91d8ab7
写了转发文件的TODO
Doctor-Yin Mar 29, 2021
69a976f
Update mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/c…
Doctor-Yin Mar 29, 2021
9773915
Update mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/c…
Doctor-Yin Mar 29, 2021
e13e80d
Update mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/…
Doctor-Yin Mar 29, 2021
4b882e3
更新建议
Doctor-Yin Mar 29, 2021
700bd8b
更新建议
Doctor-Yin Mar 29, 2021
437ab40
更新建议
Doctor-Yin Mar 29, 2021
fe7056d
更新建议
Doctor-Yin Mar 29, 2021
50b5869
更新建议
Doctor-Yin Mar 29, 2021
d73ab7d
更新建议
Doctor-Yin Mar 29, 2021
bf99cfe
Merge remote-tracking branch 'upstream/master'
Doctor-Yin Mar 30, 2021
f71cdcc
更新建议
Doctor-Yin Mar 30, 2021
bd124f3
Update mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/…
Doctor-Yin Mar 30, 2021
2d90293
更新建议(core的锅)
Doctor-Yin Mar 30, 2021
c32e914
更新API
Doctor-Yin Mar 30, 2021
b70cfa4
添加移动目录时自动创建目录,更新API
Doctor-Yin Mar 30, 2021
a27953a
添加移动目录时自动创建目录,更新API
Doctor-Yin Mar 30, 2021
3a94ef2
Update docs/API.md
Doctor-Yin Mar 30, 2021
c62f516
更新建议
Doctor-Yin Mar 30, 2021
f708084
更新API
Doctor-Yin Mar 30, 2021
1dc3ed6
还原API文档
Doctor-Yin Apr 1, 2021
3dad79d
还原API文档
Doctor-Yin Apr 1, 2021
8bc8311
更新API文档
Doctor-Yin Apr 1, 2021
e1f5023
解决合并
Doctor-Yin Apr 2, 2021
6410f84
解决合并
Doctor-Yin Apr 2, 2021
19877f3
Update API.md
Karlatemp Apr 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mirai-api-http/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id("kotlinx-serialization")
kotlin("jvm")
id("net.mamoe.mirai-console") version "2.5.0"
id("net.mamoe.mirai-console") version "2.5.1-dev-1"
}

val httpVersion: String by rootProject.ext
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2020-2021 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/

package net.mamoe.mirai.api.http.data.common

import kotlinx.serialization.Serializable
import net.mamoe.mirai.utils.RemoteFile
import net.mamoe.mirai.utils.RemoteFile.DownloadInfo
import net.mamoe.mirai.utils.RemoteFile.FileInfo
import net.mamoe.mirai.utils.toUHexString

@Serializable
sealed class FileDTO : DTO {
abstract val name: String

}

@Serializable
data class RemoteFileDTO(
override val name: String,
val id: String?,
val path: String,
val type: String

) : FileDTO() {
constructor(remoteFile: RemoteFile, isFile: Boolean) : this(
remoteFile.name,
remoteFile.id,
remoteFile.path,
if (isFile) "File"
else "Dir"
)

}

@Serializable
data class FileInfoDTO(
override val name: String,
val id: String?,
val path: String,
val length: Long,
val downloadTimes: Int,
val uploaderId: Long,
val uploadTime: Long,
val lastModifyTime: Long,
val downloadUrl: String,
val sha1: String,
val md5: String,
) : FileDTO() {
constructor(fileInfo: FileInfo, file: DownloadInfo) : this(
fileInfo.name,
fileInfo.id,
fileInfo.path,
fileInfo.length,
fileInfo.downloadTimes,
fileInfo.uploaderId,
fileInfo.uploadTime,
fileInfo.lastModifyTime,
file.url,
fileInfo.sha1.toUHexString(""),
fileInfo.md5.toUHexString(""),

)
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@ data class PokeMessageDTO(
val name: String
) : MessageDTO()

@Serializable
@SerialName("File")
data class FileMessageDTO(
val id: String,
val internalId: Int,
val name: String,
val size: Long
) : MessageDTO()

@Serializable
@SerialName("Unknown")
object UnknownMessageDTO : MessageDTO()
Expand Down Expand Up @@ -191,6 +200,7 @@ suspend fun Message.toDTO() = when (this) {
},
origin = (source.originalMessage + source).toMessageChainDTO { it != UnknownMessageDTO })
is PokeMessage -> PokeMessageDTO(PokeMap[pokeType])
is FileMessage -> FileMessageDTO(id, internalId, name, size)
else -> UnknownMessageDTO
}

Expand Down Expand Up @@ -227,7 +237,9 @@ suspend fun MessageDTO.toMessage(contact: Contact) = when (this) {
is VoiceDTO -> when {
contact !is Group -> null
!voiceId.isNullOrBlank() -> Voice(voiceId, voiceId.substringBefore(".").toHexArray(), 0, 0, "")
!url.isNullOrBlank() -> withContext(Dispatchers.IO) { URL(url).openStream().toExternalResource().uploadAsVoice(contact) }
!url.isNullOrBlank() -> withContext(Dispatchers.IO) {
URL(url).openStream().toExternalResource().uploadAsVoice(contact)
}
!path.isNullOrBlank() -> with(HttpApiPluginBase.voice(path)) {
if (exists()) {
inputStream().toExternalResource().uploadAsVoice(contact)
Expand All @@ -242,6 +254,7 @@ suspend fun MessageDTO.toMessage(contact: Contact) = when (this) {
// ignore
is QuoteDTO,
is MessageSourceDTO,
is FileMessageDTO,
is UnknownMessageDTO
-> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ internal inline fun <reified R> PipelineContext<Unit, ApplicationCall>.paramOrNu
}

String::class -> call.parameters[name]

UByte::class -> call.parameters[name]?.toUByte()
UInt::class -> call.parameters[name]?.toUInt()
UShort::class -> call.parameters[name]?.toUShort()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ package net.mamoe.mirai.api.http.route

import io.ktor.application.*
import io.ktor.routing.*
import net.mamoe.mirai.api.http.data.common.GroupDTO
import net.mamoe.mirai.api.http.data.common.MemberDTO
import net.mamoe.mirai.api.http.data.common.QQDTO
import kotlinx.coroutines.flow.toList
import net.mamoe.mirai.api.http.data.common.*
import net.mamoe.mirai.api.http.util.toJson

/**
Expand Down Expand Up @@ -46,6 +45,41 @@ fun Application.infoModule() {
call.respondJson(ls.toJson())
}

/**
* 查询群文件列表
*/
miraiGet("/fileList") { it ->
val dir: String = call.parameters["dir"].orEmpty()
val ls = it.bot.getGroupOrFail(paramOrNull("target")).filesRoot.let {
if (dir.isEmpty()) it.listFiles()
.toList().map { remoteFile ->
RemoteFileDTO(remoteFile, remoteFile.isFile())
}
else it.resolve("/$dir").listFiles()
.toList().map { remoteFile ->
RemoteFileDTO(remoteFile, true)
}
}

call.respondJson(ls.toJson())
}

/**
* 获取文件详细信息
*/
miraiGet("/fileInfo") {
val id: String = paramOrNull("id")
val fileInfo = it.bot.getGroupOrFail(paramOrNull("target")).filesRoot.resolveById(id)
?: throw error("文件ID $id 不存在")

call.respondJson(
FileInfoDTO(
fileInfo.getInfo()!!,
fileInfo.getDownloadInfo() ?: throw error("文件ID $id 是一个目录")
).toJson()
)
}

// /**
// * 查询机器人个人信息
// */
Expand Down