fluffy – Diff between revs 6 and 9
?pathlinks?
Rev 6 | Rev 9 | |||
---|---|---|---|---|
Line 130... | Line 130... | |||
130 | } |
130 | } |
|
Line 131... | Line 131... | |||
131 | |
131 | |
|
132 | switch(trimmedPath) { |
132 | switch(trimmedPath) { |
|
133 | case 'add': |
133 | case 'add': |
|
134 | // Write the icon file. |
134 | // Write the icon file. |
|
135 | var iconPath = path.join(root, 'data/' + fields['service-name'] + '.png'); |
135 | var iconPath = path.join(root, 'services/' + fields['service-name'] + '.png'); |
|
136 | var formFile = fs.createReadStream(files['service-icon'].path) |
136 | var formFile = fs.createReadStream(files['service-icon'].path) |
|
137 | .on('open', () => { |
137 | .on('open', () => { |
|
138 | formFile.pipe( |
138 | formFile.pipe( |
|
139 | fs.createWriteStream(iconPath) |
139 | fs.createWriteStream(iconPath) |
|
Line 174... | Line 174... | |||
174 | }); |
174 | }); |
|
175 | return; |
175 | return; |
|
176 | } |
176 | } |
|
177 | }); |
177 | }); |
|
Line 178... | Line -... | |||
178 | |
- | ||
179 | // Update the data file. |
178 | |
|
180 | fs.realpath(path.join(root, 'data/data.json'), (error, dataPath) => { |
- | ||
181 | // If the path does not exist, then return early. |
- | ||
182 | if (error) { |
- | ||
183 | self.emit('log', { |
- | ||
184 | message: 'Unable to access data path: ' + dataPath, |
- | ||
185 | severity: 'error' |
- | ||
186 | }); |
- | ||
187 | self.emit('data', { |
- | ||
188 | status: 404, |
- | ||
189 | data: new stream.Readable({ |
- | ||
190 | read(size) { |
- | ||
191 | this.push(null); |
- | ||
192 | } |
- | ||
193 | }), |
- | ||
194 | type: 'text/plain' |
- | ||
195 | }); |
- | ||
196 | return; |
- | ||
197 | } |
- | ||
198 | |
179 | var templatePath = path.join(root, 'templates/service.html'); |
|
199 | fs.readFile(dataPath, 'utf8', function(error, data) { |
180 | fs.readFile(templatePath, 'utf8', function(error, data) { |
|
200 | // Could not read data file. |
- | ||
201 | if (error) { |
- | ||
202 | self.emit('log', { |
- | ||
203 | message: 'Unable to read data file.', |
- | ||
204 | severity: 'error' |
- | ||
205 | }); |
- | ||
206 | self.emit('data', { |
- | ||
207 | status: 404, |
- | ||
208 | data: new stream.Readable({ |
- | ||
209 | read(size) { |
- | ||
210 | this.push(null); |
- | ||
211 | } |
- | ||
212 | }), |
- | ||
213 | type: 'text/plain' |
- | ||
214 | }); |
- | ||
215 | return; |
- | ||
216 | } |
- | ||
217 | |
- | ||
218 | // Remove previous service if it exists. |
- | ||
219 | var services = JSON.parse(data).filter((service) => { |
- | ||
220 | return service.tooltip != fields['service-name']; |
- | ||
221 | }); |
- | ||
222 | |
- | ||
223 | services.push({ |
- | ||
224 | image: '/data/' + fields['service-name'] + '.png', |
- | ||
225 | width: '50', |
- | ||
226 | height: '50', |
- | ||
227 | url: fields['service-url'], |
- | ||
228 | target: '_top', |
- | ||
229 | tooltip: fields['service-name'] |
- | ||
230 | }); |
- | ||
231 | |
- | ||
232 | fs.writeFile(dataPath, JSON.stringify(services, null, 4), (error) => { |
- | ||
233 | // Could not write data file. |
- | ||
234 | if (error) { |
- | ||
235 | self.emit('log', { |
- | ||
236 | message: 'Unable to write data file.', |
- | ||
237 | severity: 'error' |
- | ||
238 | }); |
- | ||
239 | self.emit('data', { |
- | ||
240 | status: 404, |
- | ||
241 | data: new stream.Readable({ |
- | ||
242 | read(size) { |
- | ||
243 | this.push(null); |
- | ||
244 | } |
- | ||
245 | }), |
- | ||
246 | type: 'text/plain' |
- | ||
247 | }); |
- | ||
248 | return; |
- | ||
249 | } |
- | ||
250 | |
- | ||
251 | // Send the main site index. |
- | ||
252 | self.emit('log', { |
- | ||
253 | message: 'Added new service.', |
- | ||
254 | severity: 'info' |
- | ||
255 | }); |
- | ||
256 | self.emit('data', { |
- | ||
257 | status: 200, |
- | ||
258 | data: fs.createReadStream(path.join(root, config.site.index)), |
- | ||
259 | type: 'text/html' |
- | ||
260 | }); |
- | ||
261 | |
- | ||
262 | }); |
- | ||
263 | |
- | ||
264 | }); |
- | ||
265 | |
- | ||
266 | }); |
- | ||
267 | break; |
- | ||
268 | case 'remove': |
- | ||
269 | fs.realpath(path.join(root, 'data/data.json'), (error, dataPath) => { |
- | ||
270 | // If the path does not exist, then return early. |
181 | // Could not read data file. |
|
271 | if (error) { |
182 | if (error) { |
|
272 | self.emit('log', { |
183 | self.emit('log', { |
|
273 | message: 'Unable to access data path: ' + dataPath, |
184 | message: 'Unable to read data file.', |
|
274 | severity: 'error' |
185 | severity: 'error' |
|
275 | }); |
186 | }); |
|
276 | self.emit('data', { |
187 | self.emit('data', { |
|
277 | status: 404, |
188 | status: 404, |
|
Line 282... | Line 193... | |||
282 | }), |
193 | }), |
|
283 | type: 'text/plain' |
194 | type: 'text/plain' |
|
284 | }); |
195 | }); |
|
285 | return; |
196 | return; |
|
286 | } |
197 | } |
|
287 | |
198 | |
|
- | 199 | data = data.replace(/FLUFFY_SERVICE_NAME/g, fields['service-name']); |
||
- | 200 | data = data.replace(/FLUFFY_SERVICE_URL/g, fields['service-url']) |
||
- | 201 | |
||
- | 202 | var htmlPath = path.join(root, 'services/' + fields['service-name'] + '.html'); |
||
288 | fs.readFile(dataPath, 'utf8', function(error, data) { |
203 | fs.writeFile(htmlPath, data, (error) => { |
|
289 | // Could not read data file. |
204 | // Could not write data file. |
|
290 | if (error) { |
205 | if (error) { |
|
291 | self.emit('log', { |
206 | self.emit('log', { |
|
292 | message: 'Unable to read data file.', |
207 | message: 'Unable to write data file.', |
|
293 | severity: 'error' |
208 | severity: 'error' |
|
294 | }); |
209 | }); |
|
295 | self.emit('data', { |
210 | self.emit('data', { |
|
296 | status: 404, |
211 | status: 404, |
|
297 | data: new stream.Readable({ |
212 | data: new stream.Readable({ |
|
Line 301... | Line 216... | |||
301 | }), |
216 | }), |
|
302 | type: 'text/plain' |
217 | type: 'text/plain' |
|
303 | }); |
218 | }); |
|
304 | return; |
219 | return; |
|
305 | } |
220 | } |
|
306 | |
221 | |
|
307 | // Remove all specified services and their corresponding icon files. |
- | ||
308 | var currentServices = JSON.parse(data); |
- | ||
309 | var services = []; |
- | ||
310 | currentServices.forEach(currentService => { |
- | ||
311 | if([ properfields['remove-services'] ].some((service) => service === currentService.tooltip)) { |
- | ||
312 | var imagePath = path.join(root, currentService.image); |
- | ||
313 | fs.stat(imagePath, (error, stats) => { |
- | ||
314 | // Image file does not exist. |
- | ||
315 | if (error) { |
222 | // Send the main site index. |
|
316 | self.emit('log', { |
223 | self.emit('log', { |
|
317 | message: 'Image file does not exist.', |
- | ||
318 | severity: 'warning' |
- | ||
319 | }); |
- | ||
320 | self.emit('data', { |
- | ||
321 | status: 404, |
- | ||
322 | data: new stream.Readable({ |
- | ||
323 | read(size) { |
224 | message: 'Added new service.', |
|
324 | this.push(null); |
- | ||
325 | } |
- | ||
326 | }), |
- | ||
327 | type: 'text/plain' |
- | ||
328 | }); |
- | ||
329 | return; |
- | ||
330 | } |
- | ||
331 | fs.unlink(imagePath, (error) => { |
- | ||
332 | if(error) { |
- | ||
333 | self.emit('log', { |
- | ||
334 | message: 'Could not remove image file.', |
- | ||
335 | severity: 'warning' |
- | ||
336 | }); |
- | ||
337 | self.emit('data', { |
- | ||
338 | status: 404, |
- | ||
339 | data: new stream.Readable({ |
- | ||
340 | read(size) { |
- | ||
341 | this.push(null); |
- | ||
342 | } |
- | ||
343 | }), |
- | ||
344 | type: 'text/plain' |
- | ||
345 | }); |
- | ||
346 | } |
- | ||
347 | }); |
- | ||
348 | }); |
- | ||
349 | return; |
225 | severity: 'info' |
|
350 | } |
- | ||
351 | services.push(currentService); |
- | ||
352 | }); |
226 | }); |
|
- | 227 | self.emit('data', { |
||
- | 228 | status: 200, |
||
- | 229 | data: fs.createReadStream(path.join(root, config.site.index)), |
||
- | 230 | type: 'text/html' |
||
- | 231 | }); |
||
- | 232 | |
||
- | 233 | }); |
||
353 | |
234 | |
|
- | 235 | }); |
||
- | 236 | break; |
||
- | 237 | case 'remove': |
||
- | 238 | [ properfields['remove-services'] ].forEach((service) => { |
||
354 | // Write the data file back. |
239 | var service_assets = [ |
|
- | 240 | path.join(root, 'services/' + service + '.html'), |
||
- | 241 | path.join(root, 'services/' + service + '.png') |
||
- | 242 | ] |
||
- | 243 | service_assets.forEach((asset) => { |
||
355 | fs.writeFile(dataPath, JSON.stringify(services, null, 4), (error) => { |
244 | fs.realpath(asset, (error, dataPath) => { |
|
356 | // Could not write data file. |
245 | // If the path does not exist, then return early. |
|
357 | if (error) { |
246 | if (error) { |
|
358 | self.emit('log', { |
247 | self.emit('log', { |
|
359 | message: 'Unable to write data file.', |
248 | message: 'Unable to access data path: ' + dataPath + ' error: ' + error, |
|
360 | severity: 'error' |
249 | severity: 'error' |
|
361 | }); |
250 | }); |
|
362 | self.emit('data', { |
251 | self.emit('data', { |
|
363 | status: 404, |
252 | status: 404, |
|
364 | data: new stream.Readable({ |
253 | data: new stream.Readable({ |
|
Line 368... | Line 257... | |||
368 | }), |
257 | }), |
|
369 | type: 'text/plain' |
258 | type: 'text/plain' |
|
370 | }); |
259 | }); |
|
371 | return; |
260 | return; |
|
372 | } |
261 | } |
|
373 | |
262 | |
|
- | 263 | fs.unlink(dataPath, (error) => { |
||
374 | // Send the main site index. |
264 | if(error) { |
|
375 | self.emit('log', { |
265 | self.emit('log', { |
|
376 | message: 'Removed services.', |
266 | message: 'Could not remove image file.', |
|
377 | severity: 'info' |
267 | severity: 'warning' |
|
378 | }); |
268 | }); |
|
379 | self.emit('data', { |
269 | self.emit('data', { |
|
380 | status: 200, |
270 | status: 404, |
|
381 | data: fs.createReadStream(path.join(root, config.site.index)), |
271 | data: new stream.Readable({ |
|
- | 272 | read(size) { |
||
- | 273 | this.push(null); |
||
- | 274 | } |
||
- | 275 | }), |
||
382 | type: 'text/html' |
276 | type: 'text/plain' |
|
- | 277 | }); |
||
- | 278 | } |
||
383 | }); |
279 | }); |
|
384 | |
- | ||
385 | }); |
280 | }); |
|
386 | |
- | ||
387 | }); |
281 | }); |
|
388 | }); |
282 | }); |
|
- | 283 | |
||
- | 284 | // Send the main site index. |
||
- | 285 | self.emit('log', { |
||
- | 286 | message: 'Removed services.', |
||
- | 287 | severity: 'info' |
||
- | 288 | }); |
||
- | 289 | self.emit('data', { |
||
- | 290 | status: 200, |
||
- | 291 | data: fs.createReadStream(path.join(root, config.site.index)), |
||
- | 292 | type: 'text/html' |
||
- | 293 | }); |
||
389 | break; |
294 | break; |
|
390 | default: |
295 | default: |
|
391 | self.emit('log', { |
296 | self.emit('log', { |
|
392 | message: 'No such path.', |
297 | message: 'No such path.', |
|
393 | severity: 'error' |
298 | severity: 'error' |