Skip to content

Commit 0a494b8

Browse files
committed
fix: download
1 parent aee11f2 commit 0a494b8

1 file changed

Lines changed: 29 additions & 37 deletions

File tree

  • src/main/kotlin/io/github/gnuf0rce/mirai/netdisk

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

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

33
import io.github.gnuf0rce.mirai.netdisk.data.*
44
import io.ktor.client.*
5+
import io.ktor.client.engine.okhttp.*
56
import io.ktor.client.plugins.*
7+
import io.ktor.client.plugins.compression.*
68
import io.ktor.client.request.*
79
import io.ktor.client.statement.*
810
import io.ktor.http.*
@@ -139,7 +141,7 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
139141
val slice = if (size <= SLICE_SIZE) {
140142
content
141143
} else {
142-
val slice = download(urlString = url, range = 0L until SLICE_SIZE.toLong())
144+
val slice = download(urlString = url, range = 0L until SLICE_SIZE.toLong()).body<ByteArray>()
143145
slice.md5().toUHexString("").lowercase()
144146
}
145147
RapidUploadInfo(
@@ -170,7 +172,7 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
170172

171173
if (file.size < limit) {
172174
try {
173-
val bytes = download(urlString = url, range = null)
175+
val bytes = download(urlString = url).body<ByteArray>()
174176
pcs.upload(path = rapid.path, ondup = OnDupType.NEW_COPY, size = bytes.size.toLong()) {
175177
writeFully(bytes)
176178
}
@@ -189,33 +191,24 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
189191
}
190192
val uploadId = requireNotNull(prepare.uploadId) { prepare }
191193

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()
212-
213-
temp.md5
194+
val blocks = download(urlString = url).execute { response ->
195+
val channel = response.bodyAsChannel()
196+
val capacity = (file.size / limit + 1).toInt()
197+
List(capacity) { index ->
198+
val packet = channel.readRemaining(limit)
199+
supervisorScope {
200+
async {
201+
val size = packet.remaining.toInt()
202+
val temp = pcs.temp(path = rapid.path, id = uploadId, index = index, size = size) {
203+
writePacket(packet)
214204
}
205+
packet.close()
206+
207+
temp.md5
215208
}
216209
}
217-
}.awaitAll()
218-
}
210+
}
211+
}.awaitAll()
219212

220213
val merge = MergeFileInfo(
221214
blocks = blocks.toMutableList(),
@@ -229,7 +222,8 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
229222
return rapid
230223
}
231224

232-
override val client: HttpClient = super.client.config {
225+
private val downloader: HttpClient = HttpClient(OkHttp) {
226+
ContentEncoding()
233227
BrowserUserAgent()
234228
// FIXME: MalformedInputException
235229
expectSuccess = NetdiskUploadConfig.https.not()
@@ -243,19 +237,17 @@ public object NetDisk : BaiduNetDiskClient(config = NetdiskOauthConfig), Listene
243237
}
244238
}
245239

246-
private suspend fun download(urlString: String, range: LongRange?): ByteArray {
240+
private suspend fun download(urlString: String, range: LongRange? = null): HttpStatement {
247241
val fragment = range?.run { "bytes=${start}-${endInclusive}" }
248242
logger.verbose { "download $urlString#$fragment" }
249-
return useHttpClient { client ->
250-
client.prepareGet(urlString) {
251-
url {
252-
if (NetdiskUploadConfig.https) {
253-
protocol = URLProtocol.HTTPS
254-
host = "gzc-download.ftn.qq.com"
255-
}
243+
return downloader.prepareGet(urlString) {
244+
url {
245+
if (NetdiskUploadConfig.https) {
246+
protocol = URLProtocol.HTTPS
247+
host = "gzc-download.ftn.qq.com"
256248
}
257-
header(HttpHeaders.Range, fragment)
258-
}.body()
249+
}
250+
header(HttpHeaders.Range, fragment)
259251
}
260252
}
261253
}

0 commit comments

Comments
 (0)