1 | /*
|
2 |
|
3 | ----------------------------------------------------------------------------
|
4 | | qewd: Quick and Easy Web Development |
|
5 | | |
|
6 | | Copyright (c) 2017 M/Gateway Developments Ltd, |
|
7 | | Reigate, Surrey UK. |
|
8 | | All rights reserved. |
|
9 | | |
|
10 | | http://www.mgateway.com |
|
11 | | Email: rtweed@mgateway.com |
|
12 | | |
|
13 | | |
|
14 | | Licensed under the Apache License, Version 2.0 (the "License"); |
|
15 | | you may not use this file except in compliance with the License. |
|
16 | | You may obtain a copy of the License at |
|
17 | | |
|
18 | | http://www.apache.org/licenses/LICENSE-2.0 |
|
19 | | |
|
20 | | Unless required by applicable law or agreed to in writing, software |
|
21 | | distributed under the License is distributed on an "AS IS" BASIS, |
|
22 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
23 | | See the License for the specific language governing permissions and |
|
24 | | limitations under the License. |
|
25 | ----------------------------------------------------------------------------
|
26 |
|
27 | 3 January 2017
|
28 |
|
29 | */
|
30 |
|
31 | var path = require('path');
|
32 |
|
33 | var servicePaths = {};
|
34 |
|
35 | module.exports = function(messageObj, application, finished) {
|
36 | var fs = require('fs');
|
37 | var fragmentName = messageObj.params.file;
|
38 | var targetId = messageObj.params.targetId;
|
39 |
|
40 | var wsRootPath = this.userDefined.config.webServerRootPath;
|
41 |
|
42 | var fragPath = wsRootPath + application + path.sep + fragmentName;
|
43 | var service = messageObj.service;
|
44 | if (service && service !== '') {
|
45 | var servicesAllowed = this.servicesAllowed[application];
|
46 | if (servicesAllowed && servicesAllowed[service]) {
|
47 | if (!servicePaths[service]) {
|
48 | var dirname = path.dirname(require.resolve(service));
|
49 | servicePaths[service] = dirname;
|
50 | }
|
51 | fragPath = servicePaths[service] + path.sep + 'fragments' + path.sep + fragmentName;
|
52 | }
|
53 | else {
|
54 | finished({error: service + ' service is not permitted for the ' + application + ' application'})
|
55 | return;
|
56 | }
|
57 | }
|
58 | if (messageObj.params.isServiceFragment) {
|
59 | fragPath = wsRootPath + 'services/' + fragmentName;
|
60 | }
|
61 |
|
62 | //console.log('*** ewd-fragment: fragPath = ' + fragPath);
|
63 |
|
64 | fs.exists(fragPath, function(exists) {
|
65 | if (exists) {
|
66 | fs.readFile(fragPath, 'utf8', function(err, data) {
|
67 | if (!err) {
|
68 | finished({
|
69 | fragmentName: fragmentName,
|
70 | content: data
|
71 | });
|
72 | }
|
73 | else {
|
74 | finished({error: 'Unable to read file ' + fragPath});
|
75 | }
|
76 | });
|
77 | }
|
78 | else {
|
79 | var message = {
|
80 | error: 'Fragment file ' + messageObj.params.file + ' does not exist',
|
81 | file: messageObj.params.file
|
82 | };
|
83 | if (messageObj.service) {
|
84 | message.service = messageObj.service;
|
85 | }
|
86 | if (messageObj.params.isServiceFragment) {
|
87 | message.isServiceFragment = true;
|
88 | }
|
89 | finished(message);
|
90 | }
|
91 | });
|
92 | };
|