Files
simple-nanoshare/link_composer/base_n.py
T
daniel156161 ed62837f6d
Build and Push Docker Container / build-and-push (push) Successful in 1m36s
feat: add self-contained link composer
- Add structured URL codec with compact alphabets and dictionary support.
- Add public decode, redirect, and QR endpoints for composed links.
- Add authenticated encode API and link composer UI.
- Generate PNG QR codes via qrcode with Pillow support.
- Register the new blueprint and navigation entry.
- Cover public decode, auth gating, redirect URL parsing, and QR output.
- Bump NanoShare to 1.27.0 and lock new dependencies.
2026-08-14 08:56:42 +02:00

29 lines
825 B
Python

def encode_int(value: int, alphabet: str) -> str:
if value < 0:
raise ValueError("cannot encode negative integers")
if value == 0:
return alphabet[0]
base = len(alphabet)
out = []
while value:
value, digit = divmod(value, base)
out.append(alphabet[digit])
return "".join(reversed(out))
def decode_int(text: str, alphabet: str) -> int:
index = {char: i for i, char in enumerate(alphabet)}
value = 0
base = len(alphabet)
for char in text:
if char not in index:
raise ValueError(f"invalid character for alphabet: {char!r}")
value = value * base + index[char]
return value
def bytes_to_int(data: bytes) -> int:
return int.from_bytes(data, "big") if data else 0
def int_to_bytes(value: int, length: int) -> bytes:
return value.to_bytes(length, "big") if length else b""