Implement gzip compression for cad artifacts
The stls from CadQuery and OpenSCAD are not compressed and so we're throwing away bandwidth and taking a performance hit by not gziping. Gzip for s3 basically needs to be gziped before upload and than have 'content-type' : 'text/stl' 'content-encoding' : 'gzip' set. https://stackoverflow.com/questions/8080824/how-to-serve-gzipped-assets-from-amazon-s3 The obvious part that needs to change is putObject in app/api/src/docker/common/utils.js but there might be a few more nuances. resolves #391
This commit is contained in:
@@ -7,7 +7,14 @@ module.exports.runCQ = async ({
|
|||||||
} = {}) => {
|
} = {}) => {
|
||||||
const tempFile = await makeFile(file, '.py', nanoid)
|
const tempFile = await makeFile(file, '.py', nanoid)
|
||||||
const fullPath = `/tmp/${tempFile}/output.stl`
|
const fullPath = `/tmp/${tempFile}/output.stl`
|
||||||
const command = `cq-cli/cq-cli --codec stl --infile /tmp/${tempFile}/main.py --outfile ${fullPath} --outputopts "deflection:${deflection};angularDeflection:${deflection};"`
|
const command = [
|
||||||
|
`cq-cli/cq-cli`,
|
||||||
|
`--codec stl`,
|
||||||
|
`--infile /tmp/${tempFile}/main.py`,
|
||||||
|
`--outfile ${fullPath}`,
|
||||||
|
`--outputopts "deflection:${deflection};angularDeflection:${deflection};"`,
|
||||||
|
`&& gzip ${fullPath}`,
|
||||||
|
].join(' ')
|
||||||
console.log('command', command)
|
console.log('command', command)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ async function storeAssetAndReturnUrl({
|
|||||||
let buffer
|
let buffer
|
||||||
|
|
||||||
try {
|
try {
|
||||||
buffer = await readFile(fullPath)
|
buffer = await readFile(`${fullPath}.gz`)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('read file error', e)
|
console.log('read file error', e)
|
||||||
const response = {
|
const response = {
|
||||||
@@ -134,6 +134,8 @@ async function storeAssetAndReturnUrl({
|
|||||||
Key: key,
|
Key: key,
|
||||||
Body: buffer,
|
Body: buffer,
|
||||||
CacheControl: `max-age=${FiveDays}`, // browser caching to stop downloads of the same part
|
CacheControl: `max-age=${FiveDays}`, // browser caching to stop downloads of the same part
|
||||||
|
ContentType: 'text/stl',
|
||||||
|
ContentEncoding: 'gzip',
|
||||||
Metadata: putConsoleMessageInMetadata(consoleMessage),
|
Metadata: putConsoleMessageInMetadata(consoleMessage),
|
||||||
})
|
})
|
||||||
.promise()
|
.promise()
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
const { makeFile, runCommand } = require('../common/utils')
|
const { makeFile, runCommand } = require('../common/utils')
|
||||||
const { nanoid } = require('nanoid')
|
const { nanoid } = require('nanoid')
|
||||||
|
|
||||||
|
const OPENSCAD_COMMON = `xvfb-run --auto-servernum --server-args "-screen 0 1024x768x24" openscad`
|
||||||
|
|
||||||
/** Removes our generated/hash filename with just "main.scad", so that it's a nice message in the IDE */
|
/** Removes our generated/hash filename with just "main.scad", so that it's a nice message in the IDE */
|
||||||
const cleanOpenScadError = (error) =>
|
const cleanOpenScadError = (error) =>
|
||||||
error.replace(/["|']\/tmp\/.+\/main.scad["|']/g, "'main.scad'")
|
error.replace(/["|']\/tmp\/.+\/main.scad["|']/g, "'main.scad'")
|
||||||
@@ -21,7 +23,15 @@ module.exports.runScad = async ({
|
|||||||
const { x: px, y: py, z: pz } = position
|
const { x: px, y: py, z: pz } = position
|
||||||
const cameraArg = `--camera=${px},${py},${pz},${rx},${ry},${rz},${dist}`
|
const cameraArg = `--camera=${px},${py},${pz},${rx},${ry},${rz},${dist}`
|
||||||
const fullPath = `/tmp/${tempFile}/output.png`
|
const fullPath = `/tmp/${tempFile}/output.png`
|
||||||
const command = `xvfb-run --auto-servernum --server-args "-screen 0 1024x768x24" openscad -o ${fullPath} ${cameraArg} --imgsize=${x},${y} --colorscheme CadHub /tmp/${tempFile}/main.scad`
|
const command = [
|
||||||
|
OPENSCAD_COMMON,
|
||||||
|
`-o ${fullPath}`,
|
||||||
|
cameraArg,
|
||||||
|
`--imgsize=${x},${y}`,
|
||||||
|
`--colorscheme CadHub`,
|
||||||
|
`/tmp/${tempFile}/main.scad`,
|
||||||
|
`&& gzip ${fullPath}`,
|
||||||
|
].join(' ')
|
||||||
console.log('command', command)
|
console.log('command', command)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -36,12 +46,17 @@ module.exports.runScad = async ({
|
|||||||
module.exports.stlExport = async ({ file } = {}) => {
|
module.exports.stlExport = async ({ file } = {}) => {
|
||||||
const tempFile = await makeFile(file, '.scad', nanoid)
|
const tempFile = await makeFile(file, '.scad', nanoid)
|
||||||
const fullPath = `/tmp/${tempFile}/output.stl`
|
const fullPath = `/tmp/${tempFile}/output.stl`
|
||||||
|
const command = [
|
||||||
|
OPENSCAD_COMMON,
|
||||||
|
`--export-format=binstl`,
|
||||||
|
`-o ${fullPath}`,
|
||||||
|
`/tmp/${tempFile}/main.scad`,
|
||||||
|
`&& gzip ${fullPath}`,
|
||||||
|
].join(' ')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const consoleMessage = await runCommand(
|
// lambda will time out before this, we might need to look at background jobs if we do git integration stl generation
|
||||||
`xvfb-run --auto-servernum --server-args "-screen 0 1024x768x24" openscad -o ${fullPath} /tmp/${tempFile}/main.scad`,
|
const consoleMessage = await runCommand(command, 60000)
|
||||||
60000 // lambda will time out before this, we might need to look at background jobs if we do git integration stl generation
|
|
||||||
)
|
|
||||||
return { consoleMessage, fullPath }
|
return { consoleMessage, fullPath }
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return { error, fullPath }
|
return { error, fullPath }
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import {
|
|||||||
export const render = async ({ code }) => {
|
export const render = async ({ code }) => {
|
||||||
const body = JSON.stringify({
|
const body = JSON.stringify({
|
||||||
settings: {
|
settings: {
|
||||||
deflection: 0.2,
|
deflection: 0.15,
|
||||||
},
|
},
|
||||||
file: code,
|
file: code,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user