fix: save_file accepts plain binary streams; repair admin local file share (D2)

- storage contract drops UploadFile in favor of BinaryIO (+content_type param),
  completing the framework decoupling of the storage layer
- LocalFileClass.read() leaked an open fd and its handle was passed where
  save_file expected UploadFile — /admin/local/share 500ed on every call;
  now reads bytes via with-block and shares correctly (regression tests added)
- OneDrive save_file additionally had save_path called as a function — fixed
This commit is contained in:
Marrrrrrrrry committed 2026-09-09 18:53:31 +08:00
1 parent 61d72ab6cc
commit bb95017aa3
4 files changed
+119 -29

No files matched your search

+6 -4
View File
@@ -1,4 +1,5 @@
import hashlib
import io
from pathlib import Path
import os
import time
@@ -1479,14 +1480,14 @@ class FileService:
reservation_token = f"local:{uuid.uuid4().hex}"
await reserve_storage(reservation_token, local_file.size, ttl_seconds=3600)
try:
text = await local_file.read()
data = await local_file.read() # bytes(read 内部用 with 关闭句柄)
expired_at, expired_count, used_count, code = await get_expire_info(
item.expire_value, item.expire_style
)
path, suffix, prefix, uuid_file_name, save_path = await get_file_path_name(
item
)
await self.file_storage.save_file(text, save_path)
await self.file_storage.save_file(io.BytesIO(data), save_path)
try:
await FileCodes.create(
code=code,
@@ -1673,8 +1674,9 @@ class LocalFileClass:
self.ctime = None
self.size = None
async def read(self):
return open(self.path, "rb")
async def read(self) -> bytes:
with open(self.path, "rb") as fh:
return fh.read()
async def write(self, data):
with open(self.path, "w") as f:
+2 -2
View File
@@ -124,7 +124,7 @@ async def share_file(
expired_at, expired_count, used_count, code = await get_expire_info(
expire_value, expire_style
)
await file_storage.save_file(file, save_path)
await file_storage.save_file(file.file, save_path, file.content_type)
await create_file_code(
code=code,
prefix=prefix,
@@ -767,7 +767,7 @@ async def presign_upload_proxy(
storage: FileStorageInterface = storages[settings.file_storage]()
try:
await storage.save_file(file, session.save_path)
await storage.save_file(file.file, session.save_path, file.content_type)
except Exception as e:
raise HTTPException(500, f"文件保存失败: {str(e)}")
+48 -23
View File
@@ -8,7 +8,7 @@ import os
import tempfile
from core.logger import logger
import shutil
from typing import Optional
from typing import BinaryIO, Optional
from urllib.parse import quote, unquote
import aiofiles
@@ -20,7 +20,6 @@ from dataclasses import dataclass
import re
import aioboto3
from botocore.config import Config
from fastapi import UploadFile
from core.errors import StorageError
from core.settings import data_root, settings
from core.utils import get_file_url, sanitize_filename
@@ -96,10 +95,10 @@ class FileStorageInterface:
)
file_sha256.update(chunk_data)
async def save_file(self, file: UploadFile, save_path: str):
"""
保存文件
"""
async def save_file(
self, stream: BinaryIO, save_path: str, content_type: Optional[str] = None
):
"""Save a binary stream (caller owns closing the stream)."""
raise NotImplementedError
async def delete_file(self, file_code: StoredFile):
@@ -198,7 +197,9 @@ class SystemFileStorage(FileStorageInterface):
f.write(chunk)
chunk = file.read(self.chunk_size)
async def save_file(self, file: UploadFile, save_path: str):
async def save_file(
self, stream: BinaryIO, save_path: str, content_type: Optional[str] = None
):
path_obj = Path(str(save_path).replace("\\", "/"))
directory = str(path_obj.parent).replace("\\", "/").lstrip("/")
# 提取原始文件名并进行清理
@@ -208,7 +209,7 @@ class SystemFileStorage(FileStorageInterface):
# 确保目录存在
if not safe_save_path.parent.exists():
safe_save_path.parent.mkdir(parents=True)
await asyncio.to_thread(self._save, file.file, safe_save_path)
await asyncio.to_thread(self._save, stream, safe_save_path)
async def delete_file(self, file_code: StoredFile):
save_path = self._resolve_safe_path(file_code.get_file_path())
@@ -381,14 +382,16 @@ class S3FileStorage(FileStorageInterface):
config=self._client_config(),
)
async def save_file(self, file: UploadFile, save_path: str):
async def save_file(
self, stream: BinaryIO, save_path: str, content_type: Optional[str] = None
):
async with self._client() as s3:
# 使用 upload_fileobj 流式上传,避免将整个文件加载到内存
await s3.upload_fileobj(
file.file,
stream,
self.bucket_name,
save_path,
ExtraArgs={"ContentType": file.content_type or "application/octet-stream"},
ExtraArgs={"ContentType": content_type or "application/octet-stream"},
)
async def delete_file(self, file_code: StoredFile):
@@ -703,14 +706,32 @@ class OneDriveFileStorage(FileStorageInterface):
path[-1] = path[-1].split(".")[0]
return "/".join(path)
def _save(self, file, save_path):
content = file.file.read()
name = save_path(file.filename)
path = self._get_path_str(save_path)
self.root_path.get_by_path(path).upload(name, content).execute_query()
async def save_file(
self, stream: BinaryIO, save_path: str, content_type: Optional[str] = None
):
"""保存文件(自动创建目录;修复旧实现把 save_path 字符串当函数调用的崩溃)"""
content = await asyncio.to_thread(stream.read)
normalized = str(save_path).replace("\\", "/")
name = await sanitize_filename(Path(normalized).name)
dir_path = "/".join(normalized.split("/")[:-1])
async def save_file(self, file: UploadFile, save_path: str):
await asyncio.to_thread(self._save, file, save_path)
current_folder = self.root_path
for part in dir_path.split("/"):
if not part:
continue
try:
current_folder = current_folder.get_by_path(part).get().execute_query()
except self._ClientRequestException as e:
if e.code == "itemNotFound":
current_folder = current_folder.create_folder(part).execute_query()
else:
raise e
await asyncio.to_thread(
lambda: current_folder.get_by_path(name)
.upload(name, content)
.execute_query()
)
def _delete(self, save_path):
path = self._get_path_str(save_path)
@@ -950,9 +971,11 @@ class OpenDALFileStorage(FileStorageInterface):
settings.opendal_scheme, **service_settings
)
async def save_file(self, file: UploadFile, save_path: str):
async def save_file(
self, stream: BinaryIO, save_path: str, content_type: Optional[str] = None
):
# 使用 asyncio.to_thread 避免阻塞事件循环
content = await asyncio.to_thread(file.file.read)
content = await asyncio.to_thread(stream.read)
await self.operator.write(save_path, content)
async def delete_file(self, file_code: StoredFile):
@@ -1147,7 +1170,9 @@ class WebDAVFileStorage(FileStorageInterface):
current_path = current_path.parent
async def save_file(self, file: UploadFile, save_path: str):
async def save_file(
self, stream: BinaryIO, save_path: str, content_type: Optional[str] = None
):
"""保存文件(自动创建目录,流式上传)"""
path_obj = Path(save_path)
directory_path = str(path_obj.parent)
@@ -1166,7 +1191,7 @@ class WebDAVFileStorage(FileStorageInterface):
"""流式读取文件内容"""
chunk_size = 256 * 1024 # 256KB chunks
while True:
chunk = await asyncio.to_thread(file.file.read, chunk_size)
chunk = await asyncio.to_thread(stream.read, chunk_size)
if not chunk:
break
yield chunk
@@ -1175,7 +1200,7 @@ class WebDAVFileStorage(FileStorageInterface):
async with session.put(
url,
data=file_sender(),
headers={"Content-Type": file.content_type or "application/octet-stream"}
headers={"Content-Type": content_type or "application/octet-stream"}
) as resp:
if resp.status not in (200, 201, 204):
content = await resp.text()
+63
View File
@@ -0,0 +1,63 @@
"""D2 回归:admin 本地文件分享此前必 500(read() 泄漏句柄 + save_file 误用
UploadFile 属性)。现在 save_file 接收 BinaryIO,read() 返回 bytes。"""
import asyncio
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
from tests.helpers import SettingsOverrideMixin, close_db, init_memory_db
from apps.admin.services import FileService, LocalFileClass
class LocalFileClassTests(unittest.TestCase):
def test_read_returns_bytes_and_closes_handle(self):
with TemporaryDirectory() as tmpdir:
with patch("apps.admin.services.data_root", Path(tmpdir)), patch(
"core.storage.data_root", Path(tmpdir)
):
local = Path(tmpdir) / "local"
local.mkdir()
(local / "hello.txt").write_bytes(b"local-bytes")
lf = LocalFileClass("hello.txt")
self.assertEqual(lf.size, 11)
data = asyncio.run(lf.read())
self.assertEqual(data, b"local-bytes")
class ShareLocalFileTests(SettingsOverrideMixin, unittest.TestCase):
def test_share_local_file_creates_code_and_saves_file(self):
asyncio.run(self._scenario())
async def _scenario(self):
with TemporaryDirectory() as tmpdir:
await init_memory_db()
try:
with patch("apps.admin.services.data_root", Path(tmpdir)), patch(
"core.storage.data_root", Path(tmpdir)
):
local = Path(tmpdir) / "local"
local.mkdir()
(local / "doc.txt").write_bytes(b"hello local share")
class Item:
filename = "doc.txt"
expire_value = 1
expire_style = "day"
service = FileService()
result = await service.share_local_file(Item())
self.assertIn("code", result)
saved = Path(tmpdir) / "share" / "data"
saved_files = [p for p in saved.rglob("*") if p.is_file()]
self.assertTrue(saved_files, "分享后文件应已落盘")
self.assertEqual(saved_files[0].read_bytes(), b"hello local share")
finally:
await close_db()
if __name__ == "__main__":
unittest.main()