Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: allow model to be downloaded by an unauthenticated user

Brad Barbin1a5179951858
diff --git a/apps/carbon/app/routes/file+/model+/$bucket.$.tsx b/apps/carbon/app/routes/file+/model+/$bucket.$.tsx
new file mode 100644
index 0000000000..d129f5a07d
--- /dev/null
+++ b/apps/carbon/app/routes/file+/model+/$bucket.$.tsx
@@ -0,0 +1,71 @@
+import { getCarbonServiceRole, notFound } from "@carbon/auth";
+import { supportedModelTypes } from "@carbon/react";
+import { type LoaderFunctionArgs } from "@vercel/remix";
+
+const supportedFileTypes: Record<string, string> = {
+ pdf: "application/pdf",
+ jpg: "image/jpeg",
+ jpeg: "image/jpeg",
+ png: "image/png",
+ gif: "image/gif",
+ svg: "image/svg+xml",
+ avif: "image/avif",
+ webp: "image/webp",
+ mp4: "video/mp4",
+ webm: "video/webm",
+ mov: "video/quicktime",
+ avi: "video/x-msvideo",
+ wmv: "video/x-ms-wmv",
+ mp3: "audio/mpeg",
+ wav: "audio/wav",
+ ogg: "audio/ogg",
+ flac: "audio/flac",
+};
+
+export let loader = async ({ params }: LoaderFunctionArgs) => {
+ const client = getCarbonServiceRole();
+ const { bucket } = params;
+ const path = params["*"];
+
+ if (!bucket) throw new Error("Bucket not found");
+ if (!path) throw new Error("Path not found");
+
+ if (!path.includes("models")) {
+ throw notFound("Invalid path");
+ }
+
+ const fileType = path.split(".").pop()?.toLowerCase();
+
+ if (
+ !fileType ||
+ (!(fileType in supportedFileTypes) &&
+ !supportedModelTypes.includes(fileType))
+ )
+ throw new Error("File type not supported");
+ const contentType = supportedFileTypes[fileType];
+
+ async function downloadFile() {
+ const result = await client.storage.from(bucket!).download(`${path}`);
+ if (result.error) {
+ console.error(result.error);
+ return null;
+ }
+ return result.data;
+ }
+
+ let fileData = await downloadFile();
+ if (!fileData) {
+ // Wait for a second and try again
+ await new Promise((resolve) => setTimeout(resolve, 1000));
+ fileData = await downloadFile();
+ if (!fileData) {
+ throw new Error("Failed to download file after retry");
+ }
+ }
+
+ const headers = new Headers({
+ "Content-Type": contentType,
+ "Cache-Control": "private, max-age=31536000, immutable", // Cache for a year
+ });
+ return new Response(fileData, { status: 200, headers });
+};
diff --git a/apps/carbon/app/routes/file+/model+/$id.tsx b/apps/carbon/app/routes/file+/model+/$id.tsx
index c0486dd1a3..73fdf6f95c 100644
--- a/apps/carbon/app/routes/file+/model+/$id.tsx
+++ b/apps/carbon/app/routes/file+/model+/$id.tsx
@@ -3,7 +3,7 @@ import { json, useLoaderData } from "@remix-run/react";
import { getCarbonServiceRole, notFound } from "@carbon/auth";
import type { LoaderFunctionArgs } from "@vercel/remix";
-import { getPrivateUrl } from "~/utils/path";
+import { getPublicModelUrl } from "~/utils/path";
export async function loader({ request, params }: LoaderFunctionArgs) {
const client = getCarbonServiceRole();
@@ -28,7 +28,7 @@ export default function ModelRoute() {
<ModelViewer
mode="light"
file={null}
- url={getPrivateUrl(model.modelPath)}
+ url={getPublicModelUrl(model.modelPath)}
/>
</div>
);
diff --git a/apps/carbon/app/routes/file+/preview+/$bucket.$.tsx b/apps/carbon/app/routes/file+/preview+/$bucket.$.tsx
index 7b027aefaf..fbc2c8365a 100644
--- a/apps/carbon/app/routes/file+/preview+/$bucket.$.tsx
+++ b/apps/carbon/app/routes/file+/preview+/$bucket.$.tsx
@@ -23,9 +23,7 @@ const supportedFileTypes: Record<string, string> = {
};
export let loader = async ({ request, params }: LoaderFunctionArgs) => {
- const { client } = await requirePermissions(request, {
- view: "documents",
- });
+ const { client } = await requirePermissions(request, {});
const { bucket } = params;
const path = params["*"];
diff --git a/apps/carbon/app/utils/path.ts b/apps/carbon/app/utils/path.ts
index 711227770b..fe62dd77f8 100644
--- a/apps/carbon/app/utils/path.ts
+++ b/apps/carbon/app/utils/path.ts
@@ -840,3 +840,7 @@ export const getParams = (request: Request) => {
export const getPrivateUrl = (path: string) => {
return `/file/preview/private/${path}`;
};
+
+export const getPublicModelUrl = (path: string) => {
+ return `/file/model/private/${path}`;
+};
diff --git a/apps/mes/app/routes/file+/preview+/$bucket.$.tsx b/apps/mes/app/routes/file+/preview+/$bucket.$.tsx
index 7b027aefaf..fbc2c8365a 100644
--- a/apps/mes/app/routes/file+/preview+/$bucket.$.tsx
+++ b/apps/mes/app/routes/file+/preview+/$bucket.$.tsx
@@ -23,9 +23,7 @@ const supportedFileTypes: Record<string, string> = {
};
export let loader = async ({ request, params }: LoaderFunctionArgs) => {
- const { client } = await requirePermissions(request, {
- view: "documents",
- });
+ const { client } = await requirePermissions(request, {});
const { bucket } = params;
const path = params["*"];
diff --git a/packages/database/supabase/functions/thumbnail/index.ts b/packages/database/supabase/functions/thumbnail/index.ts
index 19556ad9a6..2262156b77 100644
--- a/packages/database/supabase/functions/thumbnail/index.ts
+++ b/packages/database/supabase/functions/thumbnail/index.ts
@@ -7,7 +7,9 @@ const payloadSchema = z.object({
url: z.string(),
});
-const browserWSEndpoint = `ws://5.161.255.30?token=59ecf910-aaa8-4c7e-aedb-7c18b34e266e`;
+const browserWSEndpoint = `ws://${Deno.env.get(
+ "BROWSERLESS_IP"
+)}?token=${Deno.env.get("BROWSERLESS_TOKEN")}`;
serve(async (req: Request) => {
if (req.method === "OPTIONS") {
@@ -30,7 +32,7 @@ serve(async (req: Request) => {
await page.setViewport({ width: 400, height: 400 });
await page.goto(url);
- // // Wait for the canvas with id=viewer to be visible, but no longer than 5 seconds
+ // Wait for the canvas with id=viewer to be visible, but no longer than 5 seconds
await page.waitForSelector("#model-viewer-canvas", {
timeout: 10000,
});
diff --git a/packages/react/src/ModelViewer.tsx b/packages/react/src/ModelViewer.tsx
index c0441e1e77..c36294fd11 100644
--- a/packages/react/src/ModelViewer.tsx
+++ b/packages/react/src/ModelViewer.tsx
@@ -116,24 +116,20 @@ export function ModelViewer({
});
function loadFile(file: File) {
- console.log("load file called");
if (!file) return;
if (!viewerRef.current) return;
const viewer = viewerRef.current;
if (!viewer) return;
viewer.LoadModelFromFileList([file]);
- console.log("load file done");
}
function loadUrl(url: string) {
- console.log("load url called");
if (!url) return;
if (!viewerRef.current) return;
const viewer = viewerRef.current;
if (!viewer) return;
viewer.LoadModelFromUrlList([url]);
- console.log("load url done");
}
useEffect(() => {
@@ -166,7 +162,7 @@ export function ModelViewer({
ref={parentDiv}
role={"img"}
aria-label="Canvas showing the model in the 3D Viewer"
- className="h-full w-full items-center justify-center rounded-lg relative border border-border dark:border-0 dark:shadow-[0px_1px_0px_0px_hsla(0,0%,100%,.03)_inset,0px_0px_0px_1px_hsla(0,0%,100%,.03)_inset,0px_0px_0px_1px_rgba(0,0,0,.1),0px_2px_2px_0px_rgba(0,0,0,.1),0px_4px_4px_0px_rgba(0,0,0,.1),0px_8px_8px_0px_rgba(0,0,0,.1)] bg-gradient-to-bl from-card from-50% via-card to-background min-h-[400px] shadow-md"
+ className="h-full w-full items-center justify-center rounded-lg relative border border-border dark:border-0 dark:shadow-[0px_1px_0px_0px_hsla(0,0%,100%,.03)_inset,0px_0px_0px_1px_hsla(0,0%,100%,.03)_inset,0px_0px_0px_1px_rgba(0,0,0,.1),0px_2px_2px_0px_rgba(0,0,0,.1),0px_4px_4px_0px_rgba(0,0,0,.1),0px_8px_8px_0px_rgba(0,0,0,.1)] bg-red-500 min-h-[400px] shadow-md"
>
{isLoading ? (
<div className="absolute inset-0 bg-card h-full w-full flex items-center justify-center">