from urllib.parse import urlsplit _ALLOWED_SCHEMES = {'http', 'https'} def _normalize_host(host: str) -> str: return (host or '').split(',')[0].strip().lower().rstrip('.') def parse_share_link(url: str, host: str, root_path: str = '') -> str | None: """Return the file_id when url points at this instance's /- route.""" try: parts = urlsplit(url) except ValueError: return None if parts.scheme.lower() not in _ALLOWED_SCHEMES: return None if not parts.netloc or _normalize_host(parts.netloc) != _normalize_host(host): return None prefix = (root_path or '').rstrip('/') path = parts.path if prefix: if not path.startswith(prefix + '/'): return None path = path[len(prefix):] if not path.startswith('/-'): return None file_id = path[2:] if not file_id or '/' in file_id: return None return file_id