OutputFileOptimiser.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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) { callback = function(error) {}; }
  28. if (src.match(/\/output\.pdf$/)) {
  29. return OutputFileOptimiser.checkIfPDFIsOptimised(src, function(err, isOptimised) {
  30. if ((err != null) || isOptimised) { return callback(null); }
  31. return OutputFileOptimiser.optimisePDF(src, dst, callback);
  32. });
  33. } else {
  34. return callback((null));
  35. }
  36. },
  37. checkIfPDFIsOptimised(file, callback) {
  38. const SIZE = 16*1024; // check the header of the pdf
  39. const result = new Buffer(SIZE);
  40. result.fill(0); // prevent leakage of uninitialised buffer
  41. return fs.open(file, "r", function(err, fd) {
  42. if (err != null) { return callback(err); }
  43. return fs.read(fd, result, 0, SIZE, 0, (errRead, bytesRead, buffer) =>
  44. fs.close(fd, function(errClose) {
  45. if (errRead != null) { return callback(errRead); }
  46. if (typeof errReadClose !== 'undefined' && errReadClose !== null) { return callback(errClose); }
  47. const isOptimised = buffer.toString('ascii').indexOf("/Linearized 1") >= 0;
  48. return callback(null, isOptimised);
  49. })
  50. );
  51. });
  52. },
  53. optimisePDF(src, dst, callback) {
  54. if (callback == null) { callback = function(error) {}; }
  55. const tmpOutput = dst + '.opt';
  56. const args = ["--linearize", src, tmpOutput];
  57. logger.log({args}, "running qpdf command");
  58. const timer = new Metrics.Timer("qpdf");
  59. const proc = spawn("qpdf", args);
  60. let stdout = "";
  61. proc.stdout.on("data", chunk => stdout += chunk.toString());
  62. callback = _.once(callback); // avoid double call back for error and close event
  63. proc.on("error", function(err) {
  64. logger.warn({err, args}, "qpdf failed");
  65. return callback(null);
  66. }); // ignore the error
  67. return proc.on("close", function(code) {
  68. timer.done();
  69. if (code !== 0) {
  70. logger.warn({code, args}, "qpdf returned error");
  71. return callback(null); // ignore the error
  72. }
  73. return fs.rename(tmpOutput, dst, function(err) {
  74. if (err != null) {
  75. logger.warn({tmpOutput, dst}, "failed to rename output of qpdf command");
  76. }
  77. return callback(null);
  78. });
  79. });
  80. } // ignore the error
  81. });