OutputFileOptimiser.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * decaffeinate suggestions:
  3. * DS102: Remove unnecessary code created because of implicit returns
  4. * DS207: Consider shorter variations of null checks
  5. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  6. */
  7. let OutputFileOptimiser;
  8. const fs = require("fs");
  9. const Path = require("path");
  10. const { spawn } = require("child_process");
  11. const logger = require("logger-sharelatex");
  12. const Metrics = require("./Metrics");
  13. const _ = require("underscore");
  14. module.exports = (OutputFileOptimiser = {
  15. optimiseFile(src, dst, callback) {
  16. // check output file (src) and see if we can optimise it, storing
  17. // the result in the build directory (dst)
  18. if (callback == null) { callback = function(error) {}; }
  19. if (src.match(/\/output\.pdf$/)) {
  20. return OutputFileOptimiser.checkIfPDFIsOptimised(src, function(err, isOptimised) {
  21. if ((err != null) || isOptimised) { return callback(null); }
  22. return OutputFileOptimiser.optimisePDF(src, dst, callback);
  23. });
  24. } else {
  25. return callback((null));
  26. }
  27. },
  28. checkIfPDFIsOptimised(file, callback) {
  29. const SIZE = 16*1024; // check the header of the pdf
  30. const result = new Buffer(SIZE);
  31. result.fill(0); // prevent leakage of uninitialised buffer
  32. return fs.open(file, "r", function(err, fd) {
  33. if (err != null) { return callback(err); }
  34. return fs.read(fd, result, 0, SIZE, 0, (errRead, bytesRead, buffer) =>
  35. fs.close(fd, function(errClose) {
  36. if (errRead != null) { return callback(errRead); }
  37. if (typeof errReadClose !== 'undefined' && errReadClose !== null) { return callback(errClose); }
  38. const isOptimised = buffer.toString('ascii').indexOf("/Linearized 1") >= 0;
  39. return callback(null, isOptimised);
  40. })
  41. );
  42. });
  43. },
  44. optimisePDF(src, dst, callback) {
  45. if (callback == null) { callback = function(error) {}; }
  46. const tmpOutput = dst + '.opt';
  47. const args = ["--linearize", src, tmpOutput];
  48. logger.log({args}, "running qpdf command");
  49. const timer = new Metrics.Timer("qpdf");
  50. const proc = spawn("qpdf", args);
  51. let stdout = "";
  52. proc.stdout.on("data", chunk => stdout += chunk.toString());
  53. callback = _.once(callback); // avoid double call back for error and close event
  54. proc.on("error", function(err) {
  55. logger.warn({err, args}, "qpdf failed");
  56. return callback(null);
  57. }); // ignore the error
  58. return proc.on("close", function(code) {
  59. timer.done();
  60. if (code !== 0) {
  61. logger.warn({code, args}, "qpdf returned error");
  62. return callback(null); // ignore the error
  63. }
  64. return fs.rename(tmpOutput, dst, function(err) {
  65. if (err != null) {
  66. logger.warn({tmpOutput, dst}, "failed to rename output of qpdf command");
  67. }
  68. return callback(null);
  69. });
  70. });
  71. } // ignore the error
  72. });