Skip to content

Commit 34e3694

Browse files
committed
feat: 在所有存储实现中添加Content-Length响应头
1 parent ba8802a commit 34e3694

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

core/storage.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ async def get_file_response(self, file_code: FileCodes):
147147
return FileResponse(
148148
file_path,
149149
media_type="application/octet-stream",
150-
headers={"Content-Disposition": content_disposition},
150+
headers={"Content-Disposition": content_disposition, "Content-Length": str(file_code.size)},
151151
filename=filename # 保留原始文件名以备某些场景使用
152152
)
153153

@@ -329,7 +329,8 @@ async def stream_generator():
329329

330330
from fastapi.responses import StreamingResponse
331331
headers = {
332-
"Content-Disposition": f'attachment; filename="{filename.encode("utf-8").decode("latin-1")}"'
332+
"Content-Disposition": f'attachment; filename="{filename.encode("utf-8").decode("latin-1")}"',
333+
"Content-Length": str(file_code.size)
333334
}
334335
return StreamingResponse(
335336
stream_generator(),
@@ -633,7 +634,8 @@ async def stream_generator():
633634
yield chunk
634635

635636
headers = {
636-
"Content-Disposition": f'attachment; filename="{filename.encode("utf-8").decode("latin-1")}"'
637+
"Content-Disposition": f'attachment; filename="{filename.encode("utf-8").decode("latin-1")}"',
638+
"Content-Length": str(file_code.size)
637639
}
638640
return StreamingResponse(
639641
stream_generator(),
@@ -810,7 +812,8 @@ async def get_file_response(self, file_code: FileCodes):
810812
# 如果 reader 方法不存在,回退到全量读取(兼容旧版本)
811813
content = await self.operator.read(await file_code.get_file_path())
812814
headers = {
813-
"Content-Disposition": f'attachment; filename="{filename}"'
815+
"Content-Disposition": f'attachment; filename="{filename}"',
816+
"Content-Length": str(file_code.size)
814817
}
815818
return Response(
816819
content, headers=headers, media_type="application/octet-stream"
@@ -825,7 +828,8 @@ async def stream_generator():
825828
yield chunk
826829

827830
headers = {
828-
"Content-Disposition": f'attachment; filename="{filename}"'
831+
"Content-Disposition": f'attachment; filename="{filename}"',
832+
"Content-Length": str(file_code.size)
829833
}
830834
return StreamingResponse(
831835
stream_generator(),
@@ -1038,7 +1042,8 @@ async def stream_generator():
10381042
yield chunk
10391043

10401044
headers = {
1041-
"Content-Disposition": f'attachment; filename="{filename.encode("utf-8").decode()}"'
1045+
"Content-Disposition": f'attachment; filename="{filename.encode("utf-8").decode()}"',
1046+
"Content-Length": str(file_code.size)
10421047
}
10431048
return StreamingResponse(
10441049
stream_generator(),

0 commit comments

Comments
 (0)