From f8c682b183e462bbc45517502915d788e1a23f5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=AF=E5=A4=A7=E4=BE=A0?= Date: Mon, 15 Sep 2025 21:47:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=88=AE=E5=89=8A?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6=E6=9D=83=E9=99=90=E5=8F=AA=E6=9C=89?= =?UTF-8?q?0600=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/media.py | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/app/chain/media.py b/app/chain/media.py index 1286e625..4c08a309 100644 --- a/app/chain/media.py +++ b/app/chain/media.py @@ -1,3 +1,4 @@ +import os from pathlib import Path from tempfile import NamedTemporaryFile from threading import Lock @@ -457,10 +458,9 @@ class MediaChain(ChainBase): """ if not _fileitem or not _content or not _path: return - # 使用tempfile创建临时文件,手动删除 - tmp_file = NamedTemporaryFile(delete=False, suffix=_path.suffix) - tmp_file_path = Path(tmp_file.name) - try: + # 使用tempfile创建临时文件,自动删除 + with NamedTemporaryFile(delete_on_close=False, suffix=_path.suffix) as tmp_file: + tmp_file_path = Path(tmp_file.name) # 写入内容 if isinstance(_content, bytes): tmp_file.write(_content) @@ -468,6 +468,11 @@ class MediaChain(ChainBase): tmp_file.write(_content.encode('utf-8')) tmp_file.flush() tmp_file.close() # 关闭文件句柄 + + current_umask = os.umask(0) + os.umask(current_umask) + # 刮削文件只需要读写权限 + tmp_file_path.chmod(0o666 & ~current_umask) # 上传文件 item = storagechain.upload_file(fileitem=_fileitem, path=tmp_file_path, new_name=_path.name) @@ -475,12 +480,6 @@ class MediaChain(ChainBase): logger.info(f"已保存文件:{item.path}") else: logger.warn(f"文件保存失败:{_path}") - finally: - # 手动删除临时文件 - try: - tmp_file_path.unlink(missing_ok=True) - except Exception as cleanup_err: - logger.warning(f"清理临时文件失败:{cleanup_err}") def __download_and_save_image(_fileitem: schemas.FileItem, _path: Path, _url: str): """ @@ -496,10 +495,9 @@ class MediaChain(ChainBase): request_utils = RequestUtils(proxies=settings.PROXY, ua=settings.NORMAL_USER_AGENT) with request_utils.get_stream(url=_url) as r: if r and r.status_code == 200: - # 使用tempfile创建临时文件,手动删除 - tmp_file = NamedTemporaryFile(delete=False, suffix=_path.suffix) - tmp_file_path = Path(tmp_file.name) - try: + # 使用tempfile创建临时文件,自动删除 + with NamedTemporaryFile(delete_on_close=False, suffix=_path.suffix) as tmp_file: + tmp_file_path = Path(tmp_file.name) # 流式写入文件 for chunk in r.iter_content(chunk_size=8192): if chunk: @@ -507,6 +505,11 @@ class MediaChain(ChainBase): tmp_file.flush() tmp_file.close() # 关闭文件句柄 + current_umask = os.umask(0) + os.umask(current_umask) + # 刮削的图片只需要读写权限 + tmp_file_path.chmod(0o666 & ~current_umask) + # 上传文件 item = storagechain.upload_file(fileitem=_fileitem, path=tmp_file_path, new_name=_path.name) @@ -514,12 +517,6 @@ class MediaChain(ChainBase): logger.info(f"已保存图片:{item.path}") else: logger.warn(f"图片保存失败:{_path}") - finally: - # 手动删除临时文件 - try: - tmp_file_path.unlink(missing_ok=True) - except Exception as cleanup_err: - logger.warning(f"清理临时文件失败:{cleanup_err}") else: logger.info(f"{_url} 图片下载失败") except Exception as err: From 14961323c37c04eb146fe34c9416e9bca68d0510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=AF=E5=A4=A7=E4=BE=A0?= Date: Mon, 15 Sep 2025 22:01:00 +0800 Subject: [PATCH 2/2] fix umask --- app/chain/media.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/app/chain/media.py b/app/chain/media.py index 4c08a309..876daa95 100644 --- a/app/chain/media.py +++ b/app/chain/media.py @@ -22,6 +22,8 @@ from app.utils.string import StringUtils recognize_lock = Lock() scraping_lock = Lock() +current_umask = os.umask(0) +os.umask(current_umask) class MediaChain(ChainBase): """ @@ -459,7 +461,7 @@ class MediaChain(ChainBase): if not _fileitem or not _content or not _path: return # 使用tempfile创建临时文件,自动删除 - with NamedTemporaryFile(delete_on_close=False, suffix=_path.suffix) as tmp_file: + with NamedTemporaryFile(delete=True, delete_on_close=False, suffix=_path.suffix) as tmp_file: tmp_file_path = Path(tmp_file.name) # 写入内容 if isinstance(_content, bytes): @@ -469,11 +471,9 @@ class MediaChain(ChainBase): tmp_file.flush() tmp_file.close() # 关闭文件句柄 - current_umask = os.umask(0) - os.umask(current_umask) # 刮削文件只需要读写权限 tmp_file_path.chmod(0o666 & ~current_umask) - + # 上传文件 item = storagechain.upload_file(fileitem=_fileitem, path=tmp_file_path, new_name=_path.name) if item: @@ -496,7 +496,7 @@ class MediaChain(ChainBase): with request_utils.get_stream(url=_url) as r: if r and r.status_code == 200: # 使用tempfile创建临时文件,自动删除 - with NamedTemporaryFile(delete_on_close=False, suffix=_path.suffix) as tmp_file: + with NamedTemporaryFile(delete=True, delete_on_close=False, suffix=_path.suffix) as tmp_file: tmp_file_path = Path(tmp_file.name) # 流式写入文件 for chunk in r.iter_content(chunk_size=8192): @@ -504,12 +504,10 @@ class MediaChain(ChainBase): tmp_file.write(chunk) tmp_file.flush() tmp_file.close() # 关闭文件句柄 - - current_umask = os.umask(0) - os.umask(current_umask) + # 刮削的图片只需要读写权限 tmp_file_path.chmod(0o666 & ~current_umask) - + # 上传文件 item = storagechain.upload_file(fileitem=_fileitem, path=tmp_file_path, new_name=_path.name)