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.
This commit is contained in:
Kurt Hutten
2021-11-22 20:05:10 +11:00
parent 0face9b9c1
commit fe059384d3
11 changed files with 41 additions and 39 deletions

View File

@@ -9,12 +9,13 @@ const stl = async (req, _context, callback) => {
console.log('eventBody', eventBody) console.log('eventBody', eventBody)
const { file, settings } = JSON.parse(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({ await storeAssetAndReturnUrl({
error, error,
callback, callback,
fullPath, fullPath,
consoleMessage, consoleMessage,
tempFile
}) })
} }

View File

@@ -53,7 +53,7 @@ export const runCQ = async ({
15000, 15000,
true true
) )
return { consoleMessage, fullPath } return { consoleMessage, fullPath, tempFile }
} catch (error) { } catch (error) {
return { error: consoleMessage || error, fullPath } return { error: consoleMessage || error, fullPath }
} }

View File

@@ -104,11 +104,13 @@ export async function storeAssetAndReturnUrl({
callback, callback,
fullPath, fullPath,
consoleMessage, consoleMessage,
tempFile
}: { }: {
error: string error: string
callback: Function callback: Function
fullPath: string fullPath: string
consoleMessage: string consoleMessage: string
tempFile: string
}) { }) {
if (error) { if (error) {
const response = { const response = {

View File

@@ -9,7 +9,7 @@ const preview = async (req, _context, callback) => {
console.log('eventBody', eventBody) console.log('eventBody', eventBody)
const { file, settings } = JSON.parse(eventBody) const { file, settings } = JSON.parse(eventBody)
const { error, consoleMessage, fullPath } = await runCurv({ const { error, consoleMessage, fullPath, tempFile } = await runCurv({
file, file,
settings, settings,
}) })
@@ -18,6 +18,7 @@ const preview = async (req, _context, callback) => {
callback, callback,
fullPath, fullPath,
consoleMessage, consoleMessage,
tempFile,
}) })
} }
@@ -28,7 +29,7 @@ const stl = async (req, _context, callback) => {
console.log(eventBody, 'eventBody') console.log(eventBody, 'eventBody')
const { file, settings } = JSON.parse(eventBody) const { file, settings } = JSON.parse(eventBody)
const { error, consoleMessage, fullPath } = await stlExport({ const { error, consoleMessage, fullPath, tempFile } = await stlExport({
file, file,
settings, settings,
}) })
@@ -37,6 +38,7 @@ const stl = async (req, _context, callback) => {
callback, callback,
fullPath, fullPath,
consoleMessage, consoleMessage,
tempFile,
}) })
} }

View File

