返回提交历史
Modified
g4f/gui/server/api.py
+5
-3
XFEstudio/gpt4free
Address code review feedback: improve code locality and add logging for file size errors
Co-authored-by: hlohaus <983577+hlohaus@users.noreply.github.com>
1de870b6
代码差异
1 个文件
+5
-3
@@ -223,7 +223,6 @@ class Api:
223
223
yield self._format_json("preview", chunk.to_string(), urls=chunk.urls, alt=chunk.alt)
224
224
elif isinstance(chunk, MediaResponse):
225
225
media = chunk
226
image_metadata = None
227
226
if download_media or chunk.get("cookies"):
228
227
chunk.alt = format_media_prompt(kwargs.get("messages"), chunk.alt)
229
228
width, height = get_width_height(chunk.get("width"), chunk.get("height"))
@@ -241,6 +240,7 @@ class Api:
241
240
))
242
241
options = {}
243
242
target_paths, urls = get_target_paths_and_urls(media)
243
image_metadata = None
244
244
if target_paths:
245
245
if has_pillow:
246
246
try:
@@ -263,11 +263,13 @@ class Api:
263
263
for path in target_paths:
264
264
try:
265
265
total_size += os.path.getsize(path)
266
except OSError:
267
pass
266
except OSError as e:
267
logger.warning(f"Failed to get file size for {path}: {e}")
268
268
if total_size > 0:
269
269
image_metadata["file_size"] = total_size
270
270
media = ImageResponse(urls, chunk.alt, options) if isinstance(chunk, ImageResponse) else VideoResponse(media, chunk.alt)
271
else:
272
image_metadata = None
271
273
yield self._format_json("content", str(media), urls=media.urls, alt=media.alt)
272
274
# Yield usage statistics with image metadata for image responses
273
275
if image_metadata: