torrent: index.html links thumbnails and main file with relative paths

This commit is contained in:
russell@unturf.com 2026-04-06 19:36:45 -04:00
parent ad82b812aa
commit 3faf55203b

View file

@ -156,6 +156,7 @@ def generate_torrent(s3_client, bucket, bundle_name, files, description,
os.makedirs(bundle_dir)
webseeds = []
downloaded = set() # filenames that actually landed on disk
# 1. Download each file from S3 into the bundle directory.
for f in files:
@ -163,6 +164,7 @@ def generate_torrent(s3_client, bucket, bundle_name, files, description,
log.info("torrent: downloading s3://%s/%s → %s", bucket, f["s3_key"], f["filename"])
try:
s3_client.download_file(bucket, f["s3_key"], local_path)
downloaded.add(f["filename"])
except Exception:
log.warning("torrent: skipping missing file s3://%s/%s", bucket, f["s3_key"])
continue
@ -170,25 +172,65 @@ def generate_torrent(s3_client, bucket, bundle_name, files, description,
webseeds.append(f["webseed_url"])
# 2. Write index.md + index.html into the bundle directory.
# Separate the main file (content/preview) from thumbnails using the
# filename prefix — build_bundle_files names them content.*, preview.*,
# and thumbnail1..4.*.
main_file = next(
(f for f in files
if f["filename"].startswith(("content.", "preview."))
and f["filename"] in downloaded),
None,
)
thumb_files = [
f for f in files
if f["filename"].startswith("thumbnail")
and f["filename"] in downloaded
]
if description:
with open(os.path.join(bundle_dir, "index.md"), "w", encoding="utf-8") as fh:
fh.write(description)
try:
import markdown
html_body = markdown.markdown(description, extensions=["fenced_code", "tables"])
except ImportError:
# Fallback: wrap raw markdown in a <pre> if markdown lib not installed.
html_body = f"<pre>{description}</pre>"
html = (
"<!DOCTYPE html>\n<html>\n<head>"
"<meta charset='utf-8'>"
f"<title>{bundle_name}</title>"
"</head>\n<body>\n"
+ html_body
+ "\n</body>\n</html>\n"
try:
import markdown as _md
html_body = _md.markdown(description or "", extensions=["fenced_code", "tables"])
except ImportError:
html_body = f"<pre>{description}</pre>" if description else ""
# Thumbnail gallery — relative links, rendered inline.
thumb_html = ""
if thumb_files:
imgs = "".join(
f'<a href="{f["filename"]}">'
f'<img src="{f["filename"]}" style="max-width:320px;margin:4px" /></a>'
for f in thumb_files
)
with open(os.path.join(bundle_dir, "index.html"), "w", encoding="utf-8") as fh:
fh.write(html)
thumb_html = f'<div class="thumbnails" style="display:flex;flex-wrap:wrap;gap:4px">{imgs}</div>\n'
# Main file link — download anchor.
main_html = ""
if main_file:
label = "Download" if main_file["filename"].startswith("content.") else "Preview"
main_html = (
f'<p><a href="{main_file["filename"]}" download>'
f'{label}: {main_file["filename"]}</a></p>\n'
)
html = (
"<!DOCTYPE html>\n<html>\n<head>"
"<meta charset='utf-8'>"
f"<title>{bundle_name}</title>"
"<style>body{font-family:sans-serif;max-width:800px;margin:2em auto;padding:0 1em}"
"img{border-radius:4px}a{color:#16a34a}</style>"
"</head>\n<body>\n"
f"<h1>{bundle_name}</h1>\n"
+ thumb_html
+ main_html
+ html_body
+ "\n</body>\n</html>\n"
)
with open(os.path.join(bundle_dir, "index.html"), "w", encoding="utf-8") as fh:
fh.write(html)
# 3. Build multi-file .torrent from the bundle directory.
t = torf.Torrent(