2014-02-14 11:39:05 -05:00
|
|
|
_ = require("underscore")
|
2014-05-12 10:45:15 -04:00
|
|
|
metrics = require("metrics-sharelatex")
|
2014-02-14 11:39:05 -05:00
|
|
|
logger = require("logger-sharelatex")
|
2015-03-13 06:10:51 -04:00
|
|
|
safe_exec = require("./SafeExec")
|
2014-02-14 11:39:05 -05:00
|
|
|
approvedFormats = ["png"]
|
2016-05-09 06:37:35 -04:00
|
|
|
Settings = require "settings-sharelatex"
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2014-03-14 04:55:28 -04:00
|
|
|
fourtySeconds = 40 * 1000
|
2014-02-19 08:02:53 -05:00
|
|
|
|
2014-03-04 07:44:16 -05:00
|
|
|
childProcessOpts =
|
2015-03-13 06:15:37 -04:00
|
|
|
killSignal: "SIGTERM"
|
2014-03-14 04:55:28 -04:00
|
|
|
timeout: fourtySeconds
|
|
|
|
|
2014-03-04 07:44:16 -05:00
|
|
|
|
2014-02-14 11:39:05 -05:00
|
|
|
module.exports =
|
|
|
|
|
|
|
|
convert: (sourcePath, requestedFormat, callback)->
|
|
|
|
logger.log sourcePath:sourcePath, requestedFormat:requestedFormat, "converting file format"
|
|
|
|
timer = new metrics.Timer("imageConvert")
|
|
|
|
destPath = "#{sourcePath}.#{requestedFormat}"
|
|
|
|
sourcePath = "#{sourcePath}[0]"
|
|
|
|
if !_.include approvedFormats, requestedFormat
|
|
|
|
err = new Error("invalid format requested")
|
|
|
|
return callback err
|
2014-03-13 10:04:46 -04:00
|
|
|
width = "600x"
|
2016-05-09 06:37:35 -04:00
|
|
|
command = ["convert", "-define", "pdf:fit-page=#{width}", "-flatten", "-density", "300", sourcePath, destPath]
|
|
|
|
command = Settings.commands.convertCommandPrefix.concat(command)
|
|
|
|
safe_exec command, childProcessOpts, (err, stdout, stderr)->
|
2014-02-14 11:39:05 -05:00
|
|
|
timer.done()
|
2014-02-19 08:32:41 -05:00
|
|
|
if err?
|
2014-02-26 04:06:47 -05:00
|
|
|
logger.err err:err, stderr:stderr, sourcePath:sourcePath, requestedFormat:requestedFormat, destPath:destPath, "something went wrong converting file"
|
2014-02-19 08:32:41 -05:00
|
|
|
else
|
2014-02-26 04:06:47 -05:00
|
|
|
logger.log sourcePath:sourcePath, requestedFormat:requestedFormat, destPath:destPath, "finished converting file"
|
2014-02-14 11:39:05 -05:00
|
|
|
callback(err, destPath)
|
|
|
|
|
|
|
|
thumbnail: (sourcePath, callback)->
|
|
|
|
destPath = "#{sourcePath}.png"
|
|
|
|
sourcePath = "#{sourcePath}[0]"
|
2014-03-13 10:04:46 -04:00
|
|
|
width = "260x"
|
2016-05-09 06:37:35 -04:00
|
|
|
command = ["convert", "-flatten", "-background", "white", "-density", "300", "-define", "pdf:fit-page=#{width}", sourcePath, "-resize", width, destPath]
|
2018-05-21 04:37:54 -04:00
|
|
|
logger.log sourcePath:sourcePath, destPath:destPath, command:command, "thumbnail convert file"
|
2016-05-09 06:37:35 -04:00
|
|
|
command = Settings.commands.convertCommandPrefix.concat(command)
|
|
|
|
safe_exec command, childProcessOpts, (err, stdout, stderr)->
|
2014-02-19 08:32:41 -05:00
|
|
|
if err?
|
2015-04-09 12:14:47 -04:00
|
|
|
logger.err err:err, stderr:stderr, sourcePath:sourcePath, "something went wrong converting file to thumbnail"
|
2014-02-19 08:32:41 -05:00
|
|
|
else
|
2014-02-26 04:06:47 -05:00
|
|
|
logger.log sourcePath:sourcePath, destPath:destPath, "finished thumbnailing file"
|
2014-02-14 11:39:05 -05:00
|
|
|
callback(err, destPath)
|
|
|
|
|
|
|
|
preview: (sourcePath, callback)->
|
|
|
|
logger.log sourcePath:sourcePath, "preview convert file"
|
|
|
|
destPath = "#{sourcePath}.png"
|
|
|
|
sourcePath = "#{sourcePath}[0]"
|
2014-03-13 10:04:46 -04:00
|
|
|
width = "548x"
|
2016-05-09 06:37:35 -04:00
|
|
|
command = ["convert", "-flatten", "-background", "white", "-density", "300", "-define", "pdf:fit-page=#{width}", sourcePath, "-resize", width, destPath]
|
|
|
|
command = Settings.commands.convertCommandPrefix.concat(command)
|
|
|
|
safe_exec command, childProcessOpts, (err, stdout, stderr)->
|
2014-02-19 08:32:41 -05:00
|
|
|
if err?
|
2014-02-26 04:06:47 -05:00
|
|
|
logger.err err:err, stderr:stderr, sourcePath:sourcePath, destPath:destPath, "something went wrong converting file to preview"
|
2014-02-19 08:32:41 -05:00
|
|
|
else
|
2014-02-26 04:06:47 -05:00
|
|
|
logger.log sourcePath:sourcePath, destPath:destPath, "finished converting file to preview"
|
2014-02-14 11:39:05 -05:00
|
|
|
callback(err, destPath)
|