From db614180b92ad38e59c0028eb10ec67dc0fe4407 Mon Sep 17 00:00:00 2001 From: developer-wlj Date: Sun, 14 Sep 2025 17:14:52 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"refactor:=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6=E7=9A=84=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E5=92=8C=E4=B8=8A=E4=BC=A0=E9=80=BB=E8=BE=91"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 77c0f8f39ef29a013ea442dee4163cf7546ca789. --- app/chain/media.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/app/chain/media.py b/app/chain/media.py index ca61ce41..1286e625 100644 --- a/app/chain/media.py +++ b/app/chain/media.py @@ -458,16 +458,17 @@ class MediaChain(ChainBase): if not _fileitem or not _content or not _path: return # 使用tempfile创建临时文件,手动删除 - with NamedTemporaryFile(delete=False, suffix=_path.suffix) as tmp_file: - tmp_file_path = Path(tmp_file.name) + tmp_file = NamedTemporaryFile(delete=False, suffix=_path.suffix) + tmp_file_path = Path(tmp_file.name) + try: # 写入内容 if isinstance(_content, bytes): tmp_file.write(_content) else: tmp_file.write(_content.encode('utf-8')) tmp_file.flush() - - try: + tmp_file.close() # 关闭文件句柄 + # 上传文件 item = storagechain.upload_file(fileitem=_fileitem, path=tmp_file_path, new_name=_path.name) if item: @@ -496,15 +497,16 @@ class MediaChain(ChainBase): with request_utils.get_stream(url=_url) as r: if r and r.status_code == 200: # 使用tempfile创建临时文件,手动删除 - with NamedTemporaryFile(delete=False, suffix=_path.suffix) as tmp_file: - tmp_file_path = Path(tmp_file.name) + tmp_file = NamedTemporaryFile(delete=False, suffix=_path.suffix) + tmp_file_path = Path(tmp_file.name) + try: # 流式写入文件 for chunk in r.iter_content(chunk_size=8192): if chunk: tmp_file.write(chunk) tmp_file.flush() - - try: + tmp_file.close() # 关闭文件句柄 + # 上传文件 item = storagechain.upload_file(fileitem=_fileitem, path=tmp_file_path, new_name=_path.name)