This commit is contained in:
commit
46af290db3
100 files changed
+16663
No files matched your search
@@ -0,0 +1,64 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi.responses import Response
|
||||
from models.user import User
|
||||
from pydantic import BaseModel
|
||||
from services.export_service import export_service
|
||||
|
||||
from routers.auth import get_current_user
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class ExportRequest(BaseModel):
|
||||
project_id: str
|
||||
document_id: Optional[str] = (
|
||||
None # ID книги или главы. Если None — экспортируем всю книгу
|
||||
)
|
||||
format: str # 'epub', 'fb2', 'pdf', 'docx'
|
||||
|
||||
|
||||
def _get_user_project(project_id: str, current_user: User) -> None:
|
||||
"""Проверить что проект принадлежит пользователю"""
|
||||
from services.project_service import project_service
|
||||
|
||||
project = project_service.get_project(project_id)
|
||||
if not project:
|
||||
raise HTTPException(status_code=404, detail="Project not found")
|
||||
if project.user_id != current_user.id:
|
||||
raise HTTPException(status_code=403, detail="Access denied")
|
||||
|
||||
|
||||
@router.post("/export")
|
||||
async def export_book(
|
||||
request: ExportRequest, current_user: User = Depends(get_current_user)
|
||||
):
|
||||
_get_user_project(request.project_id, current_user)
|
||||
try:
|
||||
file_content = export_service.export_document(
|
||||
project_id=request.project_id,
|
||||
user_id=current_user.id,
|
||||
document_id=request.document_id,
|
||||
format=request.format,
|
||||
)
|
||||
|
||||
# Определяем MIME-тип
|
||||
mime_types = {
|
||||
"epub": "application/epub+zip",
|
||||
"fb2": "application/fb2",
|
||||
"pdf": "application/pdf",
|
||||
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
}
|
||||
|
||||
filename = f"export.{request.format}"
|
||||
|
||||
return Response(
|
||||
content=file_content,
|
||||
media_type=mime_types.get(request.format, "application/octet-stream"),
|
||||
headers={"Content-Disposition": f"attachment; filename={filename}"},
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
Reference in new issue
Block a user