返回提交历史
Modified
g4f/image/copy_images.py
+10
-2
XFEstudio/gpt4free
perf(media): stream base64 and data-uri media directly to disk without intermediate buffer copies
4fc153f8
代码差异
1 个文件
+10
-2
@@ -83,7 +83,13 @@ async def save_response_media(
83
83
raise ValueError("Response must be a dict or have headers")
84
84
85
85
if isinstance(response, str):
86
response = base64.b64decode(response)
86
if response.startswith("data:"):
87
header, _, b64data = response.partition(",")
88
if ";" in header:
89
mime = header.split(";", 1)[0].replace("data:", "").strip()
90
if mime and not content_type:
91
content_type = mime
92
response = b64data
87
93
88
94
extension = MEDIA_TYPE_MAP.get(content_type)
89
95
if extension is None:
@@ -96,7 +102,9 @@ async def save_response_media(
96
102
ensure_media_dir()
97
103
98
104
with open(target_path, "wb") as f:
99
if isinstance(response, bytes):
105
if isinstance(response, str):
106
f.write(base64.b64decode(response))
107
elif isinstance(response, bytes):
100
108
f.write(response)
101
109
else:
102
110
if hasattr(response, "iter_content"):