UrlAgent.coffee 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. request = require 'request'
  2. FileWriter = require('../../infrastructure/FileWriter')
  3. _ = require "underscore"
  4. urlValidator = require 'valid-url'
  5. Settings = require 'settings-sharelatex'
  6. UrlFetchFailedError = (message) ->
  7. error = new Error(message)
  8. error.name = 'UrlFetchFailedError'
  9. error.__proto__ = UrlFetchFailedError.prototype
  10. return error
  11. UrlFetchFailedError.prototype.__proto__ = Error.prototype
  12. InvalidUrlError = (message) ->
  13. error = new Error(message)
  14. error.name = 'InvalidUrlError'
  15. error.__proto__ = InvalidUrlError.prototype
  16. return error
  17. InvalidUrlError.prototype.__proto__ = Error.prototype
  18. module.exports = UrlAgent = {
  19. UrlFetchFailedError: UrlFetchFailedError
  20. InvalidUrlError: InvalidUrlError
  21. sanitizeData: (data) ->
  22. return {
  23. url: @._prependHttpIfNeeded(data.url)
  24. }
  25. checkAuth: (project_id, data, current_user_id, callback = (error, allowed)->) ->
  26. callback(null, true)
  27. writeIncomingFileToDisk: (project_id, data, current_user_id, callback = (error, fsPath) ->) ->
  28. callback = _.once(callback)
  29. url = data.url
  30. if !urlValidator.isWebUri(url)
  31. return callback(new InvalidUrlError("invalid url: #{url}"))
  32. url = UrlAgent._wrapWithProxy(url)
  33. readStream = request.get(url)
  34. readStream.on "error", callback
  35. readStream.on "response", (response) ->
  36. if 200 <= response.statusCode < 300
  37. FileWriter.writeStreamToDisk project_id, readStream, callback
  38. else
  39. error = new UrlFetchFailedError("url fetch failed: #{url}")
  40. error.statusCode = response.statusCode
  41. callback(error)
  42. handleError: (error, req, res, next) ->
  43. if error instanceof UrlFetchFailedError
  44. res.status(422).send(
  45. "Your URL could not be reached (#{error.statusCode} status code). Please check it and try again."
  46. )
  47. else if error instanceof InvalidUrlError
  48. res.status(422).send(
  49. "Your URL is not valid. Please check it and try again."
  50. )
  51. else
  52. next(error)
  53. _prependHttpIfNeeded: (url) ->
  54. if !url.match('://')
  55. url = 'http://' + url
  56. return url
  57. _wrapWithProxy: (url) ->
  58. # TODO: Consider what to do for Community and Enterprise edition?
  59. if !Settings.apis?.linkedUrlProxy?.url?
  60. throw new Error('no linked url proxy configured')
  61. return "#{Settings.apis.linkedUrlProxy.url}?url=#{encodeURIComponent(url)}"
  62. }