暂时不删除临时文件

This commit is contained in:
liuwei
2025-06-16 17:18:37 +08:00
parent 22681f975b
commit e388deb4ea

View File

@@ -187,7 +187,7 @@ async def html_to_image(html_file, output_image):
for path in possible_chrome_paths: for path in possible_chrome_paths:
if check_chromium_installed(path): if check_chromium_installed(path):
browser_path = path browser_path = path
print(f"找到浏览器路径: {browser_path}") logger.debug(f"找到浏览器路径: {browser_path}")
break break
else: # Linux else: # Linux
import glob import glob
@@ -199,17 +199,17 @@ async def html_to_image(html_file, output_image):
for path in sorted(chrome_paths, reverse=True): # 按版本名排序,最新优先 for path in sorted(chrome_paths, reverse=True): # 按版本名排序,最新优先
if check_chromium_installed(path): if check_chromium_installed(path):
browser_path = path browser_path = path
print(f"找到 Playwright Chromium 路径: {browser_path}") logger.debug(f"找到 Playwright Chromium 路径: {browser_path}")
break break
if not browser_path: if not browser_path:
print("未找到已安装的 Chromium 浏览器,尝试使用 Playwright 默认安装") logger.debug("未找到已安装的 Chromium 浏览器,尝试使用 Playwright 默认安装")
try: try:
print("正在安装 Playwright 浏览器...") logger.debug("正在安装 Playwright 浏览器...")
subprocess.run(["playwright", "install", "chromium"], check=True) subprocess.run(["playwright", "install", "chromium"], check=True)
print("Playwright 浏览器安装完成") logger.debug("Playwright 浏览器安装完成")
except Exception as install_error: except Exception as install_error:
print(f"安装 Playwright 浏览器失败: {install_error}") logger.debug(f"安装 Playwright 浏览器失败: {install_error}")
browser = await p.chromium.launch() # 使用默认路径 browser = await p.chromium.launch() # 使用默认路径
else: else:
@@ -223,15 +223,15 @@ async def html_to_image(html_file, output_image):
await page.wait_for_timeout(500) await page.wait_for_timeout(500)
await page.screenshot(path=output_image, full_page=True) await page.screenshot(path=output_image, full_page=True)
except Exception as e: except Exception as e:
print(f"截图失败: {e}") logger.exception(f"截图失败: {e}")
finally: finally:
await page.close() await page.close()
await browser.close() await browser.close()
except Exception as e: except Exception as e:
print(f"浏览器操作失败: {e}") logger.debug(f"浏览器操作失败: {e}")
if "Executable doesn't exist" in str(e): if "Executable doesn't exist" in str(e):
print("请运行 'playwright install' 命令安装必要的浏览器组件") logger.debug("请运行 'playwright install' 命令安装必要的浏览器组件")
raise raise
@@ -289,15 +289,15 @@ async def convert_md_str_to_image(md_content: str, output_image: str) -> str:
except Exception as e: except Exception as e:
logger.error(f"Error converting markdown to image: {e}") logger.error(f"Error converting markdown to image: {e}")
raise raise
finally: # finally:
# 可选:清理临时 HTML 文件 # # 可选:清理临时 HTML 文件
if temp_html_path.exists(): # if temp_html_path.exists():
try: # try:
# 使用异步方式删除文件 # # 使用异步方式删除文件
await asyncio.to_thread(os.remove, str(temp_html_path)) # # await asyncio.to_thread(os.remove, str(temp_html_path))
logger.debug(f"Deleted temporary HTML file: {temp_html_path}") # # logger.debug(f"Deleted temporary HTML file: {temp_html_path}")
except Exception as e: # except Exception as e:
logger.warning(f"Failed to delete temporary HTML file: {e}") # logger.warning(f"Failed to delete temporary HTML file: {e}")
# 示例使用 # 示例使用
if __name__ == "__main__": if __name__ == "__main__":