From 49e1ce552d43a782c290c7bb344af98e96844577 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 24 Feb 2015 15:48:34 +0000 Subject: [PATCH] add an optimisation pass for the cached output files --- .../clsi/app/coffee/OutputCacheManager.coffee | 7 ++++- .../app/coffee/OutputFileOptimiser.coffee | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 services/clsi/app/coffee/OutputFileOptimiser.coffee diff --git a/services/clsi/app/coffee/OutputCacheManager.coffee b/services/clsi/app/coffee/OutputCacheManager.coffee index 0f96b11e9d..479637d848 100644 --- a/services/clsi/app/coffee/OutputCacheManager.coffee +++ b/services/clsi/app/coffee/OutputCacheManager.coffee @@ -5,12 +5,16 @@ Path = require "path" logger = require "logger-sharelatex" _ = require "underscore" +OutputFileOptimiser = require "./OutputFileOptimiser" + module.exports = OutputCacheManager = CACHE_DIR: '.cache/clsi' saveOutputFiles: (outputFiles, target, callback) -> # make a target/build_id directory and # copy all the output files into it + # + # TODO: use Path module buildId = 'build-' + Date.now() relDir = OutputCacheManager.CACHE_DIR + '/' + buildId newDir = target + '/' + relDir @@ -31,7 +35,8 @@ module.exports = OutputCacheManager = else if stats.isFile() #console.log 'isFile: copying' fse.copy src, dst, (err) -> - cb(err, newFile) + OutputFileOptimiser.optimiseFile src, dst, (err, result) -> + cb(err, newFile) else # other filetype - shouldn't happen cb(new Error("output file is not a file"), file) diff --git a/services/clsi/app/coffee/OutputFileOptimiser.coffee b/services/clsi/app/coffee/OutputFileOptimiser.coffee new file mode 100644 index 0000000000..a00dc2053d --- /dev/null +++ b/services/clsi/app/coffee/OutputFileOptimiser.coffee @@ -0,0 +1,30 @@ +fs = require "fs" +Path = require "path" +spawn = require("child_process").spawn +logger = require "logger-sharelatex" + +module.exports = OutputFileOptimiser = + + optimiseFile: (src, dst, callback = (error) ->) -> + if src.match(/\.pdf$/) + OutputFileOptimiser.optimisePDF src, dst, callback + else + callback (null) + + optimisePDF: (src, dst, callback = (error) ->) -> + tmpOutput = dst + '.opt' + args = ["--linearize", src, tmpOutput] + logger.log args: args, "running qpdf command" + + proc = spawn("qpdf", args) + stdout = "" + proc.stdout.on "data", (chunk) -> + stdout += chunk.toString() + proc.on "error", callback + proc.on "close", (code) -> + if code != 0 + logger.warn {directory, code}, "qpdf returned error" + return callback null + fs.rename tmpOutput, dst, (err) -> + # could log an error here + callback null