Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Urdf upload #275

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions frontend/src/components/listing/ListingArtifacts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Spinner from "components/ui/Spinner";

import ListingImages from "./ListingImages";
import ListingSTLs from "./ListingSTLs";
import ListingURDFs from "./ListingURDFs";

interface Props {
listingId: string;
Expand Down Expand Up @@ -57,6 +58,11 @@ const ListingArtifacts = (props: Props) => {
allArtifacts={artifacts}
/>
<ListingSTLs listingId={listingId} edit={edit} allArtifacts={artifacts} />
<ListingURDFs
listingId={listingId}
edit={edit}
allArtifacts={artifacts}
/>
</div>
);
};
Expand Down
123 changes: 123 additions & 0 deletions frontend/src/components/listing/ListingURDFs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { useState } from "react";
import { FaCaretSquareDown, FaCaretSquareUp, FaTimes } from "react-icons/fa";
import { Link } from "react-router-dom";

import { components } from "gen/api";
import { useAlertQueue } from "hooks/useAlertQueue";
import { useAuthentication } from "hooks/useAuth";

import ListingFileUpload from "components/listing/ListingFileUpload";
import { Button } from "components/ui/Button/Button";

interface SingleURDFViewerProps {
url: string;
name: string;
}

const SingleURDFViewer = (props: SingleURDFViewerProps) => {
const { url, name } = props;

return (
<Link to={url} className="link">
{name}
</Link>
);
};

interface Props {
listingId: string;
edit: boolean;
allArtifacts: components["schemas"]["ListArtifactsResponse"]["artifacts"];
}

const ListingURDFs = (props: Props) => {
const { listingId, edit, allArtifacts } = props;

const auth = useAuthentication();
const { addErrorAlert } = useAlertQueue();

const [URDFs, setURDFs] = useState<
components["schemas"]["ListArtifactsResponse"]["artifacts"]
>(allArtifacts.filter((a) => a.artifact_type === "urdf"));
const [deletingIds, setDeletingIds] = useState<string[]>([]);
const [collapsed, setCollapsed] = useState<boolean>(true);

const onDelete = async (urdfId: string) => {
setDeletingIds([...deletingIds, urdfId]);

const { error } = await auth.client.DELETE(
"/artifacts/delete/{artifact_id}",
{
params: {
path: { artifact_id: urdfId },
},
},
);

if (error) {
addErrorAlert(error);
} else {
setURDFs(URDFs.filter((urdf) => urdf.artifact_id !== urdfId));
setDeletingIds(deletingIds.filter((id) => id !== urdfId));
}
};

return URDFs.length > 0 || edit ? (
<div className="flex flex-col items-center justify-center my-4 p-4 relative">
{URDFs.length > 0 ? (
<>
<Button
onClick={() => setCollapsed(!collapsed)}
variant="outline"
className="mt-2 mb-4 text-md p-4"
>
URDFs
{collapsed ? (
<FaCaretSquareUp className="ml-4 text-gray-700" />
) : (
<FaCaretSquareDown className="ml-4 text-gray-700" />
)}
</Button>
{!collapsed && (
<div className="grid sm:grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-2 justify-between w-full">
{URDFs.map((urdf) => (
<div key={urdf.artifact_id} className="bg-background p-2">
<SingleURDFViewer url={urdf.url} name={urdf.name} />
{edit && (
<Button
onClick={() => onDelete(urdf.artifact_id)}
variant="destructive"
className="ml-2"
disabled={deletingIds.includes(urdf.artifact_id)}
>
<FaTimes />
</Button>
)}
</div>
))}
</div>
)}
</>
) : (
<p>
<strong>URDFs</strong>
</p>
)}
{edit && (
<ListingFileUpload
artifactType="urdf"
fileExtensions={[".tar.gz"]}
maxSize={100 * 1024 * 1024}
listingId={listingId}
onUpload={(artifact) => {
setURDFs([...URDFs, artifact.artifact]);
}}
/>
)}
</div>
) : (
<></>
);
};

export default ListingURDFs;
19 changes: 2 additions & 17 deletions store/app/crud/artifacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io
import logging
from typing import Any, BinaryIO, Literal
from xml.etree import ElementTree as ET

from PIL import Image
from stl import Mode as STLMode, mesh as stlmesh
Expand All @@ -21,7 +20,6 @@
get_content_type,
)
from store.settings import settings
from store.utils import save_xml

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -134,7 +132,7 @@ async def _upload_stl(
)
return artifact

async def _upload_xml(
async def _upload_tar_gz(
self,
name: str,
file: io.BytesIO | BinaryIO,
Expand All @@ -146,19 +144,6 @@ async def _upload_xml(
if listing.user_id != user_id:
raise NotAuthorizedError("User does not have permission to upload artifacts to this listing")

# Standardizes the XML file.
try:
tree = ET.parse(file)
except Exception:
raise BadArtifactError("Invalid XML file")

# TODO: Remap the STL or OBJ file paths.

# Converts to byte stream.
out_file = io.BytesIO()
save_xml(out_file, tree)
out_file.seek(0)

# Saves the artifact to S3.
content_type = get_content_type(artifact_type)
artifact = Artifact.create(
Expand Down Expand Up @@ -189,7 +174,7 @@ async def upload_artifact(
case "stl":
return await self._upload_stl(name, file, listing, user_id, description)
case "urdf" | "mjcf":
return await self._upload_xml(name, file, listing, user_id, artifact_type, description)
return await self._upload_tar_gz(name, file, listing, user_id, artifact_type, description)
case _:
raise BadArtifactError(f"Invalid artifact type: {artifact_type}")

Expand Down
12 changes: 6 additions & 6 deletions store/app/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def create(cls, user_id: str, source: APIKeySource, permissions: APIKeyPermissio
# Image
"image": {"image/png", "image/jpeg", "image/jpg", "image/gif", "image/webp"},
# XML
"urdf": {"application/xml"},
"mjcf": {"application/xml"},
"urdf": {"application/gzip"},
"mjcf": {"application/gzip"},
# Binary or text
"stl": {"application/octet-stream", "text/xml"},
}
Expand All @@ -129,8 +129,8 @@ def create(cls, user_id: str, source: APIKeySource, permissions: APIKeyPermissio
# Image
"image": "image/png",
# XML
"urdf": "application/xml",
"mjcf": "application/xml",
"urdf": "application/gzip",
"mjcf": "application/gzip",
# Binary
"stl": "application/octet-stream",
}
Expand All @@ -149,9 +149,9 @@ def get_artifact_name(id: str, artifact_type: ArtifactType, size: ArtifactSize =
height, width = SizeMapping[size]
return f"{id}_{size}_{height}x{width}.png"
case "urdf":
return f"{id}.urdf"
return f"{id}.tar.gz"
case "mjcf":
return f"{id}.xml"
return f"{id}.tar.gz"
case "stl":
return f"{id}.stl"
case _:
Expand Down
Loading
Loading