返回提交历史
Modified
g4f/api/__init__.py
+13
-8
Modified
g4f/image/__init__.py
+2
-2
XFEstudio/gpt4free
Fix broken thumbnails
424d44e6
代码差异
2 个文件
+15
-10
@@ -677,6 +677,9 @@ class Api:
677
677
})
678
678
async def get_media(filename, request: Request, thumbnail: bool = False):
679
679
target = os.path.join(get_media_dir(), os.path.basename(filename))
680
if thumbnail and has_pillow:
681
thumbnail_dir = os.path.join(get_media_dir(), "thumbnails")
682
thumbnail = os.path.join(thumbnail_dir, filename)
680
683
if not os.path.isfile(target):
681
684
other_name = os.path.join(get_media_dir(), os.path.basename(quote_plus(filename)))
682
685
if os.path.isfile(other_name):
@@ -684,11 +687,14 @@ class Api:
684
687
result = target
685
688
ext = os.path.splitext(filename)[1][1:]
686
689
mime_type = EXTENSIONS_MAP.get(ext)
687
stat_result = SimpleNamespace()
688
stat_result.st_size = 0
689
if os.path.isfile(result):
690
stat_result.st_size = os.stat(result).st_size
691
stat_result.st_mtime = int(f"{filename.split('_')[0]}") if filename.startswith("1") else 0
690
if thumbnail and has_pillow and os.path.isfile(thumbnail):
691
stat_result = os.stat(thumbnail)
692
elif os.path.isfile(target):
693
stat_result = os.stat(target)
694
else:
695
stat_result = SimpleNamespace()
696
stat_result.st_size = 0
697
stat_result.st_mtime = 0
692
698
headers = {
693
699
"cache-control": "public, max-age=31536000",
694
700
"last-modified": formatdate(stat_result.st_mtime, usegmt=True),
@@ -731,13 +737,12 @@ class Api:
731
737
debug.error(e)
732
738
return RedirectResponse(url=source_url)
733
739
if thumbnail and has_pillow:
734
thumbnail_dir = os.path.join(get_media_dir(), "thumbnails")
735
thumbnail = os.path.join(thumbnail_dir, filename)
736
740
try:
737
741
if not os.path.isfile(thumbnail):
738
742
image = Image.open(target)
739
743
os.makedirs(thumbnail_dir, exist_ok=True)
740
process_image(image, save=os.path.join(thumbnail_dir, filename))
744
process_image(image, save=thumbnail)
745
debug.log(f"Thumbnail created: {thumbnail}")
741
746
except Exception as e:
742
747
logger.exception(e)
743
748
if os.path.isfile(thumbnail):
@@ -224,7 +224,7 @@ def get_orientation(image: Image) -> int:
224
224
exif_data = image.getexif() if hasattr(image, 'getexif') else image._getexif()
225
225
return exif_data.get(get_orientation_key()) if exif_data else None
226
226
227
def process_image(image: Image, new_width: int = 800, new_height: int = 800, save: str = None) -> Image:
227
def process_image(image: Image, new_width: int = 800, new_height: int = 400, save: str = None) -> Image:
228
228
"""
229
229
Processes the given image by adjusting its orientation and resizing it.
230
230
@@ -255,7 +255,7 @@ def process_image(image: Image, new_width: int = 800, new_height: int = 800, sav
255
255
elif image.mode != "RGB":
256
256
image = image.convert("RGB")
257
257
elif save is not None:
258
image.save(save, quality=75, exif=None)
258
image.save(save, exif=b"")
259
259
return image
260
260
261
261
def to_bytes(image: ImageType) -> bytes: