|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | **/ |
| 16 | + |
| 17 | +var apiUtils = require("../util"); |
16 | 18 | var fs = require('fs'); |
17 | 19 | var fspath = require('path'); |
18 | 20 | var when = require('when'); |
19 | 21 |
|
20 | | -var redApp = null; |
21 | | -var storage; |
22 | | -var log; |
23 | | -var redNodes; |
24 | | -var needsPermission = require("../auth").needsPermission; |
25 | | - |
26 | | -function createLibrary(type) { |
27 | | - if (redApp) { |
28 | | - redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),needsPermission("library.read"),function(req,res) { |
29 | | - var path = req.params[1]||""; |
30 | | - storage.getLibraryEntry(type,path).then(function(result) { |
31 | | - log.audit({event: "library.get",type:type},req); |
32 | | - if (typeof result === "string") { |
33 | | - res.writeHead(200, {'Content-Type': 'text/plain'}); |
34 | | - res.write(result); |
35 | | - res.end(); |
36 | | - } else { |
37 | | - res.json(result); |
38 | | - } |
39 | | - }).catch(function(err) { |
40 | | - if (err) { |
41 | | - log.warn(log._("api.library.error-load-entry",{path:path,message:err.toString()})); |
42 | | - if (err.code === 'forbidden') { |
43 | | - log.audit({event: "library.get",type:type,error:"forbidden"},req); |
44 | | - res.status(403).end(); |
45 | | - return; |
46 | | - } |
47 | | - } |
48 | | - log.audit({event: "library.get",type:type,error:"not_found"},req); |
49 | | - res.status(404).end(); |
50 | | - }); |
51 | | - }); |
52 | | - |
53 | | - redApp.post(new RegExp("/library/"+type+"\/(.*)"),needsPermission("library.write"),function(req,res) { |
54 | | - var path = req.params[0]; |
55 | | - var meta = req.body; |
56 | | - var text = meta.text; |
57 | | - delete meta.text; |
58 | | - |
59 | | - storage.saveLibraryEntry(type,path,meta,text).then(function() { |
60 | | - log.audit({event: "library.set",type:type},req); |
61 | | - res.status(204).end(); |
62 | | - }).catch(function(err) { |
63 | | - log.warn(log._("api.library.error-save-entry",{path:path,message:err.toString()})); |
64 | | - if (err.code === 'forbidden') { |
65 | | - log.audit({event: "library.set",type:type,error:"forbidden"},req); |
66 | | - res.status(403).end(); |
67 | | - return; |
68 | | - } |
69 | | - log.audit({event: "library.set",type:type,error:"unexpected_error",message:err.toString()},req); |
70 | | - res.status(500).json({error:"unexpected_error", message:err.toString()}); |
71 | | - }); |
72 | | - }); |
73 | | - } |
74 | | -} |
| 22 | +var runtimeAPI; |
75 | 23 |
|
76 | 24 | module.exports = { |
77 | | - init: function(app,runtime) { |
78 | | - redApp = app; |
79 | | - log = runtime.log; |
80 | | - storage = runtime.storage; |
81 | | - redNodes = runtime.nodes; |
| 25 | + init: function(app,_runtimeAPI) { |
| 26 | + runtimeAPI = _runtimeAPI; |
82 | 27 | }, |
83 | | - register: createLibrary, |
84 | 28 |
|
85 | 29 | getAll: function(req,res) { |
86 | | - storage.getAllFlows().then(function(flows) { |
87 | | - log.audit({event: "library.get.all",type:"flow"},req); |
88 | | - var examples = redNodes.getNodeExampleFlows(); |
89 | | - if (examples) { |
90 | | - flows.d = flows.d||{}; |
91 | | - flows.d._examples_ = redNodes.getNodeExampleFlows(); |
92 | | - } |
93 | | - res.json(flows); |
| 30 | + var opts = { |
| 31 | + user: req.user, |
| 32 | + type: 'flows' |
| 33 | + } |
| 34 | + runtimeAPI.library.getEntries(opts).then(function(result) { |
| 35 | + res.json(result); |
| 36 | + }).catch(function(err) { |
| 37 | + apiUtils.rejectHandler(req,res,err); |
94 | 38 | }); |
95 | 39 | }, |
96 | | - get: function(req,res) { |
97 | | - if (req.params[0].indexOf("_examples_/") === 0) { |
98 | | - var m = /^_examples_\/(@.*?\/[^\/]+|[^\/]+)\/(.*)$/.exec(req.params[0]); |
99 | | - if (m) { |
100 | | - var module = m[1]; |
101 | | - var path = m[2]; |
102 | | - var fullPath = redNodes.getNodeExampleFlowPath(module,path); |
103 | | - if (fullPath) { |
104 | | - try { |
105 | | - fs.statSync(fullPath); |
106 | | - log.audit({event: "library.get",type:"flow",path:req.params[0]},req); |
107 | | - return res.sendFile(fullPath,{ |
108 | | - headers:{ |
109 | | - 'Content-Type': 'application/json' |
110 | | - } |
111 | | - }) |
112 | | - } catch(err) { |
113 | | - console.log(err); |
114 | | - } |
| 40 | + getEntry: function(req,res) { |
| 41 | + var opts = { |
| 42 | + user: req.user, |
| 43 | + type: req.params[0], |
| 44 | + path: req.params[1]||"" |
| 45 | + } |
| 46 | + runtimeAPI.library.getEntry(opts).then(function(result) { |
| 47 | + if (typeof result === "string") { |
| 48 | + if (opts.type === 'flows') { |
| 49 | + res.writeHead(200, {'Content-Type': 'application/json'}); |
| 50 | + } else { |
| 51 | + res.writeHead(200, {'Content-Type': 'text/plain'}); |
115 | 52 | } |
| 53 | + res.write(result); |
| 54 | + res.end(); |
| 55 | + } else { |
| 56 | + res.json(result); |
116 | 57 | } |
117 | | - // IF we get here, we didn't find the file |
118 | | - log.audit({event: "library.get",type:"flow",path:req.params[0],error:"not_found"},req); |
119 | | - return res.status(404).end(); |
| 58 | + }).catch(function(err) { |
| 59 | + apiUtils.rejectHandler(req,res,err); |
| 60 | + }); |
| 61 | + }, |
| 62 | + saveEntry: function(req,res) { |
| 63 | + var opts = { |
| 64 | + user: req.user, |
| 65 | + type: req.params[0], |
| 66 | + path: req.params[1]||"" |
| 67 | + } |
| 68 | + // TODO: horrible inconsistencies between flows and all other types |
| 69 | + if (opts.type === "flows") { |
| 70 | + opts.meta = {}; |
| 71 | + opts.body = JSON.stringify(req.body); |
120 | 72 | } else { |
121 | | - storage.getFlow(req.params[0]).then(function(data) { |
122 | | - // data is already a JSON string |
123 | | - log.audit({event: "library.get",type:"flow",path:req.params[0]},req); |
124 | | - res.set('Content-Type', 'application/json'); |
125 | | - res.send(data); |
126 | | - }).catch(function(err) { |
127 | | - if (err) { |
128 | | - log.warn(log._("api.library.error-load-flow",{path:req.params[0],message:err.toString()})); |
129 | | - if (err.code === 'forbidden') { |
130 | | - log.audit({event: "library.get",type:"flow",path:req.params[0],error:"forbidden"},req); |
131 | | - res.status(403).end(); |
132 | | - return; |
133 | | - } |
134 | | - } |
135 | | - log.audit({event: "library.get",type:"flow",path:req.params[0],error:"not_found"},req); |
136 | | - res.status(404).end(); |
137 | | - }); |
| 73 | + opts.meta = req.body; |
| 74 | + opts.body = opts.meta.text; |
| 75 | + delete opts.meta.text; |
138 | 76 | } |
139 | | - }, |
140 | | - post: function(req,res) { |
141 | | - // if (req.params[0].indexOf("_examples_/") === 0) { |
142 | | - // log.warn(log._("api.library.error-save-flow",{path:req.params[0],message:"forbidden"})); |
143 | | - // log.audit({event: "library.set",type:"flow",path:req.params[0],error:"forbidden"},req); |
144 | | - // return res.status(403).send({error:"unexpected_error", message:"forbidden"}); |
145 | | - // } |
146 | | - var flow = JSON.stringify(req.body); |
147 | | - storage.saveFlow(req.params[0],flow).then(function() { |
148 | | - log.audit({event: "library.set",type:"flow",path:req.params[0]},req); |
| 77 | + runtimeAPI.library.saveEntry(opts).then(function(result) { |
149 | 78 | res.status(204).end(); |
150 | 79 | }).catch(function(err) { |
151 | | - log.warn(log._("api.library.error-save-flow",{path:req.params[0],message:err.toString()})); |
152 | | - if (err.code === 'forbidden') { |
153 | | - log.audit({event: "library.set",type:"flow",path:req.params[0],error:"forbidden"},req); |
154 | | - res.status(403).end(); |
155 | | - return; |
156 | | - } |
157 | | - log.audit({event: "library.set",type:"flow",path:req.params[0],error:"unexpected_error",message:err.toString()},req); |
158 | | - res.status(500).send({error:"unexpected_error", message:err.toString()}); |
| 80 | + apiUtils.rejectHandler(req,res,err); |
159 | 81 | }); |
160 | 82 | } |
161 | 83 | } |
0 commit comments