done INTEGRATION WITH GATEWAY TO C++

This commit is contained in:
azeeee05
2026-05-26 17:39:03 +05:30
parent e0d446e95c
commit 301e53d739
4 changed files with 129 additions and 2 deletions
+2 -1
View File
@@ -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)
+35 -1
View File
@@ -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}
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))
+32
View File
@@ -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))
+60
View File
@@ -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))