|
@@ -1,5 +1,13 @@
|
|
|
import logging
|
|
|
from typing import Optional
|
|
|
+import base64
|
|
|
+import io
|
|
|
+
|
|
|
+
|
|
|
+from fastapi import APIRouter, Depends, HTTPException, Request, status
|
|
|
+from fastapi.responses import Response, StreamingResponse, FileResponse
|
|
|
+from pydantic import BaseModel
|
|
|
+
|
|
|
|
|
|
from open_webui.models.auths import Auths
|
|
|
from open_webui.models.groups import Groups
|
|
@@ -21,9 +29,8 @@ from open_webui.socket.main import (
|
|
|
get_user_active_status,
|
|
|
)
|
|
|
from open_webui.constants import ERROR_MESSAGES
|
|
|
-from open_webui.env import SRC_LOG_LEVELS
|
|
|
-from fastapi import APIRouter, Depends, HTTPException, Request, status
|
|
|
-from pydantic import BaseModel
|
|
|
+from open_webui.env import SRC_LOG_LEVELS, STATIC_DIR
|
|
|
+
|
|
|
|
|
|
from open_webui.utils.auth import get_admin_user, get_password_hash, get_verified_user
|
|
|
from open_webui.utils.access_control import get_permissions, has_permission
|
|
@@ -329,6 +336,43 @@ async def get_user_by_id(user_id: str, user=Depends(get_verified_user)):
|
|
|
)
|
|
|
|
|
|
|
|
|
+############################
|
|
|
+# GetUserProfileImageById
|
|
|
+############################
|
|
|
+
|
|
|
+
|
|
|
+@router.get("/{user_id}/profile/image")
|
|
|
+async def get_user_profile_image_by_id(user_id: str, user=Depends(get_verified_user)):
|
|
|
+ user = Users.get_user_by_id(user_id)
|
|
|
+ if user:
|
|
|
+ if user.profile_image_url:
|
|
|
+ # check if it's url or base64
|
|
|
+ if user.profile_image_url.startswith("http"):
|
|
|
+ return Response(
|
|
|
+ status_code=status.HTTP_302_FOUND,
|
|
|
+ headers={"Location": user.profile_image_url},
|
|
|
+ )
|
|
|
+ elif user.profile_image_url.startswith("data:image"):
|
|
|
+ try:
|
|
|
+ header, base64_data = user.profile_image_url.split(",", 1)
|
|
|
+ image_data = base64.b64decode(base64_data)
|
|
|
+ image_buffer = io.BytesIO(image_data)
|
|
|
+
|
|
|
+ return StreamingResponse(
|
|
|
+ image_buffer,
|
|
|
+ media_type="image/png",
|
|
|
+ headers={"Content-Disposition": "inline; filename=image.png"},
|
|
|
+ )
|
|
|
+ except Exception as e:
|
|
|
+ pass
|
|
|
+ return FileResponse(f"{STATIC_DIR}/user.png")
|
|
|
+ else:
|
|
|
+ raise HTTPException(
|
|
|
+ status_code=status.HTTP_400_BAD_REQUEST,
|
|
|
+ detail=ERROR_MESSAGES.USER_NOT_FOUND,
|
|
|
+ )
|
|
|
+
|
|
|
+
|
|
|
############################
|
|
|
# GetUserActiveStatusById
|
|
|
############################
|