perf: large profile pictures and knowledge exports load much faster (#34598)

Large profile pictures of users, models and channel webhooks (animated ones, or ones set through the API) were sent out in thousands of small pieces, each handled separately, and the knowledge base export sent its zip file the same way. A 2 MB picture took around 200 to 400 ms to load, 20 loads of it at once, as on a page full of avatars, took 9 to 15 seconds, and an 8 MB export spent close to a second just sending the file. Both are now sent as one complete response with the same content type and headers, plus a size header in place of chunked sending.

| Picture | One load before | One load after | 20 at once before | 20 at once after |
| --- | --- | --- | --- | --- |
| Model, 2 MB | 207 ms | 15 ms | 9.3 s | 0.48 s |
| User, 2 MB | 379 ms | 15 ms | 15.0 s | 0.34 s |
| Model, 5 MB | 743 ms | 39 ms | 33.6 s | 0.63 s |
| User, 5 MB | 627 ms | 25 ms | 30.7 s | 0.54 s |

Fixes #34597
This commit is contained in:
Classic298 authored and GitHub committed 2026-11-17 22:18:51 +09:00
1 parent e1b1e60e7f
commit 0f54aee859
4 files changed
+12 -19

No files matched your search

+3 -5
View File
@@ -1,12 +1,11 @@
import base64
import io
import logging
import time
import uuid
from typing import Optional
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Request, status
from fastapi.responses import FileResponse, Response, StreamingResponse
from fastapi.responses import FileResponse, Response
from open_webui.config import ENABLE_ADMIN_CHAT_ACCESS, ENABLE_ADMIN_EXPORT
from open_webui.constants import ERROR_MESSAGES
from open_webui.env import ENABLE_PROFILE_IMAGE_URL_FORWARDING, STATIC_DIR
@@ -2099,11 +2098,10 @@ async def get_webhook_profile_image(
try:
header, base64_data = webhook.profile_image_url.split(',', 1)
image_data = base64.b64decode(base64_data)
image_buffer = io.BytesIO(image_data)
media_type = header.split(';')[0].lstrip('data:')
return StreamingResponse(
image_buffer,
return Response(
content=image_data,
media_type=media_type,
headers={'Content-Disposition': 'inline'},
)
+3 -5
View File
@@ -10,7 +10,7 @@ from typing import List, Optional
from urllib.parse import quote
from fastapi import APIRouter, Depends, HTTPException, Query, Request, status
from fastapi.responses import StreamingResponse
from fastapi.responses import Response, StreamingResponse
from open_webui.config import (
BYPASS_ADMIN_ACCESS_CONTROL,
ENABLE_KNOWLEDGE_FILE_RETENTION,
@@ -2311,14 +2311,12 @@ async def export_knowledge_by_id(id: str, user=Depends(get_admin_user), db: Asyn
filename = f'{filename}.txt'
zf.writestr(filename, content)
zip_buffer.seek(0)
# Sanitize knowledge name for filename
safe_name = ''.join(c if c.isalnum() or c in ' -_' else '_' for c in knowledge.name)
zip_filename = f'{safe_name}.zip'
return StreamingResponse(
zip_buffer,
return Response(
content=zip_buffer.getvalue(),
media_type='application/zip',
headers={'Content-Disposition': f"attachment; filename*=UTF-8''{quote(zip_filename, safe='')}"},
)
+3 -4
View File
@@ -18,7 +18,7 @@ from fastapi import (
Response,
status,
)
from fastapi.responses import RedirectResponse, StreamingResponse
from fastapi.responses import RedirectResponse
from open_webui.config import BYPASS_ADMIN_ACCESS_CONTROL
from open_webui.constants import ERROR_MESSAGES
from open_webui.env import (
@@ -1020,7 +1020,6 @@ async def get_model_profile_image(
try:
header, base64_data = profile_image_url.split(',', 1)
image_data = base64.b64decode(base64_data)
image_buffer = io.BytesIO(image_data)
media_type = header.split(';')[0].lstrip('data:').lower()
# only serve known-safe raster types inline; reject SVG/unknown (can run script on our origin)
@@ -1040,8 +1039,8 @@ async def get_model_profile_image(
if updated_at:
headers['ETag'] = f'"{updated_at}"'
return StreamingResponse(
image_buffer,
return Response(
content=image_data,
media_type=media_type,
headers=headers,
)
+3 -5
View File
@@ -1,7 +1,6 @@
from __future__ import annotations
import base64
import io
import logging
import time
from collections import Counter
@@ -10,7 +9,7 @@ from datetime import datetime, timedelta
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Query, Request, status
from fastapi.responses import FileResponse, Response, StreamingResponse
from fastapi.responses import FileResponse, Response
from open_webui.config import DEFAULT_USER_PERMISSIONS
from open_webui.constants import ERROR_MESSAGES
from open_webui.events import EVENTS, publish_event
@@ -850,14 +849,13 @@ async def get_user_profile_image_by_id(user_id: str, user=Depends(get_verified_u
try:
header, base64_data = user.profile_image_url.split(',', 1)
image_data = base64.b64decode(base64_data)
image_buffer = io.BytesIO(image_data)
media_type = header.split(';')[0].lstrip('data:').lower()
if media_type not in PROFILE_IMAGE_ALLOWED_MIME_TYPES:
return FileResponse(f'{STATIC_DIR}/user.png')
return StreamingResponse(
image_buffer,
return Response(
content=image_data,
media_type=media_type,
headers={
'Content-Disposition': 'inline',