mirror of
https://github.com/open-webui/open-webui
synced 2025-03-24 14:40:51 +00:00
refac
This commit is contained in:
parent
7f51ef1838
commit
4aca1e86ad
@ -10,7 +10,7 @@ from open_webui.config import UPLOAD_DIR
|
|||||||
from open_webui.constants import ERROR_MESSAGES
|
from open_webui.constants import ERROR_MESSAGES
|
||||||
from open_webui.env import SRC_LOG_LEVELS
|
from open_webui.env import SRC_LOG_LEVELS
|
||||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status
|
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse, StreamingResponse
|
||||||
from open_webui.utils.utils import get_admin_user, get_verified_user
|
from open_webui.utils.utils import get_admin_user, get_verified_user
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -189,16 +189,32 @@ async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
|
|||||||
file = Files.get_file_by_id(id)
|
file = Files.get_file_by_id(id)
|
||||||
|
|
||||||
if file and (file.user_id == user.id or user.role == "admin"):
|
if file and (file.user_id == user.id or user.role == "admin"):
|
||||||
file_path = Path(file.meta["path"])
|
file_path = file.meta.get("path")
|
||||||
|
if file_path:
|
||||||
|
file_path = Path(file_path)
|
||||||
|
|
||||||
# Check if the file already exists in the cache
|
# Check if the file already exists in the cache
|
||||||
if file_path.is_file():
|
if file_path.is_file():
|
||||||
print(f"file_path: {file_path}")
|
print(f"file_path: {file_path}")
|
||||||
return FileResponse(file_path)
|
return FileResponse(file_path)
|
||||||
|
else:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
detail=ERROR_MESSAGES.NOT_FOUND,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
raise HTTPException(
|
# File path doesn’t exist, return the content as .txt if possible
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
file_content = file.content.get("content", "")
|
||||||
detail=ERROR_MESSAGES.NOT_FOUND,
|
file_name = file.filename
|
||||||
|
|
||||||
|
# Create a generator that encodes the file content
|
||||||
|
def generator():
|
||||||
|
yield file_content.encode("utf-8")
|
||||||
|
|
||||||
|
return StreamingResponse(
|
||||||
|
generator(),
|
||||||
|
media_type="text/plain",
|
||||||
|
headers={"Content-Disposition": f"attachment; filename={file_name}"},
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
|
@ -80,7 +80,7 @@
|
|||||||
name: 'All Documents',
|
name: 'All Documents',
|
||||||
legacy: true,
|
legacy: true,
|
||||||
type: 'collection',
|
type: 'collection',
|
||||||
description: 'Deprecated (legacy collection), please use the new knowledge base.',
|
description: 'Deprecated (legacy collection), please create a new knowledge base.',
|
||||||
title: $i18n.t('All Documents'),
|
title: $i18n.t('All Documents'),
|
||||||
collection_names: legacy_documents.map((item) => item.id)
|
collection_names: legacy_documents.map((item) => item.id)
|
||||||
},
|
},
|
||||||
@ -93,7 +93,7 @@
|
|||||||
name: tag,
|
name: tag,
|
||||||
legacy: true,
|
legacy: true,
|
||||||
type: 'collection',
|
type: 'collection',
|
||||||
description: 'Deprecated (legacy collection), please use the new knowledge base.',
|
description: 'Deprecated (legacy collection), please create a new knowledge base.',
|
||||||
collection_names: legacy_documents
|
collection_names: legacy_documents
|
||||||
.filter((item) => (item?.meta?.tags ?? []).map((tag) => tag.name).includes(tag))
|
.filter((item) => (item?.meta?.tags ?? []).map((tag) => tag.name).includes(tag))
|
||||||
.map((item) => item.id)
|
.map((item) => item.id)
|
||||||
|
@ -21,7 +21,8 @@
|
|||||||
name: 'All Documents',
|
name: 'All Documents',
|
||||||
legacy: true,
|
legacy: true,
|
||||||
type: 'collection',
|
type: 'collection',
|
||||||
description: 'Deprecated (legacy collection), please use the new knowledge base.',
|
description: 'Deprecated (legacy collection), please create a new knowledge base.',
|
||||||
|
|
||||||
title: $i18n.t('All Documents'),
|
title: $i18n.t('All Documents'),
|
||||||
collection_names: legacy_documents.map((item) => item.id)
|
collection_names: legacy_documents.map((item) => item.id)
|
||||||
},
|
},
|
||||||
@ -34,7 +35,8 @@
|
|||||||
name: tag,
|
name: tag,
|
||||||
legacy: true,
|
legacy: true,
|
||||||
type: 'collection',
|
type: 'collection',
|
||||||
description: 'Deprecated (legacy collection), please use the new knowledge base.',
|
description: 'Deprecated (legacy collection), please create a new knowledge base.',
|
||||||
|
|
||||||
collection_names: legacy_documents
|
collection_names: legacy_documents
|
||||||
.filter((item) => (item?.meta?.tags ?? []).map((tag) => tag.name).includes(tag))
|
.filter((item) => (item?.meta?.tags ?? []).map((tag) => tag.name).includes(tag))
|
||||||
.map((item) => item.id)
|
.map((item) => item.id)
|
||||||
|
Loading…
Reference in New Issue
Block a user