mirror of
https://github.com/open-webui/open-webui.git
synced 2026-05-02 02:09:17 -05:00
refac
This commit is contained in:
@@ -10,7 +10,11 @@ from fastapi import (
|
||||
Request,
|
||||
UploadFile,
|
||||
)
|
||||
from typing import Optional
|
||||
from pathlib import Path
|
||||
|
||||
from open_webui.storage.provider import Storage
|
||||
from open_webui.models.files import Files
|
||||
from open_webui.routers.files import upload_file_handler
|
||||
|
||||
import mimetypes
|
||||
@@ -113,3 +117,26 @@ def get_file_url_from_base64(request, base64_file_string, metadata, user):
|
||||
elif "data:audio/wav;base64" in base64_file_string:
|
||||
return get_audio_url_from_base64(request, base64_file_string, metadata, user)
|
||||
return None
|
||||
|
||||
|
||||
def get_image_base64_from_file_id(id: str) -> Optional[str]:
|
||||
file = Files.get_file_by_id(id)
|
||||
if not file:
|
||||
return None
|
||||
|
||||
try:
|
||||
file_path = Storage.get_file(file.path)
|
||||
file_path = Path(file_path)
|
||||
|
||||
# Check if the file already exists in the cache
|
||||
if file_path.is_file():
|
||||
import base64
|
||||
|
||||
with open(file_path, "rb") as image_file:
|
||||
encoded_string = base64.b64encode(image_file.read()).decode("utf-8")
|
||||
content_type, _ = mimetypes.guess_type(file_path.name)
|
||||
return f"data:{content_type};base64,{encoded_string}"
|
||||
else:
|
||||
return None
|
||||
except Exception as e:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user