Spaces:
Sleeping
Sleeping
Claude commited on
Commit ·
3df5f8f
1
Parent(s): 4f17788
Add Download ZIP button to the review editor
Browse filesA new POST /jobs/{job_id}/zip endpoint takes the editor's current state
plus the data URIs of every currently-picked primary frame, decodes
them into a temp dir alongside the job's in-memory audio bytes, and
calls the same render_from_metadata + bundle_zip path the CLI uses.
The toolbar shows a "Download ZIP" button when the page was rendered
with zip_url set (review mode only). Filenames are validated against
the same regexes used by /alts and /audio so a crafted body can't write
outside the temp dir.
- app/main.py +97 -1
- app/pipeline/html_gen.py +5 -1
- app/templates/review.html +57 -1
app/main.py
CHANGED
|
@@ -17,8 +17,9 @@ from fastapi.staticfiles import StaticFiles
|
|
| 17 |
from fastapi.templating import Jinja2Templates
|
| 18 |
|
| 19 |
from .jobs import Job, JobQuotaExceeded, JobResult, registry
|
| 20 |
-
from .pipeline import bundle_zip, render_guide, render_review
|
| 21 |
from .pipeline.llm_extract import LLMToggles
|
|
|
|
| 22 |
from .pipeline.orchestrator import (
|
| 23 |
PipelineError,
|
| 24 |
PipelineInputs,
|
|
@@ -114,6 +115,7 @@ def _render_to_result(
|
|
| 114 |
alts_url_template: str | None = None,
|
| 115 |
ocr_url_template: str | None = None,
|
| 116 |
audio_url_template: str | None = None,
|
|
|
|
| 117 |
) -> JobResult:
|
| 118 |
audio_paths = audio_paths or [None] * len(segments)
|
| 119 |
common = dict(
|
|
@@ -135,6 +137,7 @@ def _render_to_result(
|
|
| 135 |
alts_url_template=alts_url_template,
|
| 136 |
ocr_url_template=ocr_url_template,
|
| 137 |
audio_url_template=audio_url_template,
|
|
|
|
| 138 |
**common,
|
| 139 |
)
|
| 140 |
return JobResult(body=html.encode("utf-8"), media_type="text/html; charset=utf-8")
|
|
@@ -225,6 +228,7 @@ def _run_job_in_thread(
|
|
| 225 |
audio_url_template=(
|
| 226 |
f"/audio/{job.id}/{{filename}}" if format == "review" else None
|
| 227 |
),
|
|
|
|
| 228 |
)
|
| 229 |
registry.finish_success(job, job_result)
|
| 230 |
except PipelineError as exc:
|
|
@@ -415,6 +419,7 @@ async def fetch_result(job_id: str) -> Response:
|
|
| 415 |
|
| 416 |
|
| 417 |
_ALT_FILENAME_RE = __import__("re").compile(r"^scene_\d{4}_alt\d+\.(jpg|jpeg|png)$")
|
|
|
|
| 418 |
_AUDIO_FILENAME_RE = __import__("re").compile(r"^segment_\d{4}\.mp3$")
|
| 419 |
|
| 420 |
|
|
@@ -505,6 +510,97 @@ async def fetch_ocr(job_id: str, filename: str) -> Response:
|
|
| 505 |
)
|
| 506 |
|
| 507 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 508 |
# Backwards-compat: keep a synchronous /process endpoint for non-browser clients.
|
| 509 |
@app.post("/process")
|
| 510 |
async def process(
|
|
|
|
| 17 |
from fastapi.templating import Jinja2Templates
|
| 18 |
|
| 19 |
from .jobs import Job, JobQuotaExceeded, JobResult, registry
|
| 20 |
+
from .pipeline import bundle_zip, render_from_metadata, render_guide, render_review
|
| 21 |
from .pipeline.llm_extract import LLMToggles
|
| 22 |
+
from .pipeline.metadata import PageMetadata
|
| 23 |
from .pipeline.orchestrator import (
|
| 24 |
PipelineError,
|
| 25 |
PipelineInputs,
|
|
|
|
| 115 |
alts_url_template: str | None = None,
|
| 116 |
ocr_url_template: str | None = None,
|
| 117 |
audio_url_template: str | None = None,
|
| 118 |
+
zip_url: str | None = None,
|
| 119 |
) -> JobResult:
|
| 120 |
audio_paths = audio_paths or [None] * len(segments)
|
| 121 |
common = dict(
|
|
|
|
| 137 |
alts_url_template=alts_url_template,
|
| 138 |
ocr_url_template=ocr_url_template,
|
| 139 |
audio_url_template=audio_url_template,
|
| 140 |
+
zip_url=zip_url,
|
| 141 |
**common,
|
| 142 |
)
|
| 143 |
return JobResult(body=html.encode("utf-8"), media_type="text/html; charset=utf-8")
|
|
|
|
| 228 |
audio_url_template=(
|
| 229 |
f"/audio/{job.id}/{{filename}}" if format == "review" else None
|
| 230 |
),
|
| 231 |
+
zip_url=f"/jobs/{job.id}/zip" if format == "review" else None,
|
| 232 |
)
|
| 233 |
registry.finish_success(job, job_result)
|
| 234 |
except PipelineError as exc:
|
|
|
|
| 419 |
|
| 420 |
|
| 421 |
_ALT_FILENAME_RE = __import__("re").compile(r"^scene_\d{4}_alt\d+\.(jpg|jpeg|png)$")
|
| 422 |
+
_PRIMARY_FILENAME_RE = __import__("re").compile(r"^scene_\d{4}\.(jpg|jpeg|png)$")
|
| 423 |
_AUDIO_FILENAME_RE = __import__("re").compile(r"^segment_\d{4}\.mp3$")
|
| 424 |
|
| 425 |
|
|
|
|
| 510 |
)
|
| 511 |
|
| 512 |
|
| 513 |
+
_DATA_URI_RE = __import__("re").compile(r"^data:[^;]+;base64,(.+)$", __import__("re").DOTALL)
|
| 514 |
+
|
| 515 |
+
|
| 516 |
+
@app.post("/jobs/{job_id}/zip")
|
| 517 |
+
async def fetch_job_zip(job_id: str, request: Request) -> Response:
|
| 518 |
+
"""Bundle the current editor state into a downloadable .zip.
|
| 519 |
+
|
| 520 |
+
The editor POSTs its in-memory state plus the data URIs of every
|
| 521 |
+
currently-picked primary frame (alt or original — the editor's main
|
| 522 |
+
<img> src is the source of truth). The server decodes those bytes,
|
| 523 |
+
pulls audio bytes from the in-memory job, calls the same
|
| 524 |
+
render_from_metadata + bundle_zip path the CLI uses, and returns
|
| 525 |
+
the bundle. No request body is persisted; the temp dir is deleted
|
| 526 |
+
in `finally`.
|
| 527 |
+
"""
|
| 528 |
+
job = registry.get(job_id)
|
| 529 |
+
if not job:
|
| 530 |
+
raise HTTPException(404, "Unknown job id.")
|
| 531 |
+
|
| 532 |
+
try:
|
| 533 |
+
body = await request.json()
|
| 534 |
+
except Exception:
|
| 535 |
+
raise HTTPException(400, "Invalid JSON body.")
|
| 536 |
+
state = body.get("state") if isinstance(body, dict) else None
|
| 537 |
+
primary_uris = body.get("primary_image_data_uris") if isinstance(body, dict) else None
|
| 538 |
+
if not isinstance(state, dict) or not isinstance(primary_uris, dict):
|
| 539 |
+
raise HTTPException(400, "Body must be {state, primary_image_data_uris}.")
|
| 540 |
+
|
| 541 |
+
try:
|
| 542 |
+
page = PageMetadata.model_validate(state)
|
| 543 |
+
except Exception as exc:
|
| 544 |
+
raise HTTPException(422, f"Invalid metadata: {exc}")
|
| 545 |
+
|
| 546 |
+
import base64
|
| 547 |
+
|
| 548 |
+
workdir = Path(tempfile.mkdtemp(prefix="vgm_zip_"))
|
| 549 |
+
try:
|
| 550 |
+
frames_dir = workdir / "static"
|
| 551 |
+
frames_dir.mkdir()
|
| 552 |
+
|
| 553 |
+
# Decode and write the picked primary frame bytes for each segment.
|
| 554 |
+
# Filenames must match what the metadata references — only patterns
|
| 555 |
+
# the pipeline produces are accepted.
|
| 556 |
+
for filename, uri in primary_uris.items():
|
| 557 |
+
if not isinstance(filename, str) or not isinstance(uri, str):
|
| 558 |
+
continue
|
| 559 |
+
if not _ALT_FILENAME_RE.match(filename) and not _PRIMARY_FILENAME_RE.match(filename):
|
| 560 |
+
continue
|
| 561 |
+
m = _DATA_URI_RE.match(uri)
|
| 562 |
+
if not m:
|
| 563 |
+
continue
|
| 564 |
+
try:
|
| 565 |
+
data = base64.b64decode(m.group(1), validate=True)
|
| 566 |
+
except Exception:
|
| 567 |
+
continue
|
| 568 |
+
(frames_dir / filename).write_bytes(data)
|
| 569 |
+
|
| 570 |
+
# Pull audio bytes from the job (no client upload needed) and
|
| 571 |
+
# write them next to the frames so the bundled HTML can link
|
| 572 |
+
# via `static/<audio>`.
|
| 573 |
+
for fn, data in job.audio_bytes.items():
|
| 574 |
+
if not _AUDIO_FILENAME_RE.match(fn):
|
| 575 |
+
continue
|
| 576 |
+
(frames_dir / fn).write_bytes(data)
|
| 577 |
+
|
| 578 |
+
# Render with image_dir_name="static" and inline=False; the HTML
|
| 579 |
+
# will link relatively to files we just wrote.
|
| 580 |
+
try:
|
| 581 |
+
html = render_from_metadata(page, frames_dir, mode="guide", image_dir_name="static")
|
| 582 |
+
except Exception as exc:
|
| 583 |
+
raise HTTPException(500, f"Render failed: {exc}")
|
| 584 |
+
|
| 585 |
+
frame_paths = [frames_dir / s.image_filename for s in page.segments if s.image_filename]
|
| 586 |
+
audio_paths = [
|
| 587 |
+
frames_dir / s.audio_filename for s in page.segments if s.audio_filename
|
| 588 |
+
]
|
| 589 |
+
zip_bytes = bundle_zip(
|
| 590 |
+
html,
|
| 591 |
+
frame_paths,
|
| 592 |
+
audio_paths=audio_paths,
|
| 593 |
+
)
|
| 594 |
+
filename = _safe_filename(page.title) + ".zip"
|
| 595 |
+
return Response(
|
| 596 |
+
content=zip_bytes,
|
| 597 |
+
media_type="application/zip",
|
| 598 |
+
headers={"Content-Disposition": f'attachment; filename="{filename}"'},
|
| 599 |
+
)
|
| 600 |
+
finally:
|
| 601 |
+
shutil.rmtree(workdir, ignore_errors=True)
|
| 602 |
+
|
| 603 |
+
|
| 604 |
# Backwards-compat: keep a synchronous /process endpoint for non-browser clients.
|
| 605 |
@app.post("/process")
|
| 606 |
async def process(
|
app/pipeline/html_gen.py
CHANGED
|
@@ -208,13 +208,16 @@ def render_review(
|
|
| 208 |
alts_url_template: str | None = None,
|
| 209 |
ocr_url_template: str | None = None,
|
| 210 |
audio_url_template: str | None = None,
|
|
|
|
| 211 |
) -> str:
|
| 212 |
"""Render an in-browser editor page. The primary image must be inlined
|
| 213 |
(so save/reopen still shows the picked frame), but alternates can be
|
| 214 |
lazy-fetched: pass `alts_url_template` containing `{filename}` and the
|
| 215 |
template will render `<img src=...>` URLs instead of inlined data URIs
|
| 216 |
for the alternate-frame thumbnails. `ocr_url_template` does the same
|
| 217 |
-
for on-demand OCR of an alt when the reviewer picks one.
|
|
|
|
|
|
|
| 218 |
if any(seg.image_data_uri is None for seg in segments):
|
| 219 |
raise ValueError("render_review requires segments built with inline_images=True")
|
| 220 |
template = _env().get_template("review.html")
|
|
@@ -231,6 +234,7 @@ def render_review(
|
|
| 231 |
alts_url_template=alts_url_template,
|
| 232 |
ocr_url_template=ocr_url_template,
|
| 233 |
audio_url_template=audio_url_template,
|
|
|
|
| 234 |
)
|
| 235 |
|
| 236 |
|
|
|
|
| 208 |
alts_url_template: str | None = None,
|
| 209 |
ocr_url_template: str | None = None,
|
| 210 |
audio_url_template: str | None = None,
|
| 211 |
+
zip_url: str | None = None,
|
| 212 |
) -> str:
|
| 213 |
"""Render an in-browser editor page. The primary image must be inlined
|
| 214 |
(so save/reopen still shows the picked frame), but alternates can be
|
| 215 |
lazy-fetched: pass `alts_url_template` containing `{filename}` and the
|
| 216 |
template will render `<img src=...>` URLs instead of inlined data URIs
|
| 217 |
for the alternate-frame thumbnails. `ocr_url_template` does the same
|
| 218 |
+
for on-demand OCR of an alt when the reviewer picks one. `zip_url`
|
| 219 |
+
enables the editor's Download ZIP button — POST the current state to
|
| 220 |
+
that URL to get back a server-rendered bundle."""
|
| 221 |
if any(seg.image_data_uri is None for seg in segments):
|
| 222 |
raise ValueError("render_review requires segments built with inline_images=True")
|
| 223 |
template = _env().get_template("review.html")
|
|
|
|
| 234 |
alts_url_template=alts_url_template,
|
| 235 |
ocr_url_template=ocr_url_template,
|
| 236 |
audio_url_template=audio_url_template,
|
| 237 |
+
zip_url=zip_url,
|
| 238 |
)
|
| 239 |
|
| 240 |
|
app/templates/review.html
CHANGED
|
@@ -228,7 +228,8 @@
|
|
| 228 |
<body>
|
| 229 |
<a href="#main-content" class="skip-link">Skip to main content</a>
|
| 230 |
|
| 231 |
-
<div class="vgm-toolbar" data-editor role="toolbar" aria-label="Study guide editor"
|
|
|
|
| 232 |
<strong>Editor</strong>
|
| 233 |
<button type="button" data-action="save-json">Save edits (JSON)</button>
|
| 234 |
<label class="btn">
|
|
@@ -237,6 +238,9 @@
|
|
| 237 |
</label>
|
| 238 |
<button type="button" data-action="preview-html">Preview</button>
|
| 239 |
<button type="button" data-action="download-html">Download final HTML</button>
|
|
|
|
|
|
|
|
|
|
| 240 |
<span class="vgm-status" role="status" aria-live="polite"></span>
|
| 241 |
</div>
|
| 242 |
|
|
@@ -1032,9 +1036,61 @@
|
|
| 1032 |
setStatus('Preview opened in a new tab.');
|
| 1033 |
}
|
| 1034 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1035 |
toolbar.querySelector('[data-action="save-json"]').addEventListener('click', saveJSON);
|
| 1036 |
toolbar.querySelector('[data-action="preview-html"]').addEventListener('click', previewHTML);
|
| 1037 |
toolbar.querySelector('[data-action="download-html"]').addEventListener('click', downloadHTML);
|
|
|
|
|
|
|
| 1038 |
toolbar.querySelector('[data-action="load-json"]').addEventListener('change', e => {
|
| 1039 |
const f = e.target.files && e.target.files[0];
|
| 1040 |
if (f) loadJSON(f);
|
|
|
|
| 228 |
<body>
|
| 229 |
<a href="#main-content" class="skip-link">Skip to main content</a>
|
| 230 |
|
| 231 |
+
<div class="vgm-toolbar" data-editor role="toolbar" aria-label="Study guide editor"
|
| 232 |
+
{% if zip_url %}data-zip-url="{{ zip_url }}"{% endif %}>
|
| 233 |
<strong>Editor</strong>
|
| 234 |
<button type="button" data-action="save-json">Save edits (JSON)</button>
|
| 235 |
<label class="btn">
|
|
|
|
| 238 |
</label>
|
| 239 |
<button type="button" data-action="preview-html">Preview</button>
|
| 240 |
<button type="button" data-action="download-html">Download final HTML</button>
|
| 241 |
+
{% if zip_url %}
|
| 242 |
+
<button type="button" data-action="download-zip">Download ZIP</button>
|
| 243 |
+
{% endif %}
|
| 244 |
<span class="vgm-status" role="status" aria-live="polite"></span>
|
| 245 |
</div>
|
| 246 |
|
|
|
|
| 1036 |
setStatus('Preview opened in a new tab.');
|
| 1037 |
}
|
| 1038 |
|
| 1039 |
+
async function downloadZIP() {
|
| 1040 |
+
const zipUrl = toolbar.getAttribute('data-zip-url');
|
| 1041 |
+
if (!zipUrl) {
|
| 1042 |
+
setStatus('ZIP download not available in this session.');
|
| 1043 |
+
return;
|
| 1044 |
+
}
|
| 1045 |
+
setStatus('Building ZIP…');
|
| 1046 |
+
// For each segment, read the currently-picked frame's data URI from
|
| 1047 |
+
// its main <img>. The server uses these bytes (the picked frame may
|
| 1048 |
+
// be an alt) plus the audio bytes it kept in memory to build the
|
| 1049 |
+
// bundle. Audio doesn't need to be sent — the server already has it.
|
| 1050 |
+
const state = readState();
|
| 1051 |
+
const primaryImageDataUris = {};
|
| 1052 |
+
getSegmentEls().forEach(s => {
|
| 1053 |
+
const img = s.querySelector('[data-bind="image"]');
|
| 1054 |
+
if (!img) return;
|
| 1055 |
+
const fn = img.getAttribute('data-image-filename') || '';
|
| 1056 |
+
const src = img.getAttribute('src') || '';
|
| 1057 |
+
if (fn && src.startsWith('data:')) primaryImageDataUris[fn] = src;
|
| 1058 |
+
});
|
| 1059 |
+
let resp;
|
| 1060 |
+
try {
|
| 1061 |
+
resp = await fetch(zipUrl, {
|
| 1062 |
+
method: 'POST',
|
| 1063 |
+
headers: { 'Content-Type': 'application/json' },
|
| 1064 |
+
body: JSON.stringify({
|
| 1065 |
+
state: state,
|
| 1066 |
+
primary_image_data_uris: primaryImageDataUris,
|
| 1067 |
+
}),
|
| 1068 |
+
});
|
| 1069 |
+
} catch (err) {
|
| 1070 |
+
setStatus('Network error while building ZIP: ' + (err.message || err));
|
| 1071 |
+
return;
|
| 1072 |
+
}
|
| 1073 |
+
if (!resp.ok) {
|
| 1074 |
+
let detail = '';
|
| 1075 |
+
try { detail = (await resp.json()).detail || ''; } catch (_) {}
|
| 1076 |
+
setStatus('ZIP build failed: HTTP ' + resp.status + (detail ? ' — ' + detail : ''));
|
| 1077 |
+
return;
|
| 1078 |
+
}
|
| 1079 |
+
const blob = await resp.blob();
|
| 1080 |
+
const url = URL.createObjectURL(blob);
|
| 1081 |
+
const a = document.createElement('a');
|
| 1082 |
+
a.href = url;
|
| 1083 |
+
a.download = safeName(titleEl.textContent.trim()) + '.zip';
|
| 1084 |
+
document.body.appendChild(a); a.click(); a.remove();
|
| 1085 |
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
| 1086 |
+
setStatus('Downloaded ZIP.');
|
| 1087 |
+
}
|
| 1088 |
+
|
| 1089 |
toolbar.querySelector('[data-action="save-json"]').addEventListener('click', saveJSON);
|
| 1090 |
toolbar.querySelector('[data-action="preview-html"]').addEventListener('click', previewHTML);
|
| 1091 |
toolbar.querySelector('[data-action="download-html"]').addEventListener('click', downloadHTML);
|
| 1092 |
+
const zipBtn = toolbar.querySelector('[data-action="download-zip"]');
|
| 1093 |
+
if (zipBtn) zipBtn.addEventListener('click', downloadZIP);
|
| 1094 |
toolbar.querySelector('[data-action="load-json"]').addEventListener('change', e => {
|
| 1095 |
const f = e.target.files && e.target.files[0];
|
| 1096 |
if (f) loadJSON(f);
|