shrlok: add html type

main
dece 2 years ago
parent e7dfc359f5
commit a27b25182b

1
.gitignore vendored

@ -1,3 +1,4 @@
__pycache__/
build/ build/
dist/ dist/
*.egg-info/ *.egg-info/

@ -20,7 +20,8 @@ The content is 13 bytes (assuming no LF at the end), the header is 14 bytes,
plus the null byte it is 13 + 14 + 1 = 28 bytes, thus the prefixed length. plus the null byte it is 13 + 14 + 1 = 28 bytes, thus the prefixed length.
After the content is succesfully retrieved and put on an appropriate location, After the content is succesfully retrieved and put on an appropriate location,
the server will reply the file path through the socket and close the connection. the server will reply the file path through the socket and close the
connection.
""" """
import argparse import argparse
@ -66,6 +67,8 @@ class Handler(socketserver.StreamRequestHandler):
file_name = None file_name = None
if header.get("type") == "txt": if header.get("type") == "txt":
file_name = write_text(data, title=header.get("title")) file_name = write_text(data, title=header.get("title"))
elif header.get("type") == "html":
file_name = write_content(data)
else: else:
print("Unknown type.") print("Unknown type.")
if file_name is None: if file_name is None:
@ -99,18 +102,22 @@ class Handler(socketserver.StreamRequestHandler):
def write_text(data: bytes, title=None): def write_text(data: bytes, title=None):
content = "<pre>{}</pre>".format(data.decode(errors="replace")) content = "<pre>{}</pre>".format(data.decode(errors="replace"))
html = HTML_TEMPLATE.format(title=title or "?", content=content) html = HTML_TEMPLATE.format(title=title or "?", content=content).encode()
write_content(html)
def write_content(data: bytes):
path = str(Path(ARGS.root) / "txt") path = str(Path(ARGS.root) / "txt")
if not os.path.isdir(path): if not os.path.isdir(path):
print(f"Missing '{path}' folder.") print(f"Missing '{path}' folder.")
return None return None
with tempfile.NamedTemporaryFile( with tempfile.NamedTemporaryFile(
mode="wt", mode="wb",
dir=path, dir=path,
suffix=".html", suffix=".html",
delete=False delete=False
) as output_file: ) as output_file:
output_file.write(html) output_file.write(data)
os.chmod(output_file.name, 0o644) os.chmod(output_file.name, 0o644)
return output_file.name return output_file.name

Loading…
Cancel
Save