mirror of
https://github.com/xtekky/gpt4free.git
synced 2025-10-06 00:36:57 +08:00
Add thumbnail support
This commit is contained in:
@@ -8,6 +8,7 @@ import asyncio
|
|||||||
import shutil
|
import shutil
|
||||||
import random
|
import random
|
||||||
import datetime
|
import datetime
|
||||||
|
from urllib.parse import quote_plus
|
||||||
from flask import Flask, Response, redirect, request, jsonify, send_from_directory
|
from flask import Flask, Response, redirect, request, jsonify, send_from_directory
|
||||||
from werkzeug.exceptions import NotFound
|
from werkzeug.exceptions import NotFound
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
@@ -15,6 +16,11 @@ from pathlib import Path
|
|||||||
from urllib.parse import quote_plus
|
from urllib.parse import quote_plus
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
|
|
||||||
|
try:
|
||||||
|
from PIL import Image
|
||||||
|
has_pillow = True
|
||||||
|
except ImportError:
|
||||||
|
has_pillow = False
|
||||||
try:
|
try:
|
||||||
from ...integration.markitdown import MarkItDown, StreamInfo
|
from ...integration.markitdown import MarkItDown, StreamInfo
|
||||||
has_markitdown = True
|
has_markitdown = True
|
||||||
@@ -28,7 +34,7 @@ from ...client.helper import filter_markdown
|
|||||||
from ...tools.files import supports_filename, get_streaming, get_bucket_dir, get_tempfile
|
from ...tools.files import supports_filename, get_streaming, get_bucket_dir, get_tempfile
|
||||||
from ...tools.run_tools import iter_run_tools
|
from ...tools.run_tools import iter_run_tools
|
||||||
from ...errors import ProviderNotFoundError
|
from ...errors import ProviderNotFoundError
|
||||||
from ...image import is_allowed_extension, MEDIA_TYPE_MAP
|
from ...image import is_allowed_extension, process_image, MEDIA_TYPE_MAP
|
||||||
from ...cookies import get_cookies_dir
|
from ...cookies import get_cookies_dir
|
||||||
from ...image.copy_images import secure_filename, get_source_url, get_media_dir, copy_media
|
from ...image.copy_images import secure_filename, get_source_url, get_media_dir, copy_media
|
||||||
from ... import ChatCompletion
|
from ... import ChatCompletion
|
||||||
@@ -208,6 +214,10 @@ class Backend_Api(Api):
|
|||||||
'/media/<path:name>': {
|
'/media/<path:name>': {
|
||||||
'function': self.serve_images,
|
'function': self.serve_images,
|
||||||
'methods': ['GET']
|
'methods': ['GET']
|
||||||
|
},
|
||||||
|
'/thumbnail/<path:name>': {
|
||||||
|
'function': self.serve_images,
|
||||||
|
'methods': ['GET']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -368,6 +378,15 @@ class Backend_Api(Api):
|
|||||||
media.append({"name": filename, "text": result})
|
media.append({"name": filename, "text": result})
|
||||||
else:
|
else:
|
||||||
media.append({"name": filename})
|
media.append({"name": filename})
|
||||||
|
if has_pillow:
|
||||||
|
try:
|
||||||
|
image = Image.open(copyfile)
|
||||||
|
thumbnail_dir = os.path.join(bucket_dir, "thumbnail")
|
||||||
|
os.makedirs(thumbnail_dir, exist_ok=True)
|
||||||
|
image = process_image(image)
|
||||||
|
image.save(os.path.join(thumbnail_dir, filename))
|
||||||
|
except Exception as e:
|
||||||
|
logger.exception(e)
|
||||||
elif is_supported:
|
elif is_supported:
|
||||||
newfile = os.path.join(bucket_dir, filename)
|
newfile = os.path.join(bucket_dir, filename)
|
||||||
filenames.append(filename)
|
filenames.append(filename)
|
||||||
@@ -395,6 +414,17 @@ class Backend_Api(Api):
|
|||||||
return redirect(source_url)
|
return redirect(source_url)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@app.route('/files/<bucket_id>/thumbnail/<filename>', methods=['GET'])
|
||||||
|
def get_media(bucket_id, filename, dirname: str = None):
|
||||||
|
media_dir = get_bucket_dir(dirname, bucket_id, "thumbnail")
|
||||||
|
try:
|
||||||
|
return send_from_directory(os.path.abspath(media_dir), filename)
|
||||||
|
except NotFound:
|
||||||
|
original = f'/files/{quote_plus(bucket_id)}/media/{quote_plus(filename)}'
|
||||||
|
if request.query_string:
|
||||||
|
original += f"?{request.query_string.decode()}"
|
||||||
|
return redirect(original)
|
||||||
|
|
||||||
self.match_files = {}
|
self.match_files = {}
|
||||||
|
|
||||||
@app.route('/search/<search>', methods=['GET'])
|
@app.route('/search/<search>', methods=['GET'])
|
||||||
|
@@ -214,7 +214,7 @@ def get_orientation(image: Image) -> int:
|
|||||||
if orientation is not None:
|
if orientation is not None:
|
||||||
return orientation
|
return orientation
|
||||||
|
|
||||||
def process_image(image: Image, new_width: int, new_height: int) -> Image:
|
def process_image(image: Image, new_width: int = 1000, new_height: int = 1000) -> Image:
|
||||||
"""
|
"""
|
||||||
Processes the given image by adjusting its orientation and resizing it.
|
Processes the given image by adjusting its orientation and resizing it.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user