Skip to content

Commit

Permalink
Merge pull request open-webui#6383 from open-webui/dev
Browse files Browse the repository at this point in the history
refac
  • Loading branch information
tjbck authored Oct 24, 2024
2 parents 68916f7 + 1472f12 commit 144dc0e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
31 changes: 31 additions & 0 deletions backend/open_webui/apps/webui/routers/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,37 @@ async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
)


@router.get("/{id}/content/html")
async def get_html_file_content_by_id(id: str, user=Depends(get_verified_user)):
file = Files.get_file_by_id(id)
if file and (file.user_id == user.id or user.role == "admin"):
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():
print(f"file_path: {file_path}")
return FileResponse(file_path)
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ERROR_MESSAGES.NOT_FOUND,
)
except Exception as e:
log.exception(e)
log.error(f"Error getting file content")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.DEFAULT("Error getting file content"),
)
else:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=ERROR_MESSAGES.NOT_FOUND,
)


@router.get("/{id}/content/{file_name}")
async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
file = Files.get_file_by_id(id)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const replaceTokens = (content, char, user) => {

// Replace HTML ID tags with corresponding HTML content
content = content.replace(htmlIdToken, (match, fileId) => {
const htmlUrl = `${WEBUI_BASE_URL}/api/v1/files/${fileId}/content`;
const htmlUrl = `${WEBUI_BASE_URL}/api/v1/files/${fileId}/content/html`;
return `<iframe src="${htmlUrl}" width="100%" frameborder="0" onload="this.style.height=(this.contentWindow.document.body.scrollHeight+20)+'px';"></iframe>`;
});

Expand Down

0 comments on commit 144dc0e

Please sign in to comment.