2014-07-09 14:49:39 -04:00
|
|
|
request = require("request")
|
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
logger = require("logger-sharelatex")
|
2014-07-10 09:53:53 -04:00
|
|
|
_ = require("underscore")
|
2014-07-23 07:16:49 -04:00
|
|
|
ErrorController = require "../Errors/ErrorController"
|
2014-07-09 14:49:39 -04:00
|
|
|
|
2014-07-10 09:53:53 -04:00
|
|
|
module.exports = BlogController =
|
2014-07-09 14:49:39 -04:00
|
|
|
|
2014-07-23 07:16:49 -04:00
|
|
|
getPage: (req, res, next)->
|
2014-07-10 09:53:53 -04:00
|
|
|
url = req.url?.toLowerCase()
|
|
|
|
blogUrl = "#{settings.apis.blog.url}#{url}"
|
|
|
|
|
2016-03-07 11:34:34 -05:00
|
|
|
extensionsToProxy = [".png", ".xml", ".jpeg", ".json", ".zip", ".eps", ".gif"]
|
2014-10-08 11:07:44 -04:00
|
|
|
|
2014-07-10 09:53:53 -04:00
|
|
|
shouldProxy = _.find extensionsToProxy, (extension)->
|
|
|
|
url.indexOf(extension) != -1
|
|
|
|
|
|
|
|
if shouldProxy
|
|
|
|
return BlogController._directProxy blogUrl, res
|
|
|
|
|
2014-07-09 14:49:39 -04:00
|
|
|
logger.log url:url, "proxying request to blog api"
|
2014-07-10 09:53:53 -04:00
|
|
|
request.get blogUrl, (err, r, data)->
|
2016-06-29 10:36:33 -04:00
|
|
|
if r?.statusCode == 404 or r?.statusCode == 403
|
2014-07-23 07:16:49 -04:00
|
|
|
return ErrorController.notFound(req, res, next)
|
2015-08-20 11:55:06 -04:00
|
|
|
if err?
|
|
|
|
return res.send 500
|
2014-07-10 09:53:53 -04:00
|
|
|
data = data.trim()
|
2014-07-09 14:49:39 -04:00
|
|
|
try
|
|
|
|
data = JSON.parse(data)
|
2016-07-26 06:11:28 -04:00
|
|
|
if settings.cdn?.web?.host?
|
2016-09-20 09:47:33 -04:00
|
|
|
data?.content = data?.content?.replace(/src="([^"\:]+)"/g, "src='#{settings.cdn?.web?.host}$1'");
|
2014-07-09 14:49:39 -04:00
|
|
|
catch err
|
|
|
|
logger.err err:err, data:data, "error parsing data from data"
|
2014-07-10 09:53:53 -04:00
|
|
|
res.render "blog/blog_holder", data
|
|
|
|
|
|
|
|
|
|
|
|
getIndexPage: (req, res)->
|
|
|
|
req.url = "/blog/index.html"
|
|
|
|
BlogController.getPage req, res
|
2014-07-09 14:49:39 -04:00
|
|
|
|
2014-07-10 09:53:53 -04:00
|
|
|
_directProxy: (originUrl, res)->
|
2014-11-24 08:58:41 -05:00
|
|
|
upstream = request.get(originUrl)
|
|
|
|
upstream.on "error", (error) ->
|
|
|
|
logger.error err: error, "blog proxy error"
|
|
|
|
upstream.pipe res
|