#!/usr/bin/env python3 """Split the measured snapshot's base64 into parts the shared store will actually accept. Measured constraints of the store's upload route (`POST /files`), this turn, from the server's own refusals -- see `upload-limit.json` and `probe_upload_limit.py`: * the route takes TEXT only (`content` is a JSON string), so the 12,614,410-byte zip cannot travel as itself; it goes base64 (17,040,522 characters); * the extension must be on a fixed allow-list (`.b64` is NOT: HTTP 400 "extension .b64 not allowed; allowed: md, txt, json, ...", independent of size), so the parts are named `.txt`; * a file may not exceed 5,242,880 bytes of content (HTTP 400 "file exceeds 5242880 bytes"); 4 MiB content is accepted, 6 MiB is refused. 8 MiB additionally trips a separate reverse-proxy limit (HTTP 413), which is why the cap is the 400 and not the 413. Hence 4 parts. This script is deterministic (no compression, fixed chunk size) so the part boundaries, and therefore every part's sha256, can be reproduced. usage: pack_parts.py [pad-extension] writes /snapshot-2026-09-20.zip.b64.part1of4.txt ... and /parts.json """ import base64 import hashlib import json import os import sys sys.stdout.reconfigure(encoding="utf-8", errors="replace") CAP = 5242880 # measured: the server's own "file exceeds 5242880 bytes" STEM = "snapshot-2026-09-20.zip.b64" def sha256(b): return hashlib.sha256(b).hexdigest() def main(): src, out_dir = sys.argv[1], sys.argv[2] os.makedirs(out_dir, exist_ok=True) raw = open(src, "rb").read() b64 = base64.b64encode(raw).decode("ascii") n = (len(b64) + CAP - 1) // CAP rec = {"archive": os.path.basename(src), "archive_bytes": len(raw), "archive_sha256": sha256(raw), "base64_chars": len(b64), "cap_chars": CAP, "parts": []} for i in range(n): chunk = b64[i * CAP:(i + 1) * CAP] name = "%s.part%dof%d.txt" % (STEM, i + 1, n) p = os.path.join(out_dir, name) with open(p, "w", encoding="ascii", newline="") as f: f.write(chunk) rec["parts"].append({"name": name, "index": i + 1, "chars": len(chunk), "sha256": sha256(chunk.encode("ascii"))}) print("%-44s %9d chars %s" % (name, len(chunk), rec["parts"][-1]["sha256"][:16])) # the reassembly identity, checked here rather than assumed joined = "".join(open(os.path.join(out_dir, x["name"]), encoding="ascii").read() for x in rec["parts"]) rec["reassembles_to_archive_sha256"] = sha256(base64.b64decode(joined)) rec["reassembly_ok"] = rec["reassembles_to_archive_sha256"] == rec["archive_sha256"] json.dump(rec, open(os.path.join(out_dir, "parts.json"), "w", encoding="utf-8"), indent=1, ensure_ascii=False) print("archive %s | %d bytes | sha256 %s" % (rec["archive"], len(raw), rec["archive_sha256"])) print("parts %d | reassembly_ok %s" % (n, rec["reassembly_ok"])) return 0 if rec["reassembly_ok"] else 1 if __name__ == "__main__": sys.exit(main())