Skip to content

Commit aee11f2

Browse files
committed
update: baidu sdk 3.2.0
1 parent 3bd1ac2 commit aee11f2

4 files changed

Lines changed: 53 additions & 68 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ repositories {
2626
}
2727

2828
dependencies {
29-
api("xyz.cssxsh.baidu:baidu-netdisk:3.1.6") {
29+
api("xyz.cssxsh.baidu:baidu-netdisk:3.2.0") {
3030
exclude(group = "org.jetbrains.kotlin")
3131
exclude(group = "org.jetbrains.kotlinx")
3232
exclude(group = "org.slf4j")

src/main/kotlin/io/github/gnuf0rce/mirai/netdisk/NetDisk.kt

Lines changed: 48 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
133133
}
134134

135135
private suspend fun uploadAbsoluteFile(file: AbsoluteFile): RapidUploadInfo {
136-
val url = requireNotNull(file.getUrl()) { "远程文件不存在" }
136+
val url = requireNotNull(file.getUrl()) { "远程文件 URL 获取失败" }
137137
val rapid = with(file) {
138138
val content = file.md5.toUHexString("").lowercase()
139139
val slice = if (size <= SLICE_SIZE) {
@@ -151,27 +151,29 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
151151
}
152152

153153
// 用群号做根目录
154-
createDir(path = "${file.contact.id}")
154+
mkdir(path = "${file.contact.id}")
155155
logger.info { "upload ${rapid.format()}" }
156156

157157
// 尝试秒传
158158
try {
159-
rapidUploadFile(info = rapid)
159+
rapid(upload = rapid)
160160
return rapid
161161
} catch (throwable: IllegalArgumentException) {
162162
logger.info { "文件 ${file.name} 秒传失败, 进入文件上传, ${throwable.message}" }
163163
} catch (exception: Throwable) {
164-
logger.info({ "文件 ${file.name} 秒传失败, 进入文件上传" }, exception)
164+
logger.warning({ "文件 ${file.name} 秒传失败, 进入文件上传" }, exception)
165165
}
166166

167-
val user = getUserInfo()
167+
val user = rest.user()
168168
check(file.size <= user.vip.updateLimit) { "${file.contact}-${file.name} 超过了文件上传极限" }
169169
val limit = user.vip.superLimit.toLong()
170170

171171
if (file.size < limit) {
172172
try {
173-
val bytes = download(urlString = url, 0 until file.size)
174-
uploadSingleFile(path = rapid.path, bytes = bytes, size = file.size.toInt())
173+
val bytes = download(urlString = url, range = null)
174+
pcs.upload(path = rapid.path, ondup = OnDupType.NEW_COPY, size = bytes.size.toLong()) {
175+
writeFully(bytes)
176+
}
175177
return rapid
176178
} catch (throwable: ClientRequestException) {
177179
logger.info { "文件 ${file.name} 单文件上传失败, 进入文件上传, ${throwable.message}" }
@@ -181,34 +183,49 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
181183
}
182184

183185

184-
val uploadId = with(
185-
preCreate(
186-
path = rapid.path,
187-
size = file.size,
188-
isDir = false,
189-
blocks = listOf("5910a591dd8fc18c32a8f3df4fdc1761", "a5fc157d78e6ad1c7e114b056c92821e"),
190-
rename = RenameType.PATH
191-
)
192-
) {
193-
if (type == CreateReturnType.EXIST) {
194-
return rapid
195-
} else {
196-
check(uploadId.isNotEmpty()) { this }
197-
uploadId
198-
}
186+
val prepare = rest.prepare(upload = rapid, blocks = LAZY_BLOCKS, ondup = OnDupType.NEW_COPY)
187+
if (prepare.type == PrepareReturnType.EXIST) {
188+
return rapid
199189
}
190+
val uploadId = requireNotNull(prepare.uploadId) { prepare }
200191

201-
val blocks = blocks(urlString = url, limit = limit, path = rapid.path, uploadId = uploadId)
192+
val blocks = useHttpClient { client ->
193+
client.prepareGet(url) {
194+
url {
195+
if (NetdiskUploadConfig.https) {
196+
protocol = URLProtocol.HTTPS
197+
host = "gzc-download.ftn.qq.com"
198+
}
199+
}
200+
}.execute { response ->
201+
val channel = response.bodyAsChannel()
202+
val capacity = (file.size / limit + 1).toInt()
203+
List(capacity) { index ->
204+
val packet = channel.readRemaining(limit)
205+
supervisorScope {
206+
async {
207+
val size = packet.remaining.toInt()
208+
val temp = pcs.temp(path = rapid.path, id = uploadId, index = index, size = size) {
209+
writePacket(packet)
210+
}
211+
packet.close()
202212

203-
createFile(
204-
path = rapid.path,
205-
size = file.size,
206-
isDir = false,
207-
blocks = blocks,
213+
temp.md5
214+
}
215+
}
216+
}
217+
}.awaitAll()
218+
}
219+
220+
val merge = MergeFileInfo(
221+
blocks = blocks.toMutableList(),
208222
uploadId = uploadId,
209-
rename = RenameType.PATH
223+
size = file.size,
224+
path = rapid.path
210225
)
211226

227+
rest.create(merge = merge, ondup = OnDupType.NEW_COPY)
228+
212229
return rapid
213230
}
214231

@@ -226,8 +243,8 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
226243
}
227244
}
228245

229-
private suspend fun download(urlString: String, range: LongRange): ByteArray {
230-
val fragment = range.run { "bytes=${start}-${endInclusive}" }
246+
private suspend fun download(urlString: String, range: LongRange?): ByteArray {
247+
val fragment = range?.run { "bytes=${start}-${endInclusive}" }
231248
logger.verbose { "download $urlString#$fragment" }
232249
return useHttpClient { client ->
233250
client.prepareGet(urlString) {
@@ -241,35 +258,4 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
241258
}.body()
242259
}
243260
}
244-
245-
private suspend fun blocks(urlString: String, limit: Long, path: String, uploadId: String): List<String> {
246-
return useHttpClient { client ->
247-
client.prepareGet(urlString) {
248-
url {
249-
if (NetdiskUploadConfig.https) {
250-
protocol = URLProtocol.HTTPS
251-
host = "gzc-download.ftn.qq.com"
252-
}
253-
}
254-
}.execute { response ->
255-
val channel = response.bodyAsChannel()
256-
val capacity = ((response.contentLength() ?: 0) / limit + 1).toInt()
257-
List(capacity) { index ->
258-
val packet = channel.readRemaining(limit)
259-
val bytes = packet.readBytes()
260-
supervisorScope {
261-
async {
262-
superFile(
263-
path = path,
264-
uploadId = uploadId,
265-
index = index,
266-
data = bytes,
267-
size = bytes.size
268-
).md5
269-
}
270-
}
271-
}
272-
}.awaitAll()
273-
}
274-
}
275261
}

src/main/kotlin/io/github/gnuf0rce/mirai/netdisk/command/BaiduOAuthCommand.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import net.mamoe.mirai.console.util.*
88
import net.mamoe.mirai.message.data.*
99
import net.mamoe.mirai.message.*
1010
import net.mamoe.mirai.utils.*
11-
import xyz.cssxsh.baidu.disk.*
1211

1312
internal object BaiduOAuthCommand : CompositeCommand(
1413
owner = NetDiskFileSyncPlugin,
@@ -31,7 +30,7 @@ internal object BaiduOAuthCommand : CompositeCommand(
3130
authorize { url ->
3231
sendMessage("请打开连接,然后在十分钟内输入获得的认证码, $url")
3332
read()
34-
} to getUserInfo()
33+
} to rest.user()
3534
}.onSuccess { (token, user) ->
3635
logger.info { "百度云用户认证成功, ${user.baiduName} by $token" }
3736
sendMessage("百度云用户认证成功, ${user.baiduName} by $token")
@@ -45,7 +44,7 @@ internal object BaiduOAuthCommand : CompositeCommand(
4544
suspend fun CommandSender.refresh(token: String) {
4645
NetdiskUserData.refreshTokenValue = token
4746
NetDisk.runCatching {
48-
refresh() to getUserInfo()
47+
refresh() to rest.user()
4948
}.onSuccess { (token, user) ->
5049
logger.info { "百度云用户认证成功, ${user.baiduName} by $token" }
5150
sendMessage("百度云用户认证成功, ${user.baiduName} by $token")

src/main/kotlin/io/github/gnuf0rce/mirai/netdisk/data/NetdiskOauthConfig.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package io.github.gnuf0rce.mirai.netdisk.data
22

33
import net.mamoe.mirai.console.data.*
4-
import xyz.cssxsh.baidu.oauth.*
4+
import xyz.cssxsh.baidu.disk.*
55

6-
internal object NetdiskOauthConfig : ReadOnlyPluginConfig("oauth"), BaiduAuthConfig {
6+
internal object NetdiskOauthConfig : ReadOnlyPluginConfig("oauth"), BaiduNetDiskConfig {
77
@ValueName("app_id")
88
override val appId: Long by value(0L)
99

0 commit comments

Comments
 (0)