diff --git a/config/nginx.dev.yml b/config/nginx.dev.yml new file mode 100644 index 0000000..88d83e0 --- /dev/null +++ b/config/nginx.dev.yml @@ -0,0 +1,7 @@ +server { + listen 8999; + listen [::]:8999; + + root /app/uploads; + gzip_static on; +} diff --git a/docker-compose.yml b/docker-compose.yml index fe49c31..a188568 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,5 +17,14 @@ services: - 8000:8000 volumes: - .:/app + + nginx: + image: nginx:alpine + restart: always + volumes: + - ./config/nginx.dev.yml:/etc/nginx/conf.d/app-dev.conf + - ./uploads:/app/uploads + ports: + - "8999:8999" volumes: db-data: { } \ No newline at end of file diff --git a/src/graphql_schema/entities/flight.py b/src/graphql_schema/entities/flight.py index 7553b2a..ea07afc 100644 --- a/src/graphql_schema/entities/flight.py +++ b/src/graphql_schema/entities/flight.py @@ -128,6 +128,4 @@ class EditFlightMutation: @strawberry.mutation async def edit_flight(self, info, id: int, input: EditFlightInput) -> Flight: - input_data = input.to_dict() - - return await models.Flight.update(info.context.db, id=id, data=input_data) + return await models.Flight.update(info.context.db, id=id, data=input.to_dict()) diff --git a/src/graphql_schema/entities/photo.py b/src/graphql_schema/entities/photo.py index 6ddeae2..9cedb16 100644 --- a/src/graphql_schema/entities/photo.py +++ b/src/graphql_schema/entities/photo.py @@ -4,13 +4,17 @@ from sqlalchemy import select from strawberry.file_uploads import Upload from database import models from graphql_schema.sqlalchemy_to_strawberry_type import strawberry_sqlalchemy_type, strawberry_sqlalchemy_input -from upload_utils import get_public_url, handle_file_upload +from upload_utils import get_public_url, handle_file_upload, delete_file @strawberry_sqlalchemy_type(models.Photo) class Photo: - url: str = strawberry.field(resolver=lambda root: get_public_url(root.filename)) - thumbnail_url: str = strawberry.field(resolver=lambda root: get_public_url(f"thumbs/{root.filename}")) + url: str = strawberry.field( + resolver=lambda root: get_public_url(f"photos/{root.flight_id}/{root.filename}") + ) + thumbnail_url: str = strawberry.field( + resolver=lambda root: get_public_url(f"photos/{root.flight_id}/{root.filename}") # TODO: doplnit thumb! + ) def get_base_query(user_id: int): @@ -21,6 +25,10 @@ def get_base_query(user_id: int): ) +def get_photo_basepath(flight_id: int) -> str: + return f"/app/uploads/photos/{flight_id}" + + @strawberry.type class PhotoQueries: @strawberry.field @@ -37,8 +45,7 @@ class UploadPhotoMutation: @strawberry.mutation async def upload_photo(self, info, input: UploadPhotoInput) -> Photo: - photos_dest = f"/app/uploads/photos/{input.flight_id}/" - filename = await handle_file_upload(input.photo, photos_dest) + filename = await handle_file_upload(input.photo, get_photo_basepath(input.flight_id)) # todo: udelat nahled do thumbs slozky @@ -55,27 +62,32 @@ class UploadPhotoMutation: @strawberry.type class EditPhotoMutation: - @strawberry_sqlalchemy_input(models.Photo, exclude_fields=[], all_optional=True) + @strawberry_sqlalchemy_input(models.Photo, exclude_fields=["id"], all_optional=True) class EditPhotoInput: pass @strawberry.mutation - async def update_photo(self, info, input: EditPhotoInput) -> Photo: + async def edit_photo(self, info, id: int, input: EditPhotoInput) -> Photo: query = get_base_query(info.context.user_id) - photo = info.context.db.scalars(query.filter(models.Photo.id == input.id)) + photo = (await info.context.db.scalars(query.filter(models.Photo.id == id))).one() - update_data = input.to_dict() - photo.update(**update_data) - - return photo + return await models.Photo.update(info.context.db, obj=photo, data=input.to_dict()) @strawberry.type class DeletePhotoMutation: - @strawberry.input - class DeletePhotoInput: - id: int - @strawberry.mutation - async def delete_photo(self, info, input: DeletePhotoInput) -> Photo: - pass + async def delete_photo(self, info, id: int) -> Photo: + query = get_base_query(info.context.user_id) + photo = (await info.context.db.scalars(query.filter(models.Photo.id == id))).one() + + base_path = get_photo_basepath(photo.flight_id) + try: + delete_file(f"{base_path}/{photo.filename}") + # TODO: odstranit nahledy + except Exception as e: + print("ERROR", e) + + await info.context.db.delete(photo) + + return photo diff --git a/src/upload_utils.py b/src/upload_utils.py index 3b1ede9..795658c 100644 --- a/src/upload_utils.py +++ b/src/upload_utils.py @@ -1,12 +1,11 @@ import os import uuid from typing import Optional - from strawberry.file_uploads import Upload def get_public_url(filename: Optional[str]): - return f"http://localhost:8000/uploads/{filename}" if filename else None + return f"http://localhost:8999/{filename}" if filename else None def check_directories(path: str):