@@ -18,6 +18,7 @@ export const runCurv = async ({
consoleMessage?: string consoleMessage?: string
fullPath?: string fullPath?: string
customizerPath?: string customizerPath?: string
tempFile?: string
}> => { }> => {
const tempFile = await writeFiles( 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}`, `cat ${imPath} /var/task/cadhub-concat-split /tmp/${tempFile}/metadata.json | gzip > ${fullPath}`,
15000 15000
) )
return { consoleMessage, fullPath, customizerPath } return { consoleMessage, fullPath, customizerPath, tempFile }
} catch (dirtyError) { } catch (dirtyError) {
return { error: dirtyError } return { error: dirtyError }
} }
@@ -90,11 +91,12 @@ export const stlExport = async ({ file, settings: { parameters } } = {}) => {
const fullPath = `/tmp/${tempFile}/output.gz` const fullPath = `/tmp/${tempFile}/output.gz`
const stlPath = `/tmp/${tempFile}/output.stl` const stlPath = `/tmp/${tempFile}/output.stl`
const command = [ const command = [
'curv', '(cd /tmp && curv',
'-o', stlPath, '-o', stlPath,
'-O jit', '-O jit',
'-O vsize=0.4', '-O vsize=0.6',
`/tmp/${tempFile}/main.curv`, `/tmp/${tempFile}/main.curv`,
')',
].join(' ') ].join(' ')
try { try {
@@ -113,10 +115,10 @@ export const stlExport = async ({ file, settings: { parameters } } = {}) => {
tempFile tempFile
) )
await runCommand( 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 15000
) )
return { consoleMessage, fullPath } return { consoleMessage, fullPath, tempFile }
} catch (error) { } catch (error) {
return { error, fullPath } return { error, fullPath }
} }

View File

@@ -9,7 +9,7 @@ const preview = async (req, _context, callback) => {
console.log('eventBody', eventBody) console.log('eventBody', eventBody)
const { file, settings } = JSON.parse(eventBody) const { file, settings } = JSON.parse(eventBody)
const { error, consoleMessage, fullPath } = await runScad({ const { error, consoleMessage, fullPath, tempFile } = await runScad({
file, file,
settings, settings,
}) })
@@ -18,6 +18,7 @@ const preview = async (req, _context, callback) => {
callback, callback,
fullPath, fullPath,
consoleMessage, consoleMessage,
tempFile,
}) })
} }
@@ -28,7 +29,7 @@ const stl = async (req, _context, callback) => {
console.log(eventBody, 'eventBody') console.log(eventBody, 'eventBody')
const { file, settings } = JSON.parse(eventBody) const { file, settings } = JSON.parse(eventBody)
const { error, consoleMessage, fullPath } = await stlExport({ const { error, consoleMessage, fullPath, tempFile } = await stlExport({
file, file,
settings, settings,
}) })
@@ -37,6 +38,7 @@ const stl = async (req, _context, callback) => {
callback, callback,
fullPath, fullPath,
consoleMessage, consoleMessage,
tempFile,
}) })
} }

View File

@@ -25,6 +25,7 @@ export const runScad = async ({
consoleMessage?: string consoleMessage?: string
fullPath?: string fullPath?: string
customizerPath?: string customizerPath?: string
tempFile?: string
}> => { }> => {
const tempFile = await writeFiles( 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}`, `cat ${imPath} /var/task/cadhub-concat-split /tmp/${tempFile}/metadata.json | gzip > ${fullPath}`,
15000 15000
) )
return { consoleMessage, fullPath, customizerPath } return { consoleMessage, fullPath, customizerPath, tempFile }
} catch (dirtyError) { } catch (dirtyError) {
return { error: cleanOpenScadError(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}`, `cat ${stlPath} /var/task/cadhub-concat-split /tmp/${tempFile}/metadata.json | gzip > ${fullPath}`,
15000 15000
) )
return { consoleMessage, fullPath, customizerPath } return { consoleMessage, fullPath, customizerPath, tempFile }
} catch (error) { } catch (error) {
return { error, fullPath } return { error, fullPath }
} }

View File

@@ -87,6 +87,7 @@ functions:
timeout: 30 timeout: 30
environment: environment:
BUCKET: cad-preview-bucket-prod-001 BUCKET: cad-preview-bucket-prod-001
cadquerystl: cadquerystl:
image: image:
name: cadqueryimage name: cadqueryimage
@@ -102,6 +103,7 @@ functions:
timeout: 30 timeout: 30
environment: environment:
BUCKET: cad-preview-bucket-prod-001 BUCKET: cad-preview-bucket-prod-001
curvpreview: curvpreview:
image: image:
name: curvimage name: curvimage
@@ -115,8 +117,19 @@ functions:
method: post method: post
cors: true cors: true
timeout: 25 timeout: 25
environment: curvstl:
BUCKET: cad-preview-bucket-prod-001 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 # The following are a few example events you can configure
# NOTE: Please make sure to change your handler code to work with those events # NOTE: Please make sure to change your handler code to work with those events
# Check the event documentation for details # Check the event documentation for details

View File

@@ -25,7 +25,7 @@ const IdeViewer = ({
}) })
thunkDispatch((dispatch, getState) => { thunkDispatch((dispatch, getState) => {
const state = 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' }) dispatch({ type: 'setLoading' })
requestRender({ requestRender({
state, state,

View File

@@ -218,7 +218,7 @@ export function PureIdeViewer({
)} )}
<div // eslint-disable-line jsx-a11y/no-static-element-interactions <div // eslint-disable-line jsx-a11y/no-static-element-interactions
className={`opacity-0 absolute inset-0 transition-opacity duration-500 ${ className={`opacity-0 absolute inset-0 transition-opacity duration-500 ${
ideType === 'curv' ? // TODO hide axes while curve doesn't have a controllable camera ideType === 'curv' && dataType === 'png' ? // TODO hide axes while curve doesn't have a controllable camera
'opacity-0' : 'opacity-0' :
!(isDragging || dataType !== 'png') !(isDragging || dataType !== 'png')
? 'hover:opacity-50' ? 'hover:opacity-50'
@@ -230,7 +230,7 @@ export function PureIdeViewer({
<Controls <Controls
onDragStart={() => setIsDragging(true)} onDragStart={() => setIsDragging(true)}
onInit={onInit} onInit={onInit}
onCameraChange={onCameraChange} onCameraChange={() => {onCameraChange(); setIsDragging(false)}}
controlsRef={controlsRef} controlsRef={controlsRef}
camera={camera} camera={camera}
/> />

View File

@@ -14,33 +14,12 @@ export const render = async ({ code, settings }: RenderArgs) => {
x: Math.round(settings.viewerSize?.width * pixelRatio), x: Math.round(settings.viewerSize?.width * pixelRatio),
y: Math.round(settings.viewerSize?.height * pixelRatio), y: Math.round(settings.viewerSize?.height * pixelRatio),
} }
const round1dec = (number) => Math.round((number + Number.EPSILON) * 10) / 10
const body = JSON.stringify({ const body = JSON.stringify({
settings: { settings: {
size, 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, file: code,
}) })
if (!settings.camera.position) {
return
}
try { try {
const response = await fetch(lambdaBaseURL + '/curv/preview', { const response = await fetch(lambdaBaseURL + '/curv/preview', {
method: 'POST', method: 'POST',