2020-02-19 11:14:28 +00:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-undef,
|
|
|
|
no-unused-vars,
|
2022-05-16 14:25:49 +00:00
|
|
|
n/no-deprecated-api,
|
2020-02-19 11:14:28 +00:00
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 11:14:14 +00:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 11:14:37 +00:00
|
|
|
let OutputFileOptimiser
|
|
|
|
const fs = require('fs')
|
|
|
|
const Path = require('path')
|
|
|
|
const { spawn } = require('child_process')
|
2022-03-01 15:09:36 +00:00
|
|
|
const logger = require('@overleaf/logger')
|
2020-02-19 11:14:37 +00:00
|
|
|
const Metrics = require('./Metrics')
|
2020-06-15 08:52:21 +00:00
|
|
|
const _ = require('lodash')
|
2015-02-24 15:48:34 +00:00
|
|
|
|
2020-02-19 11:14:37 +00:00
|
|
|
module.exports = OutputFileOptimiser = {
|
|
|
|
optimiseFile(src, dst, callback) {
|
|
|
|
// check output file (src) and see if we can optimise it, storing
|
|
|
|
// the result in the build directory (dst)
|
|
|
|
if (callback == null) {
|
2021-10-27 09:49:18 +00:00
|
|
|
callback = function () {}
|
2020-02-19 11:14:37 +00:00
|
|
|
}
|
|
|
|
if (src.match(/\/output\.pdf$/)) {
|
2021-07-13 11:04:48 +00:00
|
|
|
return OutputFileOptimiser.checkIfPDFIsOptimised(
|
|
|
|
src,
|
|
|
|
function (err, isOptimised) {
|
|
|
|
if (err != null || isOptimised) {
|
|
|
|
return callback(null)
|
|
|
|
}
|
|
|
|
return OutputFileOptimiser.optimisePDF(src, dst, callback)
|
2020-02-19 11:14:37 +00:00
|
|
|
}
|
2021-07-13 11:04:48 +00:00
|
|
|
)
|
2020-02-19 11:14:37 +00:00
|
|
|
} else {
|
|
|
|
return callback(null)
|
|
|
|
}
|
|
|
|
},
|
2015-02-24 15:48:34 +00:00
|
|
|
|
2020-02-19 11:14:37 +00:00
|
|
|
checkIfPDFIsOptimised(file, callback) {
|
|
|
|
const SIZE = 16 * 1024 // check the header of the pdf
|
2020-05-07 09:42:05 +00:00
|
|
|
const result = Buffer.alloc(SIZE) // fills with zeroes by default
|
2020-08-10 16:01:11 +00:00
|
|
|
return fs.open(file, 'r', function (err, fd) {
|
2020-02-19 11:14:37 +00:00
|
|
|
if (err != null) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
return fs.read(fd, result, 0, SIZE, 0, (errRead, bytesRead, buffer) =>
|
2020-08-10 16:01:11 +00:00
|
|
|
fs.close(fd, function (errClose) {
|
2020-02-19 11:14:37 +00:00
|
|
|
if (errRead != null) {
|
|
|
|
return callback(errRead)
|
|
|
|
}
|
|
|
|
if (typeof errReadClose !== 'undefined' && errReadClose !== null) {
|
|
|
|
return callback(errClose)
|
|
|
|
}
|
|
|
|
const isOptimised =
|
|
|
|
buffer.toString('ascii').indexOf('/Linearized 1') >= 0
|
|
|
|
return callback(null, isOptimised)
|
|
|
|
})
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
2015-02-24 15:48:34 +00:00
|
|
|
|
2020-02-19 11:14:37 +00:00
|
|
|
optimisePDF(src, dst, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 09:49:18 +00:00
|
|
|
callback = function () {}
|
2020-02-19 11:14:37 +00:00
|
|
|
}
|
|
|
|
const tmpOutput = dst + '.opt'
|
2021-03-19 11:35:59 +00:00
|
|
|
const args = ['--linearize', '--newline-before-endstream', src, tmpOutput]
|
2022-05-16 12:38:18 +00:00
|
|
|
logger.debug({ args }, 'running qpdf command')
|
2017-04-04 15:50:06 +00:00
|
|
|
|
2020-02-19 11:14:37 +00:00
|
|
|
const timer = new Metrics.Timer('qpdf')
|
|
|
|
const proc = spawn('qpdf', args)
|
|
|
|
let stdout = ''
|
2021-07-13 11:04:48 +00:00
|
|
|
proc.stdout.setEncoding('utf8').on('data', chunk => (stdout += chunk))
|
2020-02-19 11:14:37 +00:00
|
|
|
callback = _.once(callback) // avoid double call back for error and close event
|
2020-08-10 16:01:11 +00:00
|
|
|
proc.on('error', function (err) {
|
2020-02-19 11:14:37 +00:00
|
|
|
logger.warn({ err, args }, 'qpdf failed')
|
|
|
|
return callback(null)
|
|
|
|
}) // ignore the error
|
2020-08-10 16:01:11 +00:00
|
|
|
return proc.on('close', function (code) {
|
2020-02-19 11:14:37 +00:00
|
|
|
timer.done()
|
|
|
|
if (code !== 0) {
|
|
|
|
logger.warn({ code, args }, 'qpdf returned error')
|
|
|
|
return callback(null) // ignore the error
|
|
|
|
}
|
2020-08-10 16:01:11 +00:00
|
|
|
return fs.rename(tmpOutput, dst, function (err) {
|
2020-02-19 11:14:37 +00:00
|
|
|
if (err != null) {
|
|
|
|
logger.warn(
|
|
|
|
{ tmpOutput, dst },
|
|
|
|
'failed to rename output of qpdf command'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return callback(null)
|
|
|
|
})
|
|
|
|
})
|
2021-07-13 11:04:48 +00:00
|
|
|
}, // ignore the error
|
2020-02-19 11:14:37 +00:00
|
|
|
}
|