2014-02-14 16:39:05 +00:00
|
|
|
_ = require("underscore")
|
2014-05-12 14:45:15 +00:00
|
|
|
metrics = require("metrics-sharelatex")
|
2014-02-14 16:39:05 +00:00
|
|
|
logger = require("logger-sharelatex")
|
2014-02-18 17:43:21 +00:00
|
|
|
exec = require('child_process').exec
|
2014-02-14 16:39:05 +00:00
|
|
|
approvedFormats = ["png"]
|
|
|
|
|
2014-03-14 08:55:28 +00:00
|
|
|
fourtySeconds = 40 * 1000
|
2014-02-19 13:02:53 +00:00
|
|
|
|
2014-03-04 12:44:16 +00:00
|
|
|
childProcessOpts =
|
|
|
|
killSignal: "SIGKILL"
|
2014-03-14 08:55:28 +00:00
|
|
|
timeout: fourtySeconds
|
|
|
|
|
2014-03-04 12:44:16 +00:00
|
|
|
|
2014-02-14 16:39:05 +00: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 14:04:46 +00:00
|
|
|
width = "600x"
|
|
|
|
args = "nice convert -define pdf:fit-page=#{width} -flatten -density 300 #{sourcePath} #{destPath}"
|
|
|
|
console.log args
|
2014-03-04 12:44:16 +00:00
|
|
|
exec args, childProcessOpts, (err, stdout, stderr)->
|
2014-02-14 16:39:05 +00:00
|
|
|
timer.done()
|
2014-02-19 13:32:41 +00:00
|
|
|
if err?
|
2014-02-26 09:06:47 +00:00
|
|
|
logger.err err:err, stderr:stderr, sourcePath:sourcePath, requestedFormat:requestedFormat, destPath:destPath, "something went wrong converting file"
|
2014-02-19 13:32:41 +00:00
|
|
|
else
|
2014-02-26 09:06:47 +00:00
|
|
|
logger.log sourcePath:sourcePath, requestedFormat:requestedFormat, destPath:destPath, "finished converting file"
|
2014-02-14 16:39:05 +00:00
|
|
|
callback(err, destPath)
|
|
|
|
|
|
|
|
thumbnail: (sourcePath, callback)->
|
|
|
|
logger.log sourcePath:sourcePath, "thumbnail convert file"
|
|
|
|
destPath = "#{sourcePath}.png"
|
|
|
|
sourcePath = "#{sourcePath}[0]"
|
2014-03-13 14:04:46 +00:00
|
|
|
width = "260x"
|
|
|
|
args = "nice convert -flatten -background white -density 300 -define pdf:fit-page=#{width} #{sourcePath} -resize #{width} #{destPath}"
|
2014-03-04 12:44:16 +00:00
|
|
|
exec args, childProcessOpts, (err, stdout, stderr)->
|
2014-02-19 13:32:41 +00:00
|
|
|
if err?
|
|
|
|
logger.err err:err, stderr:stderr, sourcePath:sourcePath, "something went wrong converting file to preview"
|
|
|
|
else
|
2014-02-26 09:06:47 +00:00
|
|
|
logger.log sourcePath:sourcePath, destPath:destPath, "finished thumbnailing file"
|
2014-02-14 16:39:05 +00:00
|
|
|
callback(err, destPath)
|
|
|
|
|
|
|
|
preview: (sourcePath, callback)->
|
|
|
|
logger.log sourcePath:sourcePath, "preview convert file"
|
|
|
|
destPath = "#{sourcePath}.png"
|
|
|
|
sourcePath = "#{sourcePath}[0]"
|
2014-03-13 14:04:46 +00:00
|
|
|
width = "548x"
|
|
|
|
args = "nice convert -flatten -background white -density 300 -define pdf:fit-page=#{width} #{sourcePath} -resize #{width} #{destPath}"
|
2014-03-04 12:44:16 +00:00
|
|
|
exec args, childProcessOpts, (err, stdout, stderr)->
|
2014-02-19 13:32:41 +00:00
|
|
|
if err?
|
2014-02-26 09:06:47 +00:00
|
|
|
logger.err err:err, stderr:stderr, sourcePath:sourcePath, destPath:destPath, "something went wrong converting file to preview"
|
2014-02-19 13:32:41 +00:00
|
|
|
else
|
2014-02-26 09:06:47 +00:00
|
|
|
logger.log sourcePath:sourcePath, destPath:destPath, "finished converting file to preview"
|
2014-02-14 16:39:05 +00:00
|
|
|
callback(err, destPath)
|