Skip to content

Commit

Permalink
upload dem into s3
Browse files Browse the repository at this point in the history
  • Loading branch information
nrjadkry committed Aug 6, 2024
1 parent 102554e commit a4cca7c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,43 @@
from loguru import logger as log
import shapely.wkb as wkblib
from shapely.geometry import shape
from fastapi import HTTPException
from fastapi import HTTPException, UploadFile
from app.utils import merge_multipolygon
from fmtm_splitter.splitter import split_by_square
from fastapi.concurrency import run_in_threadpool
from databases import Database
from app.models.enums import ProjectStatus
from app.utils import generate_slug
from io import BytesIO
from app.s3 import add_obj_to_bucket
from app.config import settings


async def upload_dem_to_s3(project_id: uuid.UUID, dem_file: UploadFile) -> str:
"""Upload dem into S3.
Args:
project_id (int): The organisation id in the database.
dem_file (UploadFile): The logo image uploaded to FastAPI.
Returns:
dem_url(str): The S3 URL for the dem file.
"""
dem_path = f"/{project_id}/dem/dem.tif"

file_bytes = await dem_file.read()
file_obj = BytesIO(file_bytes)

add_obj_to_bucket(
settings.S3_BUCKET_NAME,
file_obj,
dem_path,
content_type=dem_file.content_type,
)

dem_url = f"{settings.S3_DOWNLOAD_ROOT}/{settings.S3_BUCKET_NAME}{dem_path}"

return dem_url


async def create_project_with_project_info(
Expand Down
9 changes: 9 additions & 0 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from shapely.geometry import shape, mapping
from shapely.ops import unary_union


router = APIRouter(
prefix=f"{settings.API_PREFIX}/projects",
responses={404: {"description": "Not found"}},
Expand Down Expand Up @@ -70,6 +71,7 @@ async def delete_project_by_id(
@router.post("/create_project", tags=["Projects"])
async def create_project(
project_info: project_schemas.ProjectIn,
dem: UploadFile = File(None),
db: Database = Depends(database.get_db),
user_data: AuthUser = Depends(login_required),
):
Expand All @@ -78,6 +80,13 @@ async def create_project(
project_id = await project_crud.create_project_with_project_info(
db, author_id, project_info
)

# Upload DEM to S3
dem_url = await project_crud.upload_dem_to_s3(project_id, dem) if dem else None

# Update dem url to database
await project_crud.update_project_dem_url(db, project_id, dem_url)

if not project_id:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Project creation failed"
Expand Down

0 comments on commit a4cca7c

Please sign in to comment.