overleaf/services/filestore/app/coffee/FileConverter.coffee

65 lines
2.4 KiB
CoffeeScript
Raw Normal View History

2014-02-14 11:39:05 -05:00
_ = require("underscore")
metrics = require("./metrics")
logger = require("logger-sharelatex")
exec = require('child_process').exec
2014-02-14 11:39:05 -05:00
approvedFormats = ["png"]
2014-02-20 06:45:51 -05:00
twentySeconds = 20 * 1000
childProcessOpts =
killSignal: "SIGKILL"
timeout: twentySeconds
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
args = "nice convert -flatten -density 300 #{sourcePath} #{destPath}"
exec args, childProcessOpts, (err, stdout, stderr)->
2014-02-14 11:39:05 -05:00
timer.done()
2014-02-19 08:32:41 -05:00
if err?
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
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)->
logger.log sourcePath:sourcePath, "thumbnail convert file"
destPath = "#{sourcePath}.png"
sourcePath = "#{sourcePath}[0]"
args =
src: sourcePath
dst: destPath
width: 424
height: 300
args = "nice convert -flatten -background white -resize 260x -density 300 #{sourcePath} #{destPath}"
exec args, childProcessOpts, (err, stdout, stderr)->
2014-02-19 08:32:41 -05:00
if err?
logger.err err:err, stderr:stderr, sourcePath:sourcePath, "something went wrong converting file to preview"
else
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]"
args =
src: sourcePath
dst: destPath
width: 600
height: 849
args = "nice convert -flatten -background white -resize 548x -density 300 #{sourcePath} #{destPath}"
exec args, childProcessOpts, (err, stdout, stderr)->
2014-02-19 08:32:41 -05:00
if err?
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
logger.log sourcePath:sourcePath, destPath:destPath, "finished converting file to preview"
2014-02-14 11:39:05 -05:00
callback(err, destPath)