feat: add portable project export/import ZIP with checksums and atomic restore
This commit is contained in:
+87
-1
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Form
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, Request, Form, UploadFile, File
|
||||
from fastapi.responses import RedirectResponse, Response
|
||||
from sqlalchemy.orm import Session
|
||||
from app.database import database
|
||||
@@ -13,8 +13,11 @@ from app.models.payment import Payment
|
||||
from app.models.work_journal import WorkJournalEntry
|
||||
from typing import List
|
||||
import os
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
|
||||
router = APIRouter()
|
||||
IMPORT_UPLOAD_DIR = Path(__file__).resolve().parents[1] / "uploads" / "project_import"
|
||||
|
||||
|
||||
def _journal_entry_or_404(db: Session, project_id: int, entry_id: int):
|
||||
@@ -64,6 +67,73 @@ async def read_projects(request: Request, db: Session = Depends(get_db)):
|
||||
async def new_project(request: Request):
|
||||
return request.app.templates.TemplateResponse("projects/new.html", {"request": request})
|
||||
|
||||
|
||||
@router.get("/import")
|
||||
async def project_import_form(request: Request):
|
||||
return request.app.templates.TemplateResponse("projects/import_project.html", {
|
||||
"request": request,
|
||||
"error": None,
|
||||
})
|
||||
|
||||
|
||||
@router.post("/import/upload")
|
||||
async def project_import_upload(
|
||||
request: Request,
|
||||
file: UploadFile = File(...),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
from app.exporters.project_import import ProjectImportError, validate_archive
|
||||
|
||||
data = (file.file.read() or b"")
|
||||
try:
|
||||
preview = validate_archive(data)
|
||||
except ProjectImportError as exc:
|
||||
return request.app.templates.TemplateResponse("projects/import_project.html", {
|
||||
"request": request,
|
||||
"error": str(exc),
|
||||
}, status_code=400)
|
||||
|
||||
token = secrets.token_hex(16)
|
||||
IMPORT_UPLOAD_DIR.mkdir(parents=True, exist_ok=True)
|
||||
stored = IMPORT_UPLOAD_DIR / f"{token}.wvt.zip"
|
||||
stored.write_bytes(data)
|
||||
|
||||
counts = preview["counts"]
|
||||
return request.app.templates.TemplateResponse("projects/import_project_preview.html", {
|
||||
"request": request,
|
||||
"token": token,
|
||||
"project_name": preview["project_name"],
|
||||
"project": preview["project"],
|
||||
"manifest": preview["manifest"],
|
||||
"counts": counts,
|
||||
})
|
||||
|
||||
|
||||
@router.post("/import/confirm")
|
||||
async def project_import_confirm(
|
||||
request: Request,
|
||||
token: str = Form(...),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
from app.exporters.project_import import ProjectImportError, import_project
|
||||
|
||||
safe_token = Path(token).name
|
||||
stored = IMPORT_UPLOAD_DIR / f"{safe_token}.wvt.zip"
|
||||
if not stored.exists():
|
||||
raise HTTPException(status_code=400, detail="Файл импорта не найден. Загрузите архив заново.")
|
||||
data = stored.read_bytes()
|
||||
try:
|
||||
imported = import_project(db, data)
|
||||
except ProjectImportError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
finally:
|
||||
try:
|
||||
stored.unlink()
|
||||
except OSError:
|
||||
pass
|
||||
return RedirectResponse(url=f"/projects/{imported.id}?imported=1", status_code=303)
|
||||
|
||||
|
||||
@router.post("/create")
|
||||
async def create_project(
|
||||
request: Request,
|
||||
@@ -350,3 +420,19 @@ async def export_document_package(
|
||||
media_type="application/zip",
|
||||
headers={"Content-Disposition": content_disposition(filename)},
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{project_id}/export/project")
|
||||
async def export_project_archive(project_id: int, db: Session = Depends(get_db)):
|
||||
from app.exporters.project_export import ProjectExportError, export_project
|
||||
from app.exporters import content_disposition
|
||||
|
||||
try:
|
||||
data, filename = export_project(db, project_id)
|
||||
except ProjectExportError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
return Response(
|
||||
content=data,
|
||||
media_type="application/zip",
|
||||
headers={"Content-Disposition": content_disposition(filename)},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user