-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
org.quickcorp.qcobjects.main.file.js
119 lines (114 loc) · 4.65 KB
/
org.quickcorp.qcobjects.main.file.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* QCObjects CLI 0.0.6
* ________________
*
* Author: Jean Machuca <correojean@gmail.com>
*
* Cross Browser Javascript Framework for MVC Patterns
* QuickCorp/QCObjects is licensed under the
* GNU Lesser General Public License v3.0
* [LICENSE] (https://github.com/QuickCorp/QCObjects/blob/master/LICENSE.txt)
*
* Permissions of this copyleft license are conditioned on making available
* complete source code of licensed works and modifications under the same
* license or the GNU GPLv3. Copyright and license notices must be preserved.
* Contributors provide an express grant of patent rights. However, a larger
* work using the licensed work through interfaces provided by the licensed
* work may be distributed under different terms and without source code for
* the larger work.
*
* Copyright (C) 2015 Jean Machuca,<correojean@gmail.com>
*
* Everyone is permitted to copy and distribute verbatim copies of this
* license document, but changing it is not allowed.
*/
"use strict";
const path = require('path');
const fs = require('fs');
const mime = require("mime");
const absolutePath = path.resolve( __dirname, "./" );
const Handlebars = require('handlebars');
Package('org.quickcorp.qcobjects.main.file',[
Class('FileDispatcher',{
name:CONFIG.get('documentRootFileIndex'),
template:'',
templateURI:CONFIG.get('documentRootFileIndex'),
headers:{},
body:'',
filename:'',
file_extension:function (){
return this.filename.substr(this.filename.indexOf("."));
},
isTemplate:function (){
return CONFIG.get('useTemplate') && (this.file_extension()=='.html' || this.file_extension() == '.tpl.html');
},
_done:function (){
var appTemplateInstance = this;
const source = appTemplateInstance.template;
if (appTemplateInstance.isTemplate()){
const template = Handlebars.compile(source);
appTemplateInstance.body = template({title: 'QCObjects'});
} else {
appTemplateInstance.body = source;
}
if (['.png',
'.jpg',
'.jpeg',
'.json',
'.html',
'.tpl.html',
'.css',
'.js',
'.svg'].includes(appTemplateInstance.file_extension())){
appTemplateInstance.headers['content-type']=mime.getType(appTemplateInstance.templateURI);
appTemplateInstance.done.call(appTemplateInstance,
appTemplateInstance.headers,
appTemplateInstance.body,
appTemplateInstance.templateURI,
appTemplateInstance.isTemplate());
} else {
appTemplateInstance.done.call(appTemplateInstance,
{
':status': 403,
'content-type': 'text/plain'
},
'FORBIDDEN','notfound.html',false);
}
},
done:function (headers,body){},
_new_:function (o){
var scriptname = o.scriptname;
this.filename = scriptname;
var pathname = (o.pathname !== '')?(o.pathname+'/'):('');
var appTemplateInstance = this;
appTemplateInstance.done = o.done;
appTemplateInstance.templateURI = CONFIG.get('documentRoot')+pathname+scriptname;
appTemplateInstance.templateURI = appTemplateInstance.templateURI.replace('//','/');
if (appTemplateInstance.isTemplate()){
fs.readFile(appTemplateInstance.templateURI, function(err, data) {
logger.debug('reading data from '+appTemplateInstance.templateURI);
if (typeof data !== 'undefined'){
appTemplateInstance.template = data.toString();
appTemplateInstance._done.call(appTemplateInstance);
} else {
appTemplateInstance.headers = {
':status': 404,
'content-type': 'text/html'
};
appTemplateInstance.done.call(appTemplateInstance,
appTemplateInstance.headers,
'FILE NOT FOUND','notfound.html',false);
logger.debug('file not found');
}
});
} else {
appTemplateInstance.headers[':status']=200;
appTemplateInstance.headers['content-type']=mime.getType(appTemplateInstance.templateURI);
appTemplateInstance.done.call(appTemplateInstance,
appTemplateInstance.headers,
'',appTemplateInstance.templateURI,false);
}
logger.info('FileDispatcher initialized');
}
})
]);