OutputFileOptimiser.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* eslint-disable
  2. handle-callback-err,
  3. no-return-assign,
  4. no-undef,
  5. no-unused-vars,
  6. node/no-deprecated-api,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS102: Remove unnecessary code created because of implicit returns
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. let OutputFileOptimiser
  17. const fs = require('fs')
  18. const Path = require('path')
  19. const { spawn } = require('child_process')
  20. const logger = require('logger-sharelatex')
  21. const Metrics = require('./Metrics')
  22. const _ = require('underscore')
  23. module.exports = OutputFileOptimiser = {
  24. optimiseFile(src, dst, callback) {
  25. // check output file (src) and see if we can optimise it, storing
  26. // the result in the build directory (dst)
  27. if (callback == null) {
  28. callback = function(error) {}
  29. }
  30. if (src.match(/\/output\.pdf$/)) {
  31. return OutputFileOptimiser.checkIfPDFIsOptimised(src, function(
  32. err,
  33. isOptimised
  34. ) {
  35. if (err != null || isOptimised) {
  36. return callback(null)
  37. }
  38. return OutputFileOptimiser.optimisePDF(src, dst, callback)
  39. })
  40. } else {
  41. return callback(null)
  42. }
  43. },
  44. checkIfPDFIsOptimised(file, callback) {
  45. const SIZE = 16 * 1024 // check the header of the pdf
  46. const result = new Buffer(SIZE)
  47. result.fill(0) // prevent leakage of uninitialised buffer
  48. return fs.open(file, 'r', function(err, fd) {
  49. if (err != null) {
  50. return callback(err)
  51. }
  52. return fs.read(fd, result, 0, SIZE, 0, (errRead, bytesRead, buffer) =>
  53. fs.close(fd, function(errClose) {
  54. if (errRead != null) {
  55. return callback(errRead)
  56. }
  57. if (typeof errReadClose !== 'undefined' && errReadClose !== null) {
  58. return callback(errClose)
  59. }
  60. const isOptimised =
  61. buffer.toString('ascii').indexOf('/Linearized 1') >= 0
  62. return callback(null, isOptimised)
  63. })
  64. )
  65. })
  66. },
  67. optimisePDF(src, dst, callback) {
  68. if (callback == null) {
  69. callback = function(error) {}
  70. }
  71. const tmpOutput = dst + '.opt'
  72. const args = ['--linearize', src, tmpOutput]
  73. logger.log({ args }, 'running qpdf command')
  74. const timer = new Metrics.Timer('qpdf')
  75. const proc = spawn('qpdf', args)
  76. let stdout = ''
  77. proc.stdout.setEncoding('utf8').on('data', chunk => (stdout += chunk))
  78. callback = _.once(callback) // avoid double call back for error and close event
  79. proc.on('error', function(err) {
  80. logger.warn({ err, args }, 'qpdf failed')
  81. return callback(null)
  82. }) // ignore the error
  83. return proc.on('close', function(code) {
  84. timer.done()
  85. if (code !== 0) {
  86. logger.warn({ code, args }, 'qpdf returned error')
  87. return callback(null) // ignore the error
  88. }
  89. return fs.rename(tmpOutput, dst, function(err) {
  90. if (err != null) {
  91. logger.warn(
  92. { tmpOutput, dst },
  93. 'failed to rename output of qpdf command'
  94. )
  95. }
  96. return callback(null)
  97. })
  98. })
  99. } // ignore the error
  100. }