diff --git a/app/src/main/java/com/donut/mixfile/server/core/routes/DownloadRoute.kt b/app/src/main/java/com/donut/mixfile/server/core/routes/DownloadRoute.kt index c336b14..7c88d8d 100644 --- a/app/src/main/java/com/donut/mixfile/server/core/routes/DownloadRoute.kt +++ b/app/src/main/java/com/donut/mixfile/server/core/routes/DownloadRoute.kt @@ -5,6 +5,7 @@ import com.donut.mixfile.server.core.httpClient import com.donut.mixfile.server.core.utils.SortedTask import com.donut.mixfile.server.core.utils.bean.MixShareInfo import com.donut.mixfile.server.core.utils.encodeURL +import com.donut.mixfile.server.core.utils.parseFileMimeType import com.donut.mixfile.server.core.utils.resolveMixShareInfo import io.ktor.http.ContentType import io.ktor.http.HttpStatusCode @@ -28,9 +29,8 @@ import kotlin.io.encoding.ExperimentalEncodingApi @OptIn(ExperimentalEncodingApi::class) fun MixFileServer.getDownloadRoute(): RoutingHandler { return route@{ - val shareInfoData = call.parameters["s"] - val referer = call.parameters["referer"] - var name = call.parameters["name"] + val param = call.parameters + val shareInfoData = param["s"] if (shareInfoData == null) { call.respondText("分享信息为空", status = HttpStatusCode.InternalServerError) return@route @@ -48,9 +48,12 @@ fun MixFileServer.getDownloadRoute(): RoutingHandler { ) return@route } - if (name.isNullOrEmpty()) { - name = shareInfo.fileName - } + + val referer = + (param["referer"] ?: "").ifBlank { shareInfo.referer } + val name = + (param["name"] ?: "").ifBlank { shareInfo.fileName } + var contentLength = shareInfo.fileSize val range: LongRange? = call.request.ranges()?.mergeToSingle(contentLength) call.response.apply { @@ -69,7 +72,7 @@ fun MixFileServer.getDownloadRoute(): RoutingHandler { } contentLength = mixFile.fileSize - range.first } - responseDownloadFileStream(call, fileList, contentLength, shareInfo, referer) + responseDownloadFileStream(call, fileList, contentLength, shareInfo, referer, name) } } @@ -78,12 +81,13 @@ private suspend fun MixFileServer.responseDownloadFileStream( fileDataList: List>, contentLength: Long, shareInfo: MixShareInfo, - referer: String?, + referer: String = shareInfo.referer, + name: String = shareInfo.fileName ) { coroutineScope { val fileList = fileDataList.toMutableList() call.respondBytesWriter( - contentType = ContentType.parse(shareInfo.contentType()).withCharset(Charsets.UTF_8), + contentType = ContentType.parse(name.parseFileMimeType()).withCharset(Charsets.UTF_8), contentLength = contentLength ) { val sortedTask = SortedTask(downloadTaskCount) @@ -95,7 +99,7 @@ private suspend fun MixFileServer.responseDownloadFileStream( tasks.add(async { val (url, range) = currentFile val dataBytes = - shareInfo.fetchFile(url, httpClient, referer ?: shareInfo.referer) + shareInfo.fetchFile(url, httpClient, referer) sortedTask.addTask(taskOrder) { val dataToWrite = when { range == 0 -> dataBytes diff --git a/app/src/test/java/com/donut/mixfile/ExampleUnitTest.kt b/app/src/test/java/com/donut/mixfile/ExampleUnitTest.kt index e76c8dc..34cca0f 100644 --- a/app/src/test/java/com/donut/mixfile/ExampleUnitTest.kt +++ b/app/src/test/java/com/donut/mixfile/ExampleUnitTest.kt @@ -1,7 +1,6 @@ package com.donut.mixfile -import com.alibaba.fastjson2.to -import com.alibaba.fastjson2.toJSONString +import io.ktor.http.URLBuilder import org.junit.Test import java.util.Date