2014-06-30 12:08:54 -04:00
|
|
|
request = require("request")
|
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
|
|
|
|
module.exports = TemplatesWebController =
|
|
|
|
|
|
|
|
renderTemplatesIndexPage: (req, res)->
|
|
|
|
logger.log "rendering index page of templates"
|
|
|
|
TemplatesWebController._getDataFromTemplatesApi "/user/#{req.params.user_id}", (err, data)->
|
2014-07-22 12:24:32 -04:00
|
|
|
if err? or !data?
|
2014-06-30 12:21:19 -04:00
|
|
|
logger.err err:err, "something went wrong in renderTemplatesIndexPage"
|
|
|
|
return res.send 500
|
2014-08-01 08:47:14 -04:00
|
|
|
data.title = "latex_templates"
|
2014-06-30 12:08:54 -04:00
|
|
|
res.render "templates/index", data
|
|
|
|
|
|
|
|
renerTemplateInTag: (req, res)->
|
|
|
|
{user_id, tag_name, template_name} = req.params
|
2014-06-30 12:21:19 -04:00
|
|
|
logger.log user_id:user_id, tag_name:tag_name, template_name:template_name, "rendering latex template page"
|
2014-06-30 12:08:54 -04:00
|
|
|
TemplatesWebController._getDataFromTemplatesApi "/user/#{user_id}/tag/#{tag_name}/template/#{template_name}", (err, data)->
|
2014-07-22 12:24:32 -04:00
|
|
|
if err? or !data?
|
2014-06-30 12:21:19 -04:00
|
|
|
logger.err err:err, user_id:user_id, tag_name:tag_name, template_name:template_name, "something went wrong in renerTemplateInTag"
|
|
|
|
return res.send 500
|
2014-07-22 12:24:32 -04:00
|
|
|
data.title = data?.template?.name
|
2014-06-30 12:08:54 -04:00
|
|
|
res.render "templates/template", data
|
|
|
|
|
|
|
|
tagOrCanonicalPage: (req, res)->
|
|
|
|
if req.params.template_id?
|
|
|
|
TemplatesWebController._renderCanonicalPage(req, res)
|
|
|
|
else if req.params.tag_name?.toLowerCase() == "all"
|
|
|
|
TemplatesWebController._renderAllTemplatesPage(req, res)
|
|
|
|
else if req.params.tag_name?
|
|
|
|
TemplatesWebController._renderTagPage(req, res)
|
|
|
|
else
|
|
|
|
logger.log params:req.params, "problem rendering tagOrCanonicalPage"
|
|
|
|
res.send 500
|
|
|
|
|
|
|
|
proxyToTemplatesApi: (req, res)->
|
|
|
|
url = req.url
|
2014-08-12 07:02:09 -04:00
|
|
|
|
|
|
|
name = req.query.name or "Template"
|
|
|
|
if req.query.inline?
|
|
|
|
disposition = "inline"
|
|
|
|
else
|
|
|
|
disposition = "attachment"
|
|
|
|
console.log "HEADER", "#{disposition}; filename=#{name};"
|
|
|
|
res.header({"content-disposition": "#{disposition}; filename=#{name}.#{req.params.file_type};"})
|
|
|
|
|
|
|
|
logger.log url:url, template_name: name, disposition: disposition, "proxying request to templates api"
|
|
|
|
|
2014-06-30 12:08:54 -04:00
|
|
|
getReq = request.get("#{settings.apis.templates_api.url}#{url}")
|
|
|
|
getReq.pipe(res)
|
|
|
|
getReq.on "error", (error) ->
|
|
|
|
logger.error err: error, "templates proxy API error"
|
|
|
|
res.send 500
|
|
|
|
|
2014-06-30 12:21:19 -04:00
|
|
|
_renderCanonicalPage: (req, res)->
|
2014-06-30 12:08:54 -04:00
|
|
|
{user_id, template_id} = req.params
|
|
|
|
logger.log user_id:user_id, template_id:template_id, "rendering template page"
|
|
|
|
TemplatesWebController._getDataFromTemplatesApi "/user/#{user_id}/template/#{template_id}", (err, data)->
|
2014-06-30 12:21:19 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, user_id:user_id, template_id:template_id, "something went wrong in _renderCanonicalPage"
|
|
|
|
return res.send 500
|
2014-08-08 09:43:07 -04:00
|
|
|
data.title = data?.template?.name
|
2014-06-30 12:08:54 -04:00
|
|
|
data.tag = null
|
|
|
|
res.render "templates/template", data
|
|
|
|
|
2014-06-30 12:21:19 -04:00
|
|
|
_renderAllTemplatesPage: (req, res)->
|
2014-06-30 12:08:54 -04:00
|
|
|
{user_id} = req.params
|
|
|
|
logger.log user_id:user_id, "rendering all templates page"
|
|
|
|
TemplatesWebController._getDataFromTemplatesApi "/user/#{user_id}/all", (err, data)->
|
2014-06-30 12:21:19 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, user_id:user_id, "something went wrong in _renderCanonicalPage"
|
|
|
|
return res.send 500
|
2014-08-01 08:47:14 -04:00
|
|
|
data.title = "all_templates"
|
2014-06-30 12:08:54 -04:00
|
|
|
res.render "templates/tag", data
|
|
|
|
|
2014-06-30 12:21:19 -04:00
|
|
|
_renderTagPage: (req, res)->
|
2014-06-30 12:08:54 -04:00
|
|
|
{user_id, tag_name} = req.params
|
|
|
|
logger.log user_id:user_id, tag_name:tag_name, "rendinging tag page for templates"
|
|
|
|
TemplatesWebController._getDataFromTemplatesApi "/user/#{user_id}/tag/#{tag_name}", (err, data)->
|
2014-06-30 12:21:19 -04:00
|
|
|
if err?
|
|
|
|
logger.err err:err, user_id:user_id, tag_name:tag_name, "something went wrong in _renderCanonicalPage"
|
|
|
|
return res.send 500
|
2014-07-23 07:16:18 -04:00
|
|
|
data.title = data?.tag?.name
|
2014-06-30 12:08:54 -04:00
|
|
|
res.render "templates/tag", data
|
|
|
|
|
2014-06-30 12:21:19 -04:00
|
|
|
_getDataFromTemplatesApi: (path, callback)->
|
2014-06-30 12:08:54 -04:00
|
|
|
opts =
|
|
|
|
url: "#{settings.apis.templates_api.url}#{path}"
|
|
|
|
json:true
|
|
|
|
request.get opts, (err, response, data)->
|
|
|
|
callback err, data
|