Skip to content

Commit

Permalink
render images in preview
Browse files Browse the repository at this point in the history
  • Loading branch information
glwntd committed Dec 17, 2024
1 parent a18ebda commit 5a35d87
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pyglossary/ui/ui_web/server_ws_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
- GitHub: @glowinthedark
- Website: https://legbehindneck.com
"""
import base64

import errno
import json
Expand Down Expand Up @@ -105,6 +106,7 @@
CLOSE_STATUS_NORMAL = 1000
DEFAULT_CLOSE_REASON = bytes("", encoding="utf-8")
DEFAULT_MAX_BROWSE_ENTRIES = 42
MAX_IMAGE_SIZE = 512000

class WebLogHandler(logging.Handler):
def __init__(self, server) -> None:
Expand Down Expand Up @@ -478,6 +480,11 @@ def browse_glossary(self, path):
num_results += 1
else:
response.append(f"&#128206;<pre>{entry.s_word}</pre>")
if entry.isData() and entry.size() < MAX_IMAGE_SIZE and entry.s_word.lower().endswith(('.jpg', 'jpeg','.png')):
extension = Path(entry.s_word).suffix[1:]
response.append(f"""
<img class="data" src="data:image/{extension};base64,{base64.b64encode(entry.data).decode('utf-8')}" alt="{entry.s_word}"/>
""")
if num_results >= max_results:
break
except Exception as e:
Expand Down

0 comments on commit 5a35d87

Please sign in to comment.