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:
0
server/badnote_server/ocr/__init__.py
Normal file
0
server/badnote_server/ocr/__init__.py
Normal file
78
server/badnote_server/ocr/engine.py
Normal file
78
server/badnote_server/ocr/engine.py
Normal file
@@ -0,0 +1,78 @@
|
||||
"""OCR engine for BadNote using EasyOCR.
|
||||
|
||||
Lightweight handwriting-capable OCR using EasyOCR with CPU-only inference.
|
||||
Suitable for Zen2 APU 25W / 16GB RAM (~200MB memory once loaded).
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# NOTE: `easyocr` (and its torch dependency) is heavy and optional. It is
|
||||
# imported lazily inside the engine so that importing the FastAPI app — and
|
||||
# running its test suite — does not require the OCR dependencies. Install them
|
||||
# with `pip install -r requirements-ocr.txt` when running the OCR worker.
|
||||
|
||||
|
||||
class OcrEngine:
|
||||
"""EasyOCR-based text recognition engine.
|
||||
|
||||
Lazy-loads the reader on first use to avoid startup overhead.
|
||||
Supports Chinese (simplified) + English. Runs on CPU only.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self._reader = None
|
||||
|
||||
def _ensure_reader(self):
|
||||
"""Lazy-initialize the EasyOCR reader."""
|
||||
if self._reader is None:
|
||||
import easyocr # imported lazily; see module docstring note
|
||||
|
||||
logger.info("Loading EasyOCR reader (ch_sim + en, CPU)...")
|
||||
self._reader = easyocr.Reader(['ch_sim', 'en'], gpu=False)
|
||||
logger.info("EasyOCR reader loaded")
|
||||
|
||||
async def recognize(self, image_bytes: bytes) -> str:
|
||||
"""Recognize text from image bytes.
|
||||
|
||||
Args:
|
||||
image_bytes: Raw image file bytes (PNG, JPEG, etc.)
|
||||
|
||||
Returns:
|
||||
Recognized text as a single string, or empty string on failure.
|
||||
"""
|
||||
if not image_bytes:
|
||||
return ""
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
try:
|
||||
self._ensure_reader()
|
||||
results = await loop.run_in_executor(
|
||||
None, self._reader.readtext, image_bytes
|
||||
)
|
||||
# results is a list of (bbox, text, confidence) tuples
|
||||
text_parts = [text for _, text, _ in results if text.strip()]
|
||||
return ' '.join(text_parts)
|
||||
except Exception as exc:
|
||||
logger.error("OCR recognition failed: %s", exc)
|
||||
return ""
|
||||
|
||||
async def recognize_file(self, image_path: str) -> str:
|
||||
"""Recognize text from an image file path.
|
||||
|
||||
Args:
|
||||
image_path: Path to the image file on disk.
|
||||
|
||||
Returns:
|
||||
Recognized text as a single string, or empty string on failure.
|
||||
"""
|
||||
try:
|
||||
with open(image_path, 'rb') as f:
|
||||
image_bytes = f.read()
|
||||
return await self.recognize(image_bytes)
|
||||
except Exception as exc:
|
||||
logger.error("Failed to read image file %s: %s", image_path, exc)
|
||||
return ""
|
||||
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
|
||||
69
server/badnote_server/ocr/worker.py
Normal file
69
server/badnote_server/ocr/worker.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""Background OCR worker for BadNote.
|
||||
|
||||
Polls the file-based queue and processes jobs using the OcrEngine.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
|
||||
from ..config import settings
|
||||
from .engine import OcrEngine
|
||||
from . import queue as job_queue
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def run_worker(poll_interval: int = 5) -> None:
|
||||
"""Poll the queue and process OCR jobs.
|
||||
|
||||
Loads images from the job's image_path and runs them through EasyOCR.
|
||||
"""
|
||||
# Ensure queue dirs exist
|
||||
for subdir in ("pending", "processing", "done", "failed"):
|
||||
os.makedirs(os.path.join(settings.queue_path, subdir), exist_ok=True)
|
||||
|
||||
engine = OcrEngine()
|
||||
logger.info("OCR worker started (poll_interval=%ds)", poll_interval)
|
||||
|
||||
while True:
|
||||
job = job_queue.dequeue()
|
||||
if job is not None:
|
||||
job_id = job["id"]
|
||||
logger.info("Processing OCR job %s", job_id)
|
||||
try:
|
||||
# Read image from the path specified in the job
|
||||
image_path = job.get("image_path", "")
|
||||
if image_path and os.path.exists(image_path):
|
||||
result = await engine.recognize_file(image_path)
|
||||
else:
|
||||
# Fall back to image_bytes if provided inline
|
||||
image_bytes = job.get("image_bytes", b"")
|
||||
if isinstance(image_bytes, str):
|
||||
import base64
|
||||
image_bytes = base64.b64decode(image_bytes)
|
||||
result = await engine.recognize(image_bytes)
|
||||
|
||||
job_queue.complete(job_id, result)
|
||||
logger.info("OCR job %s completed: %d chars", job_id, len(result))
|
||||
except Exception as exc:
|
||||
logger.error("OCR job %s failed: %s", job_id, exc)
|
||||
job_queue.fail(job_id, str(exc))
|
||||
else:
|
||||
await asyncio.sleep(poll_interval)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Entry point for `python -m badnote_server.ocr.worker`."""
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
||||
)
|
||||
try:
|
||||
asyncio.run(run_worker())
|
||||
except KeyboardInterrupt:
|
||||
logger.info("OCR worker stopped")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user