ImageOptimiser.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* eslint-disable
  2. no-unused-vars,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const { exec } = require('child_process')
  13. const logger = require('logger-sharelatex')
  14. const Settings = require('settings-sharelatex')
  15. module.exports = {
  16. compressPng(localPath, callback) {
  17. const startTime = new Date()
  18. logger.log({ localPath }, 'optimising png path')
  19. const args = `optipng ${localPath}`
  20. const opts = {
  21. timeout: 30 * 1000,
  22. killSignal: 'SIGKILL'
  23. }
  24. if (!Settings.enableConversions) {
  25. const error = new Error('Image conversions are disabled')
  26. return callback(error)
  27. }
  28. return exec(args, opts, function(err, stdout, stderr) {
  29. if (err != null && err.signal === 'SIGKILL') {
  30. logger.warn({ err, stderr, localPath }, 'optimiser timeout reached')
  31. err = null
  32. } else if (err != null) {
  33. logger.err(
  34. { err, stderr, localPath },
  35. 'something went wrong converting compressPng'
  36. )
  37. } else {
  38. logger.log({ localPath }, 'finished compressPng file')
  39. }
  40. return callback(err)
  41. })
  42. }
  43. }