2018-02-14 10:12:46 -05:00
|
|
|
request = require 'request'
|
|
|
|
FileWriter = require('../../infrastructure/FileWriter')
|
2018-02-16 12:13:26 -05:00
|
|
|
_ = 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 = {
|
2018-02-16 12:13:26 -05:00
|
|
|
UrlFetchFailedError: UrlFetchFailedError
|
|
|
|
InvalidUrlError: InvalidUrlError
|
|
|
|
|
2018-02-14 10:12:46 -05:00
|
|
|
sanitizeData: (data) ->
|
|
|
|
return {
|
|
|
|
url: data.url
|
|
|
|
}
|
|
|
|
|
2018-02-16 12:13:26 -05:00
|
|
|
_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) ->) ->
|
2018-02-16 11:35:51 -05:00
|
|
|
# TODO: Proxy through external API
|
2018-02-16 12:13:26 -05:00
|
|
|
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)
|
2018-02-16 12:13:26 -05:00
|
|
|
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
|
|
|
}
|