FileConverter.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* eslint-disable
  2. camelcase,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS207: Consider shorter variations of null checks
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. const _ = require('underscore')
  13. const metrics = require('metrics-sharelatex')
  14. const logger = require('logger-sharelatex')
  15. const safe_exec = require('./SafeExec')
  16. const approvedFormats = ['png']
  17. const Settings = require('settings-sharelatex')
  18. const fourtySeconds = 40 * 1000
  19. const childProcessOpts = {
  20. killSignal: 'SIGTERM',
  21. timeout: fourtySeconds
  22. }
  23. module.exports = {
  24. convert(sourcePath, requestedFormat, callback) {
  25. logger.log({ sourcePath, requestedFormat }, 'converting file format')
  26. const timer = new metrics.Timer('imageConvert')
  27. const destPath = `${sourcePath}.${requestedFormat}`
  28. sourcePath = `${sourcePath}[0]`
  29. if (!_.include(approvedFormats, requestedFormat)) {
  30. const err = new Error('invalid format requested')
  31. return callback(err)
  32. }
  33. const width = '600x'
  34. let command = [
  35. 'convert',
  36. '-define',
  37. `pdf:fit-page=${width}`,
  38. '-flatten',
  39. '-density',
  40. '300',
  41. sourcePath,
  42. destPath
  43. ]
  44. command = Settings.commands.convertCommandPrefix.concat(command)
  45. return safe_exec(command, childProcessOpts, function(err, stdout, stderr) {
  46. timer.done()
  47. if (err != null) {
  48. logger.err(
  49. { err, stderr, sourcePath, requestedFormat, destPath },
  50. 'something went wrong converting file'
  51. )
  52. } else {
  53. logger.log(
  54. { sourcePath, requestedFormat, destPath },
  55. 'finished converting file'
  56. )
  57. }
  58. return callback(err, destPath)
  59. })
  60. },
  61. thumbnail(sourcePath, callback) {
  62. const destPath = `${sourcePath}.png`
  63. sourcePath = `${sourcePath}[0]`
  64. const width = '260x'
  65. let command = [
  66. 'convert',
  67. '-flatten',
  68. '-background',
  69. 'white',
  70. '-density',
  71. '300',
  72. '-define',
  73. `pdf:fit-page=${width}`,
  74. sourcePath,
  75. '-resize',
  76. width,
  77. destPath
  78. ]
  79. logger.log({ sourcePath, destPath, command }, 'thumbnail convert file')
  80. command = Settings.commands.convertCommandPrefix.concat(command)
  81. return safe_exec(command, childProcessOpts, function(err, stdout, stderr) {
  82. if (err != null) {
  83. logger.err(
  84. { err, stderr, sourcePath },
  85. 'something went wrong converting file to thumbnail'
  86. )
  87. } else {
  88. logger.log({ sourcePath, destPath }, 'finished thumbnailing file')
  89. }
  90. return callback(err, destPath)
  91. })
  92. },
  93. preview(sourcePath, callback) {
  94. logger.log({ sourcePath }, 'preview convert file')
  95. const destPath = `${sourcePath}.png`
  96. sourcePath = `${sourcePath}[0]`
  97. const width = '548x'
  98. let command = [
  99. 'convert',
  100. '-flatten',
  101. '-background',
  102. 'white',
  103. '-density',
  104. '300',
  105. '-define',
  106. `pdf:fit-page=${width}`,
  107. sourcePath,
  108. '-resize',
  109. width,
  110. destPath
  111. ]
  112. command = Settings.commands.convertCommandPrefix.concat(command)
  113. return safe_exec(command, childProcessOpts, function(err, stdout, stderr) {
  114. if (err != null) {
  115. logger.err(
  116. { err, stderr, sourcePath, destPath },
  117. 'something went wrong converting file to preview'
  118. )
  119. } else {
  120. logger.log(
  121. { sourcePath, destPath },
  122. 'finished converting file to preview'
  123. )
  124. }
  125. return callback(err, destPath)
  126. })
  127. }
  128. }