fluffy

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 6  →  ?path2? @ 9
/src/methods/post.js
@@ -132,7 +132,7 @@
switch(trimmedPath) {
case 'add':
// Write the icon file.
var iconPath = path.join(root, 'data/' + fields['service-name'] + '.png');
var iconPath = path.join(root, 'services/' + fields['service-name'] + '.png');
var formFile = fs.createReadStream(files['service-icon'].path)
.on('open', () => {
formFile.pipe(
@@ -176,101 +176,12 @@
}
});
// Update the data file.
fs.realpath(path.join(root, 'data/data.json'), (error, dataPath) => {
// If the path does not exist, then return early.
if (error) {
self.emit('log', {
message: 'Unable to access data path: ' + dataPath,
severity: 'error'
});
self.emit('data', {
status: 404,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
}
fs.readFile(dataPath, 'utf8', function(error, data) {
// Could not read data file.
if (error) {
self.emit('log', {
message: 'Unable to read data file.',
severity: 'error'
});
self.emit('data', {
status: 404,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
}
// Remove previous service if it exists.
var services = JSON.parse(data).filter((service) => {
return service.tooltip != fields['service-name'];
});
services.push({
image: '/data/' + fields['service-name'] + '.png',
width: '50',
height: '50',
url: fields['service-url'],
target: '_top',
tooltip: fields['service-name']
});
fs.writeFile(dataPath, JSON.stringify(services, null, 4), (error) => {
// Could not write data file.
if (error) {
self.emit('log', {
message: 'Unable to write data file.',
severity: 'error'
});
self.emit('data', {
status: 404,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
}
// Send the main site index.
self.emit('log', {
message: 'Added new service.',
severity: 'info'
});
self.emit('data', {
status: 200,
data: fs.createReadStream(path.join(root, config.site.index)),
type: 'text/html'
});
});
});
 
});
break;
case 'remove':
fs.realpath(path.join(root, 'data/data.json'), (error, dataPath) => {
// If the path does not exist, then return early.
var templatePath = path.join(root, 'templates/service.html');
fs.readFile(templatePath, 'utf8', function(error, data) {
// Could not read data file.
if (error) {
self.emit('log', {
message: 'Unable to access data path: ' + dataPath,
message: 'Unable to read data file.',
severity: 'error'
});
self.emit('data', {
@@ -284,12 +195,16 @@
});
return;
}
fs.readFile(dataPath, 'utf8', function(error, data) {
// Could not read data file.
data = data.replace(/FLUFFY_SERVICE_NAME/g, fields['service-name']);
data = data.replace(/FLUFFY_SERVICE_URL/g, fields['service-url'])
 
var htmlPath = path.join(root, 'services/' + fields['service-name'] + '.html');
fs.writeFile(htmlPath, data, (error) => {
// Could not write data file.
if (error) {
self.emit('log', {
message: 'Unable to read data file.',
message: 'Unable to write data file.',
severity: 'error'
});
self.emit('data', {
@@ -303,60 +218,34 @@
});
return;
}
// Remove all specified services and their corresponding icon files.
var currentServices = JSON.parse(data);
var services = [];
currentServices.forEach(currentService => {
if([ properfields['remove-services'] ].some((service) => service === currentService.tooltip)) {
var imagePath = path.join(root, currentService.image);
fs.stat(imagePath, (error, stats) => {
// Image file does not exist.
if (error) {
self.emit('log', {
message: 'Image file does not exist.',
severity: 'warning'
});
self.emit('data', {
status: 404,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
return;
}
fs.unlink(imagePath, (error) => {
if(error) {
self.emit('log', {
message: 'Could not remove image file.',
severity: 'warning'
});
self.emit('data', {
status: 404,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
}
});
});
return;
}
services.push(currentService);
 
// Send the main site index.
self.emit('log', {
message: 'Added new service.',
severity: 'info'
});
// Write the data file back.
fs.writeFile(dataPath, JSON.stringify(services, null, 4), (error) => {
// Could not write data file.
self.emit('data', {
status: 200,
data: fs.createReadStream(path.join(root, config.site.index)),
type: 'text/html'
});
 
});
 
});
break;
case 'remove':
[ properfields['remove-services'] ].forEach((service) => {
var service_assets = [
path.join(root, 'services/' + service + '.html'),
path.join(root, 'services/' + service + '.png')
]
service_assets.forEach((asset) => {
fs.realpath(asset, (error, dataPath) => {
// If the path does not exist, then return early.
if (error) {
self.emit('log', {
message: 'Unable to write data file.',
message: 'Unable to access data path: ' + dataPath + ' error: ' + error,
severity: 'error'
});
self.emit('data', {
@@ -370,22 +259,38 @@
});
return;
}
// Send the main site index.
self.emit('log', {
message: 'Removed services.',
severity: 'info'
fs.unlink(dataPath, (error) => {
if(error) {
self.emit('log', {
message: 'Could not remove image file.',
severity: 'warning'
});
self.emit('data', {
status: 404,
data: new stream.Readable({
read(size) {
this.push(null);
}
}),
type: 'text/plain'
});
}
});
self.emit('data', {
status: 200,
data: fs.createReadStream(path.join(root, config.site.index)),
type: 'text/html'
});
});
});
});
// Send the main site index.
self.emit('log', {
message: 'Removed services.',
severity: 'info'
});
self.emit('data', {
status: 200,
data: fs.createReadStream(path.join(root, config.site.index)),
type: 'text/html'
});
break;
default:
self.emit('log', {