rdubwiley commited on
Commit
ac6caaa
·
verified ·
1 Parent(s): 5be21bb

Deploy Agenda Parser

Browse files
Files changed (1) hide show
  1. webapp/backend.py +37 -1
webapp/backend.py CHANGED
@@ -117,6 +117,12 @@ SAMPLES: list[dict] = [
117
  },
118
  ]
119
  _SAMPLE_BY_ID = {s["id"]: s for s in SAMPLES}
 
 
 
 
 
 
120
 
121
 
122
  def list_samples() -> list[dict]:
@@ -142,7 +148,12 @@ def ingest_sample(sample_id: str) -> dict:
142
  path = _SAMPLES_DIR / s["file"]
143
  if not path.exists():
144
  return {"error": f"Sample '{sample_id}' is not bundled on this server."}
145
- return upload_packet(path.read_bytes(), s["file"], s["agenda_pages"])
 
 
 
 
 
146
 
147
 
148
  # --------------------------------------------------------------------------- #
@@ -905,9 +916,34 @@ def cached_packet(upload_id: str) -> tuple[Path, str] | None:
905
  if meta is not None and Path(meta["path"]).exists():
906
  _PKT_META.move_to_end(upload_id)
907
  return Path(meta["path"]), meta["name"]
 
 
 
 
 
 
 
908
  return None
909
 
910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
911
  def _extract_pages_text(src: bytes | str) -> list[str]:
912
  """Per-page text via pypdfium2 (PDFium native).
913
 
 
117
  },
118
  ]
119
  _SAMPLE_BY_ID = {s["id"]: s for s in SAMPLES}
120
+ _SAMPLE_UID_PREFIX = "sample-"
121
+
122
+
123
+ def _sample_uid(sample_id: str) -> str:
124
+ """Stable upload_id for a bundled sample (so its PDF is always re-derivable)."""
125
+ return f"{_SAMPLE_UID_PREFIX}{sample_id}"
126
 
127
 
128
  def list_samples() -> list[dict]:
 
148
  path = _SAMPLES_DIR / s["file"]
149
  if not path.exists():
150
  return {"error": f"Sample '{sample_id}' is not bundled on this server."}
151
+ # Deterministic upload_id (vs a random one) so the cached PDF is re-derivable from
152
+ # the bundle: cached_packet rehydrates a sample on a miss (Space restart / LRU
153
+ # eviction / a saved sample from a prior session), so sample downloads & page views
154
+ # never 404 the way an expired user upload would.
155
+ return upload_packet(path.read_bytes(), s["file"], s["agenda_pages"],
156
+ upload_id=_sample_uid(sid))
157
 
158
 
159
  # --------------------------------------------------------------------------- #
 
916
  if meta is not None and Path(meta["path"]).exists():
917
  _PKT_META.move_to_end(upload_id)
918
  return Path(meta["path"]), meta["name"]
919
+ # Bundled samples are always re-derivable from disk: on a cache miss (Space restart,
920
+ # LRU eviction, or a saved sample from a prior session), rehydrate from samples/
921
+ # rather than 404. User uploads have no such source, so they still surface no_packet.
922
+ if upload_id.startswith(_SAMPLE_UID_PREFIX):
923
+ s = _SAMPLE_BY_ID.get(upload_id[len(_SAMPLE_UID_PREFIX):])
924
+ if s is not None and (_SAMPLES_DIR / s["file"]).exists():
925
+ return _rehydrate_sample(upload_id, s)
926
  return None
927
 
928
 
929
+ def _rehydrate_sample(upload_id: str, s: dict) -> tuple[Path, str] | None:
930
+ """Re-cache a bundled sample's PDF on disk + register its meta (no re-parse), so the
931
+ /packet route can serve it after the in-memory cache lost it."""
932
+ _PKT_DIR.mkdir(parents=True, exist_ok=True)
933
+ safe = re.sub(r"[^A-Za-z0-9_-]", "_", upload_id)
934
+ path = _PKT_DIR / f"{safe}.pdf"
935
+ try:
936
+ if not path.exists():
937
+ path.write_bytes((_SAMPLES_DIR / s["file"]).read_bytes())
938
+ except OSError:
939
+ return None
940
+ with _PKT_LOCK:
941
+ _PKT_META[upload_id] = {"path": str(path), "name": s["file"],
942
+ "agenda_pages": s["agenda_pages"]}
943
+ _PKT_META.move_to_end(upload_id)
944
+ return path, s["file"]
945
+
946
+
947
  def _extract_pages_text(src: bytes | str) -> list[str]:
948
  """Per-page text via pypdfium2 (PDFium native).
949