Completed
Push — master ( 1fc2c9...675e71 )
by greg
01:46
created

get-main.js ➔ ... ➔ ???   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 3
c 2
b 1
f 0
nc 4
dl 0
loc 23
rs 9.0856
nop 1
1
import xss from 'xss'
2
import path from 'path'
3
import pkg from '../../../package'
4
5
import {
6
  FileParser,
7
  config,
8
  Page,
9
  cmsData,
10
  cmsTemplates,
11
  coreUtils,
12
  Hooks,
13
  Manager
14
} from '../../cli'
15
16
import {editor} from '../controllers/editor'
17
import locale from '../helpers/abe-locale'
18
19
var route = function(req, res, next) {
20
  if(req.query.filePath){
21
    var testXSS = xss(req.query.filePath, {
22
      whiteList: [],
23
      stripIgnoreTag: true
24
    })
25
    if(testXSS !== req.query.filePath){
26
      res.redirect(`/abe/${req.params[0]}?filePath=${testXSS}`)
27
      return
28
    }
29
  }
30
  Hooks.instance.trigger('beforeRoute', req, res, next)
31
  if(typeof res._header !== 'undefined' && res._header !== null) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
32
33
  var templatePath = req.params[0]
34
35
  var filePath = null
36
  if(typeof req.query.filePath !== 'undefined' && req.query.filePath !== null) {
37
    filePath = path.join(config.root, config.draft.url, req.query.filePath.replace(config.root))
38
  }
39
  var debugJson = (req.query.debugJson && req.query.debugJson == 'true' ) ? true : false
40
  var debugJsonKey = (req.query.key) ? req.query.key : false
41
  var debugHtml = (req.query.debugHtml && req.query.debugHtml == 'true' ) ? true : false
42
43
  var isHome = true
44
45
  let p = new Promise((resolve) => {
46
47
    if(templatePath !== null && filePath !== null) {
48
      var jsonPath = null
49
      var linkPath = null
50
      isHome = false
51
52
      var filePathTest = cmsData.revision.getDocumentRevision(req.query.filePath)
53
      if(typeof filePathTest !== 'undefined' && filePathTest !== null) {
54
        // filePath = filePathTest.path
55
        jsonPath = filePathTest.path
56
        linkPath = filePathTest.abe_meta.link
57
      }
58
59
      if(jsonPath === null || !coreUtils.file.exist(jsonPath)) { 
60
        res.redirect('/abe/') 
61
        return 
62
      } 
63
64
      editor(templatePath, jsonPath, linkPath)
65
        .then((result) => {
66
          var manager = {}
67
          var revisionFilePath = FileParser.changePathEnv(filePath, config.draft.url)
68
          var dirPath = path.dirname(revisionFilePath)
69
          var allDraft = FileParser.getFiles(dirPath, true, 99, new RegExp('\\.' + config.files.templates.extension))
70
71
          allDraft = cmsData.metas.get(allDraft, 'draft')
72
          var breadcrumb = req.params[0].split('/')
73
          manager.file = {
74
            revision: cmsData.revision.getFilesRevision(allDraft, cmsData.fileAttr.delete(revisionFilePath))
75
            ,template: breadcrumb
76
            ,path: (req.query.filePath) ? path.resolve(req.query.filePath).replace(/^\//, '') : ''
77
          }
78
          if(manager.file.revision.length > 0){
79
            var publishPath = cmsData.fileAttr.delete(manager.file.revision[0].path.replace(new RegExp(`/${config.draft.url}/`), `/${config.publish.url}/`))
80
            manager.file.isPublished = coreUtils.file.exist(publishPath)
81
          }
82
83
          resolve({
84
            obj: result,
85
            manager: manager
86
          })
87
        }).catch(function(e) {
88
          console.error(e)
89
        })
90
    }else {
91
      resolve({
92
        obj: {},
93
        manager: {}
94
      })
95
    }
96
  }).catch(function(e) {
97
    console.error(e) // "oh, no!"
98
  })
99
100
  p.then((result) => {
101
    var obj = result.obj
102
    var manager = result.manager
103
    // let tplUrl = result.tplUrl
104
  
105
    manager.home = {
106
      files: Manager.instance.getList()
107
    }
108
109
    manager.list = req.app.get('projectFiles')
110
    manager.editConfig = req.app.get('config')
111
    manager.config = JSON.stringify(config)
112
    
113
    var _hasBlock = (obj) ? obj.hasBlock : false
114
    var _hasSingleBlock = (obj) ? obj.hasSingleBlock : false
115
    var _template = (filePath) ? '/page/' + req.params[0] + `?filePath=${req.query.filePath}` : false
116
    var _form = (obj) ? obj.form : false
117
    var _json = (obj) ? obj.json : false
118
    var _text = (obj) ? obj.text : false
119
    // var _file = (tplUrl) ? tplUrl.draft.file : false
120
    var _filePath = (req.query.filePath) ? req.query.filePath : false
121
    if (_filePath) {
122
      _filePath = '/' + _filePath.replace(/^\/+/, '')
123
    }
124
125
    var pageHtml = '' 
126
    if(typeof _json !== 'undefined' && _json !== null 
127
      && typeof _json.abe_meta !== 'undefined' && _json.abe_meta !== null) { 
128
      var text = cmsTemplates.template.getTemplate(_json.abe_meta.template) 
129
      var page = new Page(_json.abe_meta.template, text, _json, false) 
130
      pageHtml = page.html.replace(/"/g, '"').replace(/'/g, '\'').replace(/<!--/g, '<ABE!--').replace(/-->/g, '--ABE>')
131
    } 
132
133
    var EditorVariables = {
134
      pageHtml: pageHtml,
135
      isHome: isHome,
136
      abeUrl: '/abe/',
137
      test: JSON.stringify(locale),
138
      text: locale,
139
      templatePath: req.params[0],
140
      template: _template,
141
      hasSingleBlock: _hasSingleBlock,
142
      hasBlock: _hasBlock,
143
      form: _form,
144
      urlToSaveFile: _filePath,
145
      // tplName: _file,
146
      json: _json,
147
      config: config,
148
      Locales: coreUtils.locales.instance.i18n,
149
      manager: manager,
150
      express: {
151
        res: res,
152
        req: req
153
      },
154
      abeVersion: pkg.version,
155
      nonce: '\'nonce-' + res.locals.nonce + '\''
156
    }
157
    EditorVariables = Hooks.instance.trigger('afterVariables', EditorVariables)
158
159
    if (debugJson) {
160
      var dj = _json
161
      if(debugJsonKey && typeof dj[debugJsonKey] !== 'undefined' && dj[debugJsonKey] !== null) {
162
        dj = dj[debugJsonKey]
163
      }
164
      res.set('Content-Type', 'application/json')
165
      res.send(JSON.stringify(dj))
166
    }else if (debugHtml) {
167
      res.set('Content-Type', 'text/plain')
168
      res.send(_text)
169
    }else {
170
      res.render(config.abeEngine, EditorVariables)
171
    }
172
  }).catch((e) => {
173
    console.log('error', e)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
174
  })
175
}
176
177
export default route