| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- _ = require("underscore")
- metrics = require("./metrics")
- logger = require("logger-sharelatex")
- exec = require('child_process').exec
- approvedFormats = ["png"]
- twoMinsInMs = 2 * (60 * 1000)
- 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}"
- opts =
- timeout: twoMinsInMs
- exec args, opts, (err, stdout, stderr)->
- timer.done()
- 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}"
- opts =
- timeout: twoMinsInMs
- exec args, opts,(err, stdout, stderr)->
- 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}"
- opts =
- timeout: twoMinsInMs
- exec args, opts,(err, stdout, stderr)->
- callback(err, destPath)
|