overleaf/services/web/app/coffee/Features/Blog/BlogController.coffee

46 lines
1.4 KiB
CoffeeScript
Raw Normal View History

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