✨ Update backup: create archive using async job
This commit is contained in:
parent
9155219946
commit
9d0666cb76
@ -1,19 +1,24 @@
|
|||||||
import json
|
import json
|
||||||
from datetime import datetime
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Annotated
|
from typing import Annotated
|
||||||
|
from zipfile import ZIP_DEFLATED, ZipFile
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, File, HTTPException, UploadFile
|
from fastapi import (APIRouter, BackgroundTasks, Depends, File, HTTPException,
|
||||||
|
UploadFile)
|
||||||
|
from fastapi.responses import FileResponse
|
||||||
from sqlalchemy.orm import selectinload
|
from sqlalchemy.orm import selectinload
|
||||||
from sqlmodel import select
|
from sqlmodel import select
|
||||||
|
|
||||||
|
from .. import __version__ as trip_version
|
||||||
from ..config import settings
|
from ..config import settings
|
||||||
from ..deps import SessionDep, get_current_username
|
from ..deps import SessionDep, get_current_username
|
||||||
from ..models.models import (Category, CategoryRead, Image, Place, PlaceRead,
|
from ..models.models import (Backup, BackupRead, BackupStatus, Category,
|
||||||
Trip, TripDay, TripItem, TripRead, User, UserRead,
|
CategoryRead, Image, Place, PlaceRead, Trip,
|
||||||
|
TripDay, TripItem, TripRead, User, UserRead,
|
||||||
UserUpdate)
|
UserUpdate)
|
||||||
from ..utils.utils import (b64e, b64img_decode, check_update, remove_image,
|
from ..utils.utils import (assets_folder_path, attachments_trip_folder_path,
|
||||||
save_image_to_file)
|
b64img_decode, check_update, save_image_to_file,
|
||||||
|
utc_now)
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/settings", tags=["settings"])
|
router = APIRouter(prefix="/api/settings", tags=["settings"])
|
||||||
|
|
||||||
@ -52,52 +57,156 @@ async def check_version(session: SessionDep, current_user: Annotated[str, Depend
|
|||||||
return await check_update()
|
return await check_update()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/export")
|
@router.post("/backups", response_model=BackupRead)
|
||||||
def export_data(session: SessionDep, current_user: Annotated[str, Depends(get_current_username)]):
|
def create_backup_export(
|
||||||
trips_query = (
|
background_tasks: BackgroundTasks,
|
||||||
select(Trip)
|
session: SessionDep,
|
||||||
.where(Trip.user == current_user)
|
current_user: Annotated[str, Depends(get_current_username)],
|
||||||
.options(
|
) -> BackupRead:
|
||||||
selectinload(Trip.days)
|
db_backup = Backup(user=current_user)
|
||||||
.selectinload(TripDay.items)
|
session.add(db_backup)
|
||||||
.options(
|
session.commit()
|
||||||
selectinload(TripItem.place).selectinload(Place.category).selectinload(Category.image),
|
session.refresh(db_backup)
|
||||||
selectinload(TripItem.place).selectinload(Place.image),
|
background_tasks.add_task(_process_backup_task, session, db_backup.id)
|
||||||
selectinload(TripItem.image),
|
return BackupRead.serialize(db_backup)
|
||||||
),
|
|
||||||
selectinload(Trip.places).options(
|
|
||||||
selectinload(Place.category).selectinload(Category.image),
|
@router.get("/backups", response_model=list[BackupRead])
|
||||||
selectinload(Place.image),
|
def read_backups(
|
||||||
),
|
session: SessionDep, current_user: Annotated[str, Depends(get_current_username)]
|
||||||
selectinload(Trip.image),
|
) -> list[BackupRead]:
|
||||||
selectinload(Trip.memberships),
|
db_backups = session.exec(select(Backup).where(Backup.user == current_user)).all()
|
||||||
selectinload(Trip.shares),
|
return [BackupRead.serialize(backup) for backup in db_backups]
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/backups/{backup_id}/download")
|
||||||
|
def download_backup(
|
||||||
|
backup_id: int, session: SessionDep, current_user: Annotated[str, Depends(get_current_username)]
|
||||||
|
):
|
||||||
|
db_backup = session.exec(
|
||||||
|
select(Backup).where(
|
||||||
|
Backup.id == backup_id, Backup.user == current_user, Backup.status == BackupStatus.COMPLETED
|
||||||
)
|
)
|
||||||
)
|
).first()
|
||||||
|
if not db_backup or not db_backup.filename:
|
||||||
|
raise HTTPException(status_code=404, detail="Not found")
|
||||||
|
|
||||||
user_settings = UserRead.serialize(session.get(User, current_user))
|
file_path = Path(settings.BACKUPS_FOLDER) / db_backup.filename
|
||||||
categories = session.exec(select(Category).where(Category.user == current_user)).all()
|
if not file_path.exists():
|
||||||
places = session.exec(select(Place).where(Place.user == current_user)).all()
|
raise HTTPException(status_code=404, detail="Not found")
|
||||||
trips = session.exec(trips_query).all()
|
|
||||||
images = session.exec(select(Image).where(Image.user == current_user)).all()
|
|
||||||
|
|
||||||
data = {
|
iso_date = db_backup.created_at.strftime("%Y-%m-%d")
|
||||||
"_": {"at": datetime.timestamp(datetime.now())},
|
filename = f"TRIP_{iso_date}_{current_user}_backup.zip"
|
||||||
"settings": user_settings,
|
return FileResponse(path=file_path, filename=filename, media_type="application/zip")
|
||||||
"categories": [CategoryRead.serialize(c) for c in categories],
|
|
||||||
"places": [PlaceRead.serialize(place, exclude_gpx=False) for place in places],
|
|
||||||
"trips": [TripRead.serialize(t) for t in trips],
|
def _process_backup_task(session: SessionDep, backup_id: int):
|
||||||
"images": {},
|
db_backup = session.get(Backup, backup_id)
|
||||||
}
|
if not db_backup:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
db_backup.status = BackupStatus.PROCESSING
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
trips_query = (
|
||||||
|
select(Trip)
|
||||||
|
.where(Trip.user == db_backup.user)
|
||||||
|
.options(
|
||||||
|
selectinload(Trip.days)
|
||||||
|
.selectinload(TripDay.items)
|
||||||
|
.options(
|
||||||
|
selectinload(TripItem.place).selectinload(Place.category).selectinload(Category.image),
|
||||||
|
selectinload(TripItem.place).selectinload(Place.image),
|
||||||
|
selectinload(TripItem.image),
|
||||||
|
),
|
||||||
|
selectinload(Trip.places).options(
|
||||||
|
selectinload(Place.category).selectinload(Category.image),
|
||||||
|
selectinload(Place.image),
|
||||||
|
),
|
||||||
|
selectinload(Trip.image),
|
||||||
|
selectinload(Trip.memberships),
|
||||||
|
selectinload(Trip.shares),
|
||||||
|
selectinload(Trip.packing_items),
|
||||||
|
selectinload(Trip.checklist_items),
|
||||||
|
selectinload(Trip.attachments),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
user_settings = UserRead.serialize(session.get(User, db_backup.user))
|
||||||
|
categories = session.exec(select(Category).where(Category.user == db_backup.user)).all()
|
||||||
|
places = session.exec(select(Place).where(Place.user == db_backup.user)).all()
|
||||||
|
trips = session.exec(trips_query).all()
|
||||||
|
images = session.exec(select(Image).where(Image.user == db_backup.user)).all()
|
||||||
|
|
||||||
|
backup_datetime = utc_now()
|
||||||
|
iso_date = backup_datetime.strftime("%Y-%m-%d")
|
||||||
|
filename = f"TRIP_{iso_date}_{db_backup.user}_backup.zip"
|
||||||
|
zip_fp = Path(settings.BACKUPS_FOLDER) / filename
|
||||||
|
Path(settings.BACKUPS_FOLDER).mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
with ZipFile(zip_fp, "w", ZIP_DEFLATED) as zipf:
|
||||||
|
data = {
|
||||||
|
"_": {
|
||||||
|
"version": trip_version,
|
||||||
|
"at": backup_datetime.isoformat(),
|
||||||
|
"user": db_backup.user,
|
||||||
|
},
|
||||||
|
"settings": user_settings,
|
||||||
|
"categories": [CategoryRead.serialize(c) for c in categories],
|
||||||
|
"places": [PlaceRead.serialize(place, exclude_gpx=False) for place in places],
|
||||||
|
"trips": [TripRead.serialize(t) for t in trips],
|
||||||
|
}
|
||||||
|
zipf.writestr("data.json", json.dumps(data, ensure_ascii=False, indent=2, default=str))
|
||||||
|
|
||||||
|
for db_image in images:
|
||||||
|
try:
|
||||||
|
filepath = assets_folder_path() / db_image.filename
|
||||||
|
if filepath.exists() and filepath.is_file():
|
||||||
|
zipf.write(filepath, f"images/{db_image.filename}")
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for trip in trips:
|
||||||
|
if not trip.attachments:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for attachment in trip.attachments:
|
||||||
|
try:
|
||||||
|
filepath = attachments_trip_folder_path(trip.id) / attachment.stored_filename
|
||||||
|
if filepath.exists() and filepath.is_file():
|
||||||
|
zipf.write(filepath, f"attachments/{trip.id}/{attachment.stored_filename}")
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
|
db_backup.file_size = zip_fp.stat().st_size
|
||||||
|
db_backup.status = BackupStatus.COMPLETED
|
||||||
|
db_backup.completed_at = utc_now()
|
||||||
|
db_backup.filename = filename
|
||||||
|
session.commit()
|
||||||
|
except Exception as exc:
|
||||||
|
db_backup.status = BackupStatus.FAILED
|
||||||
|
db_backup.error_message = str(exc)[:200]
|
||||||
|
session.commit()
|
||||||
|
|
||||||
for im in images:
|
|
||||||
try:
|
try:
|
||||||
with open(Path(settings.ASSETS_FOLDER) / im.filename, "rb") as f:
|
if filepath.exists():
|
||||||
data["images"][im.id] = b64e(f.read())
|
filepath.unlink()
|
||||||
except FileNotFoundError:
|
except Exception:
|
||||||
continue
|
pass
|
||||||
|
|
||||||
return data
|
|
||||||
|
@router.delete("/backups/{backup_id}")
|
||||||
|
async def delete_backup(
|
||||||
|
backup_id: int, session: SessionDep, current_user: Annotated[str, Depends(get_current_username)]
|
||||||
|
):
|
||||||
|
db_backup = session.get(Backup, backup_id)
|
||||||
|
if not db_backup.user == current_user:
|
||||||
|
raise HTTPException(status_code=403, detail="Forbidden")
|
||||||
|
|
||||||
|
session.delete(db_backup)
|
||||||
|
session.commit()
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
@router.post("/import")
|
@router.post("/import")
|
||||||
@ -147,7 +256,6 @@ async def import_data(
|
|||||||
if category_exists.image_id:
|
if category_exists.image_id:
|
||||||
old_image = session.get(Image, category_exists.image_id)
|
old_image = session.get(Image, category_exists.image_id)
|
||||||
try:
|
try:
|
||||||
remove_image(old_image.filename)
|
|
||||||
session.delete(old_image)
|
session.delete(old_image)
|
||||||
category_exists.image_id = None
|
category_exists.image_id = None
|
||||||
session.flush()
|
session.flush()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user