overleaf/services/filestore/app/js/ImageOptimiser.js

35 lines
746 B
JavaScript
Raw Normal View History

const logger = require('@overleaf/logger')
const metrics = require('@overleaf/metrics')
2019-12-19 16:56:03 +00:00
const { callbackify } = require('util')
const safeExec = require('./SafeExec').promises
2014-02-14 16:39:05 +00:00
module.exports = {
2019-12-19 16:56:03 +00:00
compressPng: callbackify(compressPng),
promises: {
2021-07-13 11:04:46 +00:00
compressPng,
},
2019-12-19 16:56:03 +00:00
}
async function compressPng(localPath, callback) {
const timer = new metrics.Timer('compressPng')
const args = ['optipng', localPath]
const opts = {
timeout: 30 * 1000,
2021-07-13 11:04:46 +00:00
killSignal: 'SIGKILL',
2019-12-19 16:56:03 +00:00
}
try {
await safeExec(args, opts)
timer.done()
} catch (err) {
if (err.code === 'SIGKILL') {
logger.warn(
{ err, stderr: err.stderr, localPath },
'optimiser timeout reached'
)
} else {
throw err
}
}
}