From fe059384d39738e80e43f60bc349c8d1efa6be7a Mon Sep 17 00:00:00 2001 From: Kurt Hutten Date: Mon, 22 Nov 2021 20:05:10 +1100 Subject: [PATCH] Run curv inside of /tmp When exporting an stl it writes temporary files which is not allowed when deployed to aws unless it's in temp. --- app/api/src/docker/cadquery/cadquery.ts | 3 ++- app/api/src/docker/cadquery/runCQ.ts | 2 +- app/api/src/docker/common/utils.ts | 2 ++ app/api/src/docker/curv/curv.ts | 6 ++++-- app/api/src/docker/curv/runCurv.ts | 12 ++++++----- app/api/src/docker/openscad/openscad.ts | 6 ++++-- app/api/src/docker/openscad/runScad.ts | 5 +++-- app/api/src/docker/serverless.yml | 17 +++++++++++++-- .../src/components/IdeViewer/IdeViewer.tsx | 2 +- .../components/IdeViewer/PureIdeViewer.tsx | 4 ++-- .../cadPackages/curv/curvController.ts | 21 ------------------- 11 files changed, 41 insertions(+), 39 deletions(-) diff --git a/app/api/src/docker/cadquery/cadquery.ts b/app/api/src/docker/cadquery/cadquery.ts index 9c41ba5..6712876 100644 --- a/app/api/src/docker/cadquery/cadquery.ts +++ b/app/api/src/docker/cadquery/cadquery.ts @@ -9,12 +9,13 @@ const stl = async (req, _context, callback) => { console.log('eventBody', eventBody) const { file, settings } = JSON.parse(eventBody) - const { error, consoleMessage, fullPath } = await runCQ({ file, settings }) + const { error, consoleMessage, fullPath, tempFile } = await runCQ({ file, settings }) await storeAssetAndReturnUrl({ error, callback, fullPath, consoleMessage, + tempFile }) } diff --git a/app/api/src/docker/cadquery/runCQ.ts b/app/api/src/docker/cadquery/runCQ.ts index 2f73e68..6a1db1d 100644 --- a/app/api/src/docker/cadquery/runCQ.ts +++ b/app/api/src/docker/cadquery/runCQ.ts @@ -53,7 +53,7 @@ export const runCQ = async ({ 15000, true ) - return { consoleMessage, fullPath } + return { consoleMessage, fullPath, tempFile } } catch (error) { return { error: consoleMessage || error, fullPath } } diff --git a/app/api/src/docker/common/utils.ts b/app/api/src/docker/common/utils.ts index 16cf418..33bfda6 100644 --- a/app/api/src/docker/common/utils.ts +++ b/app/api/src/docker/common/utils.ts @@ -104,11 +104,13 @@ export async function storeAssetAndReturnUrl({ callback, fullPath, consoleMessage, + tempFile }: { error: string callback: Function fullPath: string consoleMessage: string + tempFile: string }) { if (error) { const response = { diff --git a/app/api/src/docker/curv/curv.ts b/app/api/src/docker/curv/curv.ts index a81eb0b..cdcf3da 100644 --- a/app/api/src/docker/curv/curv.ts +++ b/app/api/src/docker/curv/curv.ts @@ -9,7 +9,7 @@ const preview = async (req, _context, callback) => { console.log('eventBody', eventBody) const { file, settings } = JSON.parse(eventBody) - const { error, consoleMessage, fullPath } = await runCurv({ + const { error, consoleMessage, fullPath, tempFile } = await runCurv({ file, settings, }) @@ -18,6 +18,7 @@ const preview = async (req, _context, callback) => { callback, fullPath, consoleMessage, + tempFile, }) } @@ -28,7 +29,7 @@ const stl = async (req, _context, callback) => { console.log(eventBody, 'eventBody') const { file, settings } = JSON.parse(eventBody) - const { error, consoleMessage, fullPath } = await stlExport({ + const { error, consoleMessage, fullPath, tempFile } = await stlExport({ file, settings, }) @@ -37,6 +38,7 @@ const stl = async (req, _context, callback) => { callback, fullPath, consoleMessage, + tempFile, }) } diff --git a/app/api/src/docker/curv/runCurv.ts b/app/api/src/docker/curv/runCurv.ts index 767d529..9d349a5 100644 --- a/app/api/src/docker/curv/runCurv.ts +++ b/app/api/src/docker/curv/runCurv.ts @@ -18,6 +18,7 @@ export const runCurv = async ({ consoleMessage?: string fullPath?: string customizerPath?: string + tempFile?: string }> => { const tempFile = await writeFiles( [ @@ -67,7 +68,7 @@ export const runCurv = async ({ `cat ${imPath} /var/task/cadhub-concat-split /tmp/${tempFile}/metadata.json | gzip > ${fullPath}`, 15000 ) - return { consoleMessage, fullPath, customizerPath } + return { consoleMessage, fullPath, customizerPath, tempFile } } catch (dirtyError) { return { error: dirtyError } } @@ -90,11 +91,12 @@ export const stlExport = async ({ file, settings: { parameters } } = {}) => { const fullPath = `/tmp/${tempFile}/output.gz` const stlPath = `/tmp/${tempFile}/output.stl` const command = [ - 'curv', + '(cd /tmp && curv', '-o', stlPath, '-O jit', - '-O vsize=0.4', + '-O vsize=0.6', `/tmp/${tempFile}/main.curv`, + ')', ].join(' ') try { @@ -113,10 +115,10 @@ export const stlExport = async ({ file, settings: { parameters } } = {}) => { tempFile ) await runCommand( - `cat ${stlPath} /var/task/cadhub-concat-split /tmp/${tempFile}/metadata.json | gzip > ${fullPath}`, + `cat ${stlPath} /var/task/cadhub-concat-split /tmp/${tempFile}/metadata.json | gzip > ${fullPath} && rm ${stlPath}`, 15000 ) - return { consoleMessage, fullPath } + return { consoleMessage, fullPath, tempFile } } catch (error) { return { error, fullPath } } diff --git a/app/api/src/docker/openscad/openscad.ts b/app/api/src/docker/openscad/openscad.ts index 752c8c5..823f1a5 100644 --- a/app/api/src/docker/openscad/openscad.ts +++ b/app/api/src/docker/openscad/openscad.ts @@ -9,7 +9,7 @@ const preview = async (req, _context, callback) => { console.log('eventBody', eventBody) const { file, settings } = JSON.parse(eventBody) - const { error, consoleMessage, fullPath } = await runScad({ + const { error, consoleMessage, fullPath, tempFile } = await runScad({ file, settings, }) @@ -18,6 +18,7 @@ const preview = async (req, _context, callback) => { callback, fullPath, consoleMessage, + tempFile, }) } @@ -28,7 +29,7 @@ const stl = async (req, _context, callback) => { console.log(eventBody, 'eventBody') const { file, settings } = JSON.parse(eventBody) - const { error, consoleMessage, fullPath } = await stlExport({ + const { error, consoleMessage, fullPath, tempFile } = await stlExport({ file, settings, }) @@ -37,6 +38,7 @@ const stl = async (req, _context, callback) => { callback, fullPath, consoleMessage, + tempFile, }) } diff --git a/app/api/src/docker/openscad/runScad.ts b/app/api/src/docker/openscad/runScad.ts index c176931..de34352 100644 --- a/app/api/src/docker/openscad/runScad.ts +++ b/app/api/src/docker/openscad/runScad.ts @@ -25,6 +25,7 @@ export const runScad = async ({ consoleMessage?: string fullPath?: string customizerPath?: string + tempFile?: string }> => { const tempFile = await writeFiles( [ @@ -88,7 +89,7 @@ export const runScad = async ({ `cat ${imPath} /var/task/cadhub-concat-split /tmp/${tempFile}/metadata.json | gzip > ${fullPath}`, 15000 ) - return { consoleMessage, fullPath, customizerPath } + return { consoleMessage, fullPath, customizerPath, tempFile } } catch (dirtyError) { return { error: cleanOpenScadError(dirtyError) } } @@ -143,7 +144,7 @@ export const stlExport = async ({ file, settings: { parameters } } = {}) => { `cat ${stlPath} /var/task/cadhub-concat-split /tmp/${tempFile}/metadata.json | gzip > ${fullPath}`, 15000 ) - return { consoleMessage, fullPath, customizerPath } + return { consoleMessage, fullPath, customizerPath, tempFile } } catch (error) { return { error, fullPath } } diff --git a/app/api/src/docker/serverless.yml b/app/api/src/docker/serverless.yml index 78e22f3..af39194 100644 --- a/app/api/src/docker/serverless.yml +++ b/app/api/src/docker/serverless.yml @@ -87,6 +87,7 @@ functions: timeout: 30 environment: BUCKET: cad-preview-bucket-prod-001 + cadquerystl: image: name: cadqueryimage @@ -102,6 +103,7 @@ functions: timeout: 30 environment: BUCKET: cad-preview-bucket-prod-001 + curvpreview: image: name: curvimage @@ -115,8 +117,19 @@ functions: method: post cors: true timeout: 25 - environment: - BUCKET: cad-preview-bucket-prod-001 + curvstl: + image: + name: curvimage + command: + - js/curv.stl + entryPoint: + - '/entrypoint.sh' + events: + - http: + path: curv/stl + method: post + cors: true + timeout: 30 # The following are a few example events you can configure # NOTE: Please make sure to change your handler code to work with those events # Check the event documentation for details diff --git a/app/web/src/components/IdeViewer/IdeViewer.tsx b/app/web/src/components/IdeViewer/IdeViewer.tsx index 287f126..3a0b63b 100644 --- a/app/web/src/components/IdeViewer/IdeViewer.tsx +++ b/app/web/src/components/IdeViewer/IdeViewer.tsx @@ -25,7 +25,7 @@ const IdeViewer = ({ }) thunkDispatch((dispatch, getState) => { const state = getState() - if (['png', 'INIT'].includes(state?.objectData?.type)) { + if (['png', 'INIT'].includes(state?.objectData?.type) && (ideType === 'openscad' || state?.objectData?.type === 'INIT' || !state?.objectData?.type)) { dispatch({ type: 'setLoading' }) requestRender({ state, diff --git a/app/web/src/components/IdeViewer/PureIdeViewer.tsx b/app/web/src/components/IdeViewer/PureIdeViewer.tsx index 3f03d85..56ec2a7 100644 --- a/app/web/src/components/IdeViewer/PureIdeViewer.tsx +++ b/app/web/src/components/IdeViewer/PureIdeViewer.tsx @@ -218,7 +218,7 @@ export function PureIdeViewer({ )}
setIsDragging(true)} onInit={onInit} - onCameraChange={onCameraChange} + onCameraChange={() => {onCameraChange(); setIsDragging(false)}} controlsRef={controlsRef} camera={camera} /> diff --git a/app/web/src/helpers/cadPackages/curv/curvController.ts b/app/web/src/helpers/cadPackages/curv/curvController.ts index e972b68..af452ce 100644 --- a/app/web/src/helpers/cadPackages/curv/curvController.ts +++ b/app/web/src/helpers/cadPackages/curv/curvController.ts @@ -14,33 +14,12 @@ export const render = async ({ code, settings }: RenderArgs) => { x: Math.round(settings.viewerSize?.width * pixelRatio), y: Math.round(settings.viewerSize?.height * pixelRatio), } - const round1dec = (number) => Math.round((number + Number.EPSILON) * 10) / 10 const body = JSON.stringify({ settings: { size, - viewAll: settings.viewAll, - parameters: settings.parameters, - camera: { - // rounding to give our caching a chance to sometimes work - ...settings.camera, - dist: round1dec(settings.camera.dist), - position: { - x: round1dec(settings.camera.position.x), - y: round1dec(settings.camera.position.y), - z: round1dec(settings.camera.position.z), - }, - rotation: { - x: round1dec(settings.camera.rotation.x), - y: round1dec(settings.camera.rotation.y), - z: round1dec(settings.camera.rotation.z), - }, - }, }, file: code, }) - if (!settings.camera.position) { - return - } try { const response = await fetch(lambdaBaseURL + '/curv/preview', { method: 'POST',