Skip to content

Commit

Permalink
fuck go back
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Aug 20, 2024
1 parent 593c066 commit a2816a7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 119 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/listing/ListingUrdf.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const ListingUrdf = (props: Props) => {
<Spinner />
</div>
) : (
urdf.urdf_id !== null && (
urdf.urdf !== null && (
<>
{showUrdf ? (
<>
Expand Down
64 changes: 11 additions & 53 deletions frontend/src/gen/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,25 +354,8 @@ export interface paths {
path?: never;
cookie?: never;
};
/** Get Urdf Info */
get: operations["get_urdf_info_urdf_info__listing_id__get"];
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/urdf/download/{listing_id}": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
/** Download Urdf */
get: operations["download_urdf_urdf_download__listing_id__get"];
/** Get Urdf */
get: operations["get_urdf_urdf_info__listing_id__get"];
put?: never;
post?: never;
delete?: never;
Expand Down Expand Up @@ -936,10 +919,16 @@ export interface components {
/** Artifacts */
artifacts: components["schemas"]["ListArtifactsItem"][];
};
/** UrdfInfo */
UrdfInfo: {
/** Artifact Id */
artifact_id: string;
/** Url */
url: string;
};
/** UrdfResponse */
UrdfResponse: {
/** Urdf Id */
urdf_id: string | null;
urdf: components["schemas"]["UrdfInfo"] | null;
/** Listing Id */
listing_id: string;
};
Expand Down Expand Up @@ -1620,7 +1609,7 @@ export interface operations {
};
};
};
get_urdf_info_urdf_info__listing_id__get: {
get_urdf_urdf_info__listing_id__get: {
parameters: {
query?: never;
header?: never;
Expand Down Expand Up @@ -1651,37 +1640,6 @@ export interface operations {
};
};
};
download_urdf_urdf_download__listing_id__get: {
parameters: {
query?: never;
header?: never;
path: {
listing_id: string;
};
cookie?: never;
};
requestBody?: never;
responses: {
/** @description Successful Response */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": unknown;
};
};
/** @description Validation Error */
422: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["HTTPValidationError"];
};
};
};
};
set_urdf_urdf_upload__listing_id__post: {
parameters: {
query?: never;
Expand Down
93 changes: 28 additions & 65 deletions store/app/routers/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,58 @@
from typing import Annotated

from fastapi import APIRouter, Depends, HTTPException, UploadFile, status
from fastapi.responses import StreamingResponse
from pydantic.main import BaseModel

from store.app.crud.urdf import URDF_PACKAGE_NAME
from store.app.db import Crud
from store.app.model import (
DOWNLOAD_CONTENT_TYPE,
User,
can_read_artifact,
can_read_listing,
can_write_listing,
get_artifact_url,
get_compression_type,
)
from store.app.routers.users import get_session_user_with_read_permission, get_session_user_with_write_permission
from store.app.routers.users import get_session_user_with_write_permission
from store.settings import settings

urdf_router = APIRouter()

logger = logging.getLogger(__name__)


def get_urdf_url(listing_id: str) -> str:
return get_artifact_url(
artifact_type="urdf",
listing_id=listing_id,
name=URDF_PACKAGE_NAME,
)


class UrdfInfo(BaseModel):
artifact_id: str
url: str


class UrdfResponse(BaseModel):
urdf_id: str | None
urdf: UrdfInfo | None
listing_id: str


@urdf_router.get("/info/{listing_id}")
async def get_urdf_info(
@urdf_router.get("/info/{listing_id}", response_model=UrdfResponse)
async def get_urdf(
listing_id: str,
crud: Annotated[Crud, Depends(Crud.get)],
user: Annotated[User, Depends(get_session_user_with_read_permission)],
) -> UrdfResponse:
listing = await crud.get_listing(listing_id)
if listing is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Could not find listing associated with the given id",
)
if not await can_read_listing(user, listing):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="User does not have permission to view the URDF for this listing",
)
urdf = await crud.get_urdf(listing_id)
if urdf is None:
return UrdfResponse(urdf_id=None, listing_id=listing_id)
if not await can_read_artifact(user, urdf):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="User does not have permission to view the URDF for this listing",
)
return UrdfResponse(urdf_id=urdf.id, listing_id=listing_id)
return UrdfResponse(urdf=None, listing_id=listing_id)
return UrdfResponse(
urdf=UrdfInfo(artifact_id=urdf.id, url=get_urdf_url(listing_id)),
listing_id=listing_id,
)


@urdf_router.get("/download/{listing_id}")
async def download_urdf(
listing_id: str,
crud: Annotated[Crud, Depends(Crud.get)],
user: Annotated[User, Depends(get_session_user_with_read_permission)],
) -> StreamingResponse:
listing = await crud.get_listing(listing_id)
if listing is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Could not find listing associated with the given id",
)
if not await can_read_listing(user, listing):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="User does not have permission to download the URDF for this listing",
)
urdf = await crud.get_urdf(listing_id)
if urdf is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Could not find URDF associated with the given listing id",
)
if not await can_read_artifact(user, urdf):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="User does not have permission to download the URDF for this listing",
)
urdf_stream = await crud.stream_artifact(urdf)
return StreamingResponse(
urdf_stream,
media_type=DOWNLOAD_CONTENT_TYPE["urdf"],
headers={
"Content-Disposition": f"attachment; filename={urdf.name}",
},
)
class SetUrdfResponse(BaseModel):
urdf: UrdfInfo


@urdf_router.post("/upload/{listing_id}")
Expand Down Expand Up @@ -153,7 +116,7 @@ async def set_urdf(
)

return UrdfResponse(
urdf_id=urdf.id,
urdf=UrdfInfo(artifact_id=urdf.id, url=get_urdf_url(listing.id)),
listing_id=listing.id,
)

Expand Down Expand Up @@ -184,4 +147,4 @@ async def delete_urdf(
detail="User does not have permission to delete the URDF for this listing",
)
await crud.remove_artifact(urdf)
return UrdfResponse(urdf_id=None, listing_id=listing_id)
return UrdfResponse(urdf=None, listing_id=listing_id)
1 change: 1 addition & 0 deletions tests/test_listings.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ async def test_listings(app_client: AsyncClient, tmpdir: Path) -> None:
data = response.json()
assert data["urdf_id"] is not None

# Downloads and checks that the files were converted to OBJ files.
response = await app_client.get(f"/urdf/download/{listing_id}", headers=auth_headers)
assert response.status_code == status.HTTP_200_OK, response.content
with tempfile.NamedTemporaryFile(suffix=".tgz") as f:
Expand Down

0 comments on commit a2816a7

Please sign in to comment.