diff --git a/src/tinyweb/handlers/__init__.py b/src/tinyweb/handlers/__init__.py index 3fde8be..14a37fd 100644 --- a/src/tinyweb/handlers/__init__.py +++ b/src/tinyweb/handlers/__init__.py @@ -18,7 +18,6 @@ from ._helpers import ( from .search import handle_search from .pages import ( handle_add_form, handle_add_submit, handle_add_manual_submit, - handle_page_view, handle_pages, _render_bulk_delete_confirm, handle_bulk_action, handle_edit_form, handle_edit_submit, handle_delete_confirm, handle_delete, @@ -66,9 +65,6 @@ def _dispatch_inner(data): elif path == "/add": prefill_url = query.get("url", [""])[0].strip() return handle_add_form(prefill_url=prefill_url) - elif path.startswith("/pages/"): - pid = extract_id("/pages/") - return handle_page_view(pid) if pid is not None else _error(400) elif path == "/pages": return handle_pages(query) elif path.startswith("/edit/"): diff --git a/src/tinyweb/handlers/pages.py b/src/tinyweb/handlers/pages.py index be68e18..9d7f96c 100644 --- a/src/tinyweb/handlers/pages.py +++ b/src/tinyweb/handlers/pages.py @@ -29,93 +29,86 @@ def handle_add_form(msg="", action_type="index", prefill_url=""): ) url_value = f'value="{esc(prefill_url)}" ' if prefill_url else "" return _respond( - f"
Add a site to your index
" + f"Add a site to your index — URL or RNS destination hash
" f'" - f"Browse a remote TinyWeb instance over Reticulum
" - f'" f"{msg}
" f'back' ) def handle_add_submit(body): - input_type = body.get("input_type", ["url"])[0] - url = body.get("url", [""])[0].strip() - reticulum_dest = body.get("reticulum_dest", [""])[0].strip().replace("<", "").replace(">", "") - name = body.get("name", [""])[0].strip() + raw = body.get("url", [""])[0].strip().replace("<", "").replace(">", "") note = body.get("note", [""])[0].strip() tags = body.get("tags", [""])[0].strip() - if input_type == "rns": - if not reticulum_dest: - return handle_add_form("RNS destination hash is required.") - if len(reticulum_dest) != 32 or not all(c in "0123456789abcdefABCDEF" for c in reticulum_dest): - return handle_add_form("Invalid RNS destination hash. Must be 32 hex characters.") + if not raw: + return handle_add_form("URL or RNS hash is required.") + + is_rns = ( + len(raw) == 32 + and all(c in "0123456789abcdefABCDEF" for c in raw) + ) + if raw.startswith("rns:") or raw.startswith("RNS:"): + raw = raw[4:] + is_rns = ( + len(raw) == 32 + and all(c in "0123456789abcdefABCDEF" for c in raw) + ) + + if is_rns: from .rns import handle_rns_add_hash - handle_rns_add_hash(reticulum_dest, name) + errs = handle_rns_add_hash(raw) + if errs: + return handle_add_form(f"Hash saved but indexing failed: {'; '.join(errs)}") return _redirect("/") - if input_type == "url": - if not url: - return handle_add_form("URL is required.") - url = clean_url(url) - if not url.startswith(("http://", "https://")): - return handle_add_form("URL must start with http:// or https://") + url = clean_url(raw) + if not url.startswith(("http://", "https://")): + return handle_add_form("Enter a URL (http:// or https://) or a 32-char RNS destination hash.") - try: - title = index_url(url, note) - if tags: - db = get_db() - try: - row = db.execute("SELECT id FROM pages WHERE url = ?", (url,)).fetchone() - if row: - _set_page_tags(row["id"], tags, db) - db.commit() - finally: - return_db(db) - return handle_add_form(f'Indexed: {esc(url)}') - except ValueError as e: - return handle_add_form(f"Error: {esc(str(e))}") - except Exception as e: - error_msg = str(e).lower() - if any(x in error_msg for x in ("block", "cloudflare", "403", "ssl", "handshake", "max retries", "timeout", "connection")): - return _respond( - f"{esc(url)} blocks automated access. " - f"You can still save it manually:
" - f'" - f'back' - ) - return handle_add_form(f"Error: could not fetch or index that URL. {esc(str(e)[:100])}") - else: - return handle_add_form("Invalid input type.") + try: + title = index_url(url, note) + if tags: + db = get_db() + try: + row = db.execute("SELECT id FROM pages WHERE url = ?", (url,)).fetchone() + if row: + _set_page_tags(row["id"], tags, db) + db.commit() + finally: + return_db(db) + return handle_add_form(f'Indexed: {esc(url)}') + except ValueError as e: + return handle_add_form(f"Error: {esc(str(e))}") + except Exception as e: + error_msg = str(e).lower() + if any(x in error_msg for x in ("block", "cloudflare", "403", "ssl", "handshake", "max retries", "timeout", "connection")): + return _respond( + f"{esc(url)} blocks automated access. " + f"You can still save it manually:
" + f'" + f'back' + ) + return handle_add_form(f"Error: could not fetch or index that URL. {esc(str(e)[:100])}") def handle_add_manual_submit(body): @@ -162,32 +155,6 @@ def handle_add_manual_submit(body): return_db(db) -def handle_page_view(page_id): - db = get_db() - try: - row = db.execute("SELECT id, url, title, body, note FROM pages WHERE id = ?", (page_id,)).fetchone() - if not row: - return _error(404) - tags = _get_page_tags(row["id"], db) - tag_links = " ".join(f'[{esc(t)}]' for t in tags) if tags else "" - note_html = f'{esc(row["note"])}
' if row["note"] else "" - title = esc(row["title"] or "(untitled)") - body_html = row["body"] or "(no content)" - url_link = f'source: {esc(row["url"])}
' - return _respond( - f"{esc(msg)}
' if msg else "" @@ -208,10 +175,17 @@ def handle_pages(query=None): if tags: tag_links = " ".join(f'[{esc(t)}]' for t in tags) tags_html = f' {tag_links}' + url = r["url"] + if url.startswith("rns:"): + display_url = url + link_url = f"/rns/{esc(url[4:])}/" + else: + display_url = url + link_url = url items += ( f'{name}' ' | search | browse' - ' | tags | subscriptions | mesh' + ' | tags | subscriptions' f'{forum_link}' ' | customize | about
\n' "