Completed
Push — master ( e073c7...e389ae )
by greg
01:55
created

get-main.js ➔ ... ➔ req.headers.cookie.forEach   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
nop 1
1
import xss from 'xss'
2
import pkg from '../../../package'
3
4
import {
5
  config,
6
  Page,
7
  cmsData,
8
  cmsTemplates,
9
  coreUtils,
10
  abeExtend,
11
  Manager
12
} from '../../cli'
13
14
import {editor} from '../controllers/editor'
15
import locale from '../helpers/abe-locale'
16
17
var route = function(req, res, next) {
18
  var filePath = req.originalUrl.replace('/abe', '')
19
  if (filePath === '' || filePath === '/') {
20
    filePath = null
21
  }
22
23
  if(filePath != null){
24
    var testXSS = xss(filePath, {
25
      whiteList: [],
26
      stripIgnoreTag: true
27
    })
28
    if(testXSS !== filePath){
29
      filePath = testXSS
30
    }
31
  }
32
33
  abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
34
  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...
35
36
  var isHome = true
37
  var jsonPath = null
38
  var linkPath = null
39
  var template = null
40
  var fileName = null
41
  var folderPath = null
42
43
  let p = new Promise((resolve) => {
44
45
    if(filePath != null) {
46
      fileName = filePath.split('/')
47
      fileName = fileName[fileName.length-1].replace(`.${config.files.templates.extension}`, '')
48
49
      folderPath = filePath.split('/')
50
      folderPath.pop()
51
      folderPath = folderPath.join('/')
52
53
      isHome = false
54
55
      var filePathTest = cmsData.revision.getDocumentRevision(filePath)
56
      if(typeof filePathTest !== 'undefined' && filePathTest !== null) {
57
        jsonPath = filePathTest.path
58
        linkPath = filePathTest.abe_meta.link
59
        template = filePathTest.abe_meta.template
60
      }
61
62
      if(jsonPath === null || !coreUtils.file.exist(jsonPath)) { 
63
        res.redirect('/abe/') 
64
        return 
65
      }
66
67
      editor(template, jsonPath, linkPath)
68
        .then((result) => {
69
          resolve(result)
70
        }).catch(function(e) {
71
          console.error(e)
72
        })
73
    }else {
74
      resolve({
75
        obj: {},
76
        manager: {}
77
      })
78
    }
79
  }).catch(function(e) {
80
    console.error(e) // "oh, no!"
81
  })
82
83
  p.then((result) => {
84
    var obj = result
85
    var manager = {}
86
  
87
    manager.home = {
88
      files: Manager.instance.getList()
89
    }
90
91
    manager.list = Manager.instance.getStructureAndTemplates()
92
    manager.editConfig = req.app.get('config')
93
    manager.config = JSON.stringify(config)
94
    
95
    var _hasBlock = (obj) ? obj.hasBlock : false
96
    var _hasSingleBlock = (obj) ? obj.hasSingleBlock : false
97
    var _preview = (filePath) ? '/abe/page/' + req.params[0] + `?filePath=${req.query.filePath}` : false
98
    var _form = (obj) ? obj.form : false
99
    var _json = (obj) ? obj.json : false
100
    var _text = (obj) ? obj.text : false
0 ignored issues
show
Unused Code introduced by
The variable _text seems to be never used. Consider removing it.
Loading history...
101
    // var _file = (tplUrl) ? tplUrl.draft.file : false
102
    var _filePath = (filePath) ? filePath : false
103
    if (_filePath) {
104
      _filePath = '/' + _filePath.replace(/^\/+/, '')
0 ignored issues
show
Unused Code introduced by
The assignment to variable _filePath seems to be never used. Consider removing it.
Loading history...
105
    }
106
107
    var pageHtml = ''
108
    if(typeof _json !== 'undefined' && _json !== null 
109
      && typeof _json.abe_meta !== 'undefined' && _json.abe_meta !== null) {
110
111
      var text = cmsTemplates.template.getTemplate(_json.abe_meta.template) 
112
      var page = new Page(_json.abe_meta.template, text, _json, false) 
113
      pageHtml = page.html.replace(/"/g, '"').replace(/'/g, '\'').replace(/<!--/g, '<ABE!--').replace(/-->/g, '--ABE>')
114
    }
115
    
116
    var editorWidth = '33%'
117
    req.headers && req.headers.cookie.split(';').forEach(function(cookie) {
118
      var parts = cookie.match(/(.*?)=(.*)$/)
119
      if(parts[1] === 'editorWidth') editorWidth = parts[2]
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...
120
    })
121
    var EditorVariables = {
122
      pageHtml: pageHtml,
123
      isHome: isHome,
124
      abeUrl: '/abe/',
125
      test: JSON.stringify(locale),
126
      text: locale,
127
      preview: _preview,
128
      filename: fileName,
129
      folderPath: folderPath,
130
      hasSingleBlock: _hasSingleBlock,
131
      hasBlock: _hasBlock,
132
      form: _form,
133
      json: _json,
134
      config: config,
135
      Locales: coreUtils.locales.instance.i18n,
136
      manager: manager,
137
      express: {
138
        res: res,
139
        req: req
140
      },
141
      abeVersion: pkg.version,
142
      nonce: '\'nonce-' + res.locals.nonce + '\'',
143
      editorWidth: editorWidth
144
    }
145
    EditorVariables = abeExtend.hooks.instance.trigger('afterVariables', EditorVariables)
146
147
    if (filePath != null && filePath.indexOf('.json') > -1) {
148
      res.set('Content-Type', 'application/json')
149
      res.send(JSON.stringify(_json))
150
    }else {
151
      res.render(config.abeEngine, EditorVariables)
152
    }
153
  }).catch((e) => {
154
    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...
155
  })
156
}
157
158
export default route