From 301e53d73984419cdd4a07e5709aebddbd32a62d Mon Sep 17 00:00:00 2001 From: azeeee05 Date: Tue, 26 May 2026 17:39:03 +0530 Subject: [PATCH] done INTEGRATION WITH GATEWAY TO C++ --- gateway/app/main.py | 3 +- gateway/app/routers/documents.py | 36 ++++++++++++++++++- gateway/app/routers/info.py | 32 +++++++++++++++++ gateway/app/routers/render.py | 60 ++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 gateway/app/routers/info.py diff --git a/gateway/app/main.py b/gateway/app/main.py index fad421f..d996a4e 100644 --- a/gateway/app/main.py +++ b/gateway/app/main.py @@ -4,7 +4,7 @@ from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from app import __version__ -from app.routers import documents, edits, health, render +from app.routers import documents, edits, health, render, info def create_app() -> FastAPI: @@ -23,6 +23,7 @@ def create_app() -> FastAPI: ) app.include_router(health.router) + app.include_router(info.router) app.include_router(documents.router) app.include_router(render.router) app.include_router(render.compat_router) diff --git a/gateway/app/routers/documents.py b/gateway/app/routers/documents.py index 5e32454..f9f617a 100644 --- a/gateway/app/routers/documents.py +++ b/gateway/app/routers/documents.py @@ -101,4 +101,38 @@ def delete_document(document_id: str): if not deleted: raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found") - return {"success": True} \ No newline at end of file + return {"success": True} + +class DocumentMetadataResponse(BaseModel): + title: str + author: str + creator: str + producer: str + creation_date: str + modification_date: str + +@router.get("/{document_id}/metadata", response_model=DocumentMetadataResponse) +def get_document_metadata(document_id: str) -> DocumentMetadataResponse: + if not engine.is_available(): + raise HTTPException( + status_code=status.HTTP_501_NOT_IMPLEMENTED, + detail="Engine bridge (bindings/python) not yet available." + ) + + d = document_store.get_document(document_id) + if not d: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found") + + try: + doc = d["doc_instance"] + meta = doc.metadata + return DocumentMetadataResponse( + title=meta.title, + author=meta.author, + creator=meta.creator, + producer=meta.producer, + creation_date=meta.creation_date, + modification_date=meta.modification_date + ) + except Exception as e: + raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) \ No newline at end of file diff --git a/gateway/app/routers/info.py b/gateway/app/routers/info.py new file mode 100644 index 0000000..ace7fc8 --- /dev/null +++ b/gateway/app/routers/info.py @@ -0,0 +1,32 @@ +from fastapi import APIRouter, HTTPException, status +from pydantic import BaseModel + +from app.services import engine + +router = APIRouter(prefix="/engine", tags=["info"]) + +class EngineInfoResponse(BaseModel): + version: str + build_info: str + has_pdfium: bool + has_skia: bool + +@router.get("/info", response_model=EngineInfoResponse) +def get_engine_info() -> EngineInfoResponse: + if not engine.is_available(): + raise HTTPException( + status_code=status.HTTP_501_NOT_IMPLEMENTED, + detail="Engine bridge (bindings/python) not yet available." + ) + + pdfengine = engine.require() + + try: + return EngineInfoResponse( + version=pdfengine.engine_version(), + build_info=pdfengine.engine_build_info(), + has_pdfium=pdfengine.engine_has_pdfium(), + has_skia=pdfengine.engine_has_skia() + ) + except Exception as e: + raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) diff --git a/gateway/app/routers/render.py b/gateway/app/routers/render.py index 0ff4d2c..4a485f0 100644 --- a/gateway/app/routers/render.py +++ b/gateway/app/routers/render.py @@ -59,3 +59,63 @@ def render_page_compat(document_id: str, page: int = 0, zoom: float = 1.0, rotat dpi = int(96 * zoom) return render_page(document_id, page, dpi) +@router.get("/{page_index}") +def get_page_info(document_id: str, page_index: int): + if not engine.is_available(): + raise HTTPException( + status_code=status.HTTP_501_NOT_IMPLEMENTED, + detail="Engine bridge (bindings/python) not yet available." + ) + + doc_info = document_store.get_document(document_id) + if not doc_info: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found") + + try: + doc = doc_info["doc_instance"] + page = doc.get_page(page_index) + return {"width": page.width, "height": page.height} + except IndexError: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Page index out of bounds") + except Exception as e: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) + +@router.get("/{page_index}/transform/page-to-device") +def transform_page_to_device(document_id: str, page_index: int, x: float, y: float, device_width: int, device_height: int, rotate: int = 0): + if not engine.is_available(): + raise HTTPException(status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Engine unavailable") + + doc_info = document_store.get_document(document_id) + if not doc_info: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found") + + try: + pdfengine = engine.require() + doc = doc_info["doc_instance"] + page = doc.get_page(page_index) + + pt = pdfengine.Point2D(x=x, y=y) + res = page.page_to_device(pt, device_width, device_height, rotate) + return {"x": res.x, "y": res.y} + except Exception as e: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e)) + +@router.get("/{page_index}/transform/device-to-page") +def transform_device_to_page(document_id: str, page_index: int, x: int, y: int, device_width: int, device_height: int, rotate: int = 0): + if not engine.is_available(): + raise HTTPException(status_code=status.HTTP_501_NOT_IMPLEMENTED, detail="Engine unavailable") + + doc_info = document_store.get_document(document_id) + if not doc_info: + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Document not found") + + try: + pdfengine = engine.require() + doc = doc_info["doc_instance"] + page = doc.get_page(page_index) + + pt = pdfengine.DevicePoint(x=x, y=y) + res = page.device_to_page(pt, device_width, device_height, rotate) + return {"x": res.x, "y": res.y} + except Exception as e: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))