OutputFileOptimiser.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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('lodash')
  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(
  32. src,
  33. function (err, isOptimised) {
  34. if (err != null || isOptimised) {
  35. return callback(null)
  36. }
  37. return OutputFileOptimiser.optimisePDF(src, dst, callback)
  38. }
  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 = Buffer.alloc(SIZE) // fills with zeroes by default
  47. return fs.open(file, 'r', function (err, fd) {
  48. if (err != null) {
  49. return callback(err)
  50. }
  51. return fs.read(fd, result, 0, SIZE, 0, (errRead, bytesRead, buffer) =>
  52. fs.close(fd, function (errClose) {
  53. if (errRead != null) {
  54. return callback(errRead)
  55. }
  56. if (typeof errReadClose !== 'undefined' && errReadClose !== null) {
  57. return callback(errClose)
  58. }
  59. const isOptimised =
  60. buffer.toString('ascii').indexOf('/Linearized 1') >= 0
  61. return callback(null, isOptimised)
  62. })
  63. )
  64. })
  65. },
  66. optimisePDF(src, dst, callback) {
  67. if (callback == null) {
  68. callback = function (error) {}
  69. }
  70. const tmpOutput = dst + '.opt'
  71. const args = ['--linearize', '--newline-before-endstream', src, tmpOutput]
  72. logger.log({ args }, 'running qpdf command')
  73. const timer = new Metrics.Timer('qpdf')
  74. const proc = spawn('qpdf', args)
  75. let stdout = ''
  76. proc.stdout.setEncoding('utf8').on('data', chunk => (stdout += chunk))
  77. callback = _.once(callback) // avoid double call back for error and close event
  78. proc.on('error', function (err) {
  79. logger.warn({ err, args }, 'qpdf failed')
  80. return callback(null)
  81. }) // ignore the error
  82. return proc.on('close', function (code) {
  83. timer.done()
  84. if (code !== 0) {
  85. logger.warn({ code, args }, 'qpdf returned error')
  86. return callback(null) // ignore the error
  87. }
  88. return fs.rename(tmpOutput, dst, function (err) {
  89. if (err != null) {
  90. logger.warn(
  91. { tmpOutput, dst },
  92. 'failed to rename output of qpdf command'
  93. )
  94. }
  95. return callback(null)
  96. })
  97. })
  98. }, // ignore the error
  99. }