overleaf/services/web/app/coffee/Features/LinkedFiles/UrlAgent.coffee

62 lines
1.8 KiB
CoffeeScript
Raw Normal View History

2018-02-14 10:12:46 -05:00
request = require 'request'
FileWriter = require('../../infrastructure/FileWriter')
_ = require "underscore"
urlValidator = require 'valid-url'
UrlFetchFailedError = (message) ->
error = new Error(message)
error.name = 'UrlFetchFailedError'
error.__proto__ = UrlFetchFailedError.prototype
return error
UrlFetchFailedError.prototype.__proto__ = Error.prototype
InvalidUrlError = (message) ->
error = new Error(message)
error.name = 'InvalidUrlError'
error.__proto__ = InvalidUrlError.prototype
return error
InvalidUrlError.prototype.__proto__ = Error.prototype
2018-02-14 10:12:46 -05:00
module.exports = UrlAgent = {
UrlFetchFailedError: UrlFetchFailedError
InvalidUrlError: InvalidUrlError
2018-02-14 10:12:46 -05:00
sanitizeData: (data) ->
return {
url: data.url
}
_prependHttpIfNeeded: (url) ->
if !url.match('://')
url = 'http://' + url
return url
2018-02-14 10:12:46 -05:00
writeIncomingFileToDisk: (project_id, data, current_user_id, callback = (error, fsPath) ->) ->
# TODO: Proxy through external API
callback = _.once(callback)
url = @._prependHttpIfNeeded(data.url)
if !urlValidator.isWebUri(url)
return callback(new InvalidUrlError())
2018-02-14 10:12:46 -05:00
readStream = request.get(url)
readStream.on "error", callback
readStream.on "response", (response) ->
if 200 <= response.statusCode < 300
FileWriter.writeStreamToDisk project_id, readStream, callback
else
error = new UrlFetchFailedError()
error.statusCode = response.statusCode
callback(error)
handleError: (error, req, res, next) ->
if error instanceof UrlFetchFailedError
res.status(422).send(
"Your URL could not be reached (#{error.statusCode} status code). Please check it and try again."
)
else if error instanceof InvalidUrlError
res.status(422).send(
"Your URL is not valid. Please check it and try again."
)
else
next(error)
2018-02-14 10:12:46 -05:00
}