Fix bugs across app + server, optimize UI/UX, add Gitea CI
Bug fixes (Flutter): - Wrap multi-statement DB writes (insert/update/delete note, deleteDocument, deletePageData, OCR FTS merge, migrations) in transactions to prevent data loss on interruption and a read-modify-write FTS race. - Fix PdfDocument leaks on exception (try/finally dispose) and preserve image aspect ratio when stamping images onto PDF pages. - Guard file-picker against empty selection (was .single -> crash). - Fix eraser ConcurrentModificationError and unmodifiable-list crash on PDF pages; capture page synchronously on save to stop wrong-page data loss. - Fix Riverpod DB-not-ready races, broken pull-to-refresh, settings load race, and search N+1; transform stored annotations on PDF page rotation. - Normalize pen pressure for devices without a pressure range. - PPT: single source of truth for slide strokes so ink displays and exports. UI/UX: - Material 3 typography, theme-aware colors (dark-mode fixes), hover cursors and right-click/visible actions on desktop, keyboard shortcuts (undo/redo/ save/find), toolbar overflow handling, friendlier empty states, semantic OCR status badges, relative timestamps, 1-based page indicators, large-deck PPT navigation, and a scratchpad-scope label in split view. Server (optional backend): - Persist JWT secret (was per-process random), block path traversal in storage, fix CORS '*'+credentials, add OCR job ownership checks, last-writer-wins sync guard, constant-time login, and split out heavy OCR deps so the API/tests run without them. CI: Gitea workflows for format+analyze+test (Linux, system sqlite) and a Windows release build; pristine `flutter analyze`, all Flutter and server tests green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
136
server/badnote_server/ocr/queue.py
Normal file
136
server/badnote_server/ocr/queue.py
Normal file
@@ -0,0 +1,136 @@
|
||||
"""File-based OCR job queue for BadNote."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
from datetime import datetime, timezone
|
||||
from uuid import uuid4
|
||||
|
||||
from ..config import settings
|
||||
|
||||
|
||||
def _queue_dir(subdir: str) -> str:
|
||||
path = os.path.join(settings.queue_path, subdir)
|
||||
os.makedirs(path, exist_ok=True)
|
||||
return path
|
||||
|
||||
|
||||
def _job_path(job_id: str, subdir: str) -> str:
|
||||
return os.path.join(_queue_dir(subdir), f"{job_id}.json")
|
||||
|
||||
|
||||
def _read_job(path: str) -> dict | None:
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
return json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return None
|
||||
|
||||
|
||||
def _write_job(path: str, data: dict) -> None:
|
||||
with open(path, "w") as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
|
||||
def enqueue(job_data: dict) -> str:
|
||||
"""Add a job to the pending queue. Returns job_id."""
|
||||
job_id = job_data.get("id", str(uuid4()))
|
||||
job_data["id"] = job_id
|
||||
job_data["status"] = "pending"
|
||||
job_data["created_at"] = datetime.now(timezone.utc).isoformat()
|
||||
_write_job(_job_path(job_id, "pending"), job_data)
|
||||
return job_id
|
||||
|
||||
|
||||
def dequeue() -> dict | None:
|
||||
"""Move the first pending job to processing. Returns job dict or None."""
|
||||
pending_dir = _queue_dir("pending")
|
||||
try:
|
||||
files = sorted(os.listdir(pending_dir))
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
for fname in files:
|
||||
if not fname.endswith(".json"):
|
||||
continue
|
||||
src = os.path.join(pending_dir, fname)
|
||||
job = _read_job(src)
|
||||
if job is None:
|
||||
continue
|
||||
job["status"] = "processing"
|
||||
dst = _job_path(job["id"], "processing")
|
||||
shutil.move(src, dst)
|
||||
return job
|
||||
return None
|
||||
|
||||
|
||||
def complete(job_id: str, result: str) -> None:
|
||||
"""Mark a job as done with result text."""
|
||||
src = _job_path(job_id, "processing")
|
||||
job = _read_job(src)
|
||||
if job is None:
|
||||
return
|
||||
job["status"] = "done"
|
||||
job["result_text"] = result
|
||||
job["completed_at"] = datetime.now(timezone.utc).isoformat()
|
||||
dst = _job_path(job_id, "done")
|
||||
if os.path.exists(src):
|
||||
os.remove(src)
|
||||
_write_job(dst, job)
|
||||
|
||||
|
||||
def fail(job_id: str, error: str) -> None:
|
||||
"""Mark a job as failed with error message."""
|
||||
src = _job_path(job_id, "processing")
|
||||
job = _read_job(src)
|
||||
if job is None:
|
||||
return
|
||||
job["status"] = "failed"
|
||||
job["error_message"] = error
|
||||
job["completed_at"] = datetime.now(timezone.utc).isoformat()
|
||||
dst = _job_path(job_id, "failed")
|
||||
if os.path.exists(src):
|
||||
os.remove(src)
|
||||
_write_job(dst, job)
|
||||
|
||||
|
||||
def get_status(job_id: str) -> dict | None:
|
||||
"""Check all directories for a job and return its data."""
|
||||
for subdir in ("pending", "processing", "done", "failed"):
|
||||
job = _read_job(_job_path(job_id, subdir))
|
||||
if job is not None:
|
||||
return job
|
||||
return None
|
||||
|
||||
|
||||
def get_jobs_for_note(note_id: str) -> list[dict]:
|
||||
"""Return all completed OCR jobs for a given note_id."""
|
||||
results = []
|
||||
for subdir in ("done", "pending", "processing", "failed"):
|
||||
dir_path = _queue_dir(subdir)
|
||||
try:
|
||||
for fname in os.listdir(dir_path):
|
||||
if not fname.endswith(".json"):
|
||||
continue
|
||||
job = _read_job(os.path.join(dir_path, fname))
|
||||
if job and job.get("note_id") == note_id:
|
||||
results.append(job)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
return results
|
||||
|
||||
|
||||
def get_jobs_for_document(document_id: str) -> list[dict]:
|
||||
"""Return all OCR jobs for a given document_id."""
|
||||
results = []
|
||||
for subdir in ("done", "pending", "processing", "failed"):
|
||||
dir_path = _queue_dir(subdir)
|
||||
try:
|
||||
for fname in os.listdir(dir_path):
|
||||
if not fname.endswith(".json"):
|
||||
continue
|
||||
job = _read_job(os.path.join(dir_path, fname))
|
||||
if job and job.get("document_id") == document_id:
|
||||
results.append(job)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
return results
|
||||
Reference in New Issue
Block a user