fix content type

This commit is contained in:
Evan You
2025-04-08 09:48:11 +08:00
parent 50b5e4e422
commit 354cd21cf6
2 changed files with 15 additions and 12 deletions

View File

@@ -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<Pair<String, Int>>,
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

View File

@@ -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