FileConverter.coffee 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. _ = require("underscore")
  2. metrics = require("./metrics")
  3. logger = require("logger-sharelatex")
  4. exec = require('child_process').exec
  5. approvedFormats = ["png"]
  6. twoMinsInMs = 2 * (60 * 1000)
  7. module.exports =
  8. convert: (sourcePath, requestedFormat, callback)->
  9. logger.log sourcePath:sourcePath, requestedFormat:requestedFormat, "converting file format"
  10. timer = new metrics.Timer("imageConvert")
  11. destPath = "#{sourcePath}.#{requestedFormat}"
  12. sourcePath = "#{sourcePath}[0]"
  13. if !_.include approvedFormats, requestedFormat
  14. err = new Error("invalid format requested")
  15. return callback err
  16. args = "nice convert -flatten -density 300 #{sourcePath} #{destPath}"
  17. opts =
  18. timeout: twoMinsInMs
  19. exec args, opts, (err, stdout, stderr)->
  20. timer.done()
  21. callback(err, destPath)
  22. thumbnail: (sourcePath, callback)->
  23. logger.log sourcePath:sourcePath, "thumbnail convert file"
  24. destPath = "#{sourcePath}.png"
  25. sourcePath = "#{sourcePath}[0]"
  26. args =
  27. src: sourcePath
  28. dst: destPath
  29. width: 424
  30. height: 300
  31. args = "nice convert -flatten -background white -resize 260x -density 300 #{sourcePath} #{destPath}"
  32. opts =
  33. timeout: twoMinsInMs
  34. exec args, opts,(err, stdout, stderr)->
  35. callback(err, destPath)
  36. preview: (sourcePath, callback)->
  37. logger.log sourcePath:sourcePath, "preview convert file"
  38. destPath = "#{sourcePath}.png"
  39. sourcePath = "#{sourcePath}[0]"
  40. args =
  41. src: sourcePath
  42. dst: destPath
  43. width: 600
  44. height: 849
  45. args = "nice convert -flatten -background white -resize 548x -density 300 #{sourcePath} #{destPath}"
  46. opts =
  47. timeout: twoMinsInMs
  48. exec args, opts,(err, stdout, stderr)->
  49. callback(err, destPath)