-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
56 lines (45 loc) · 1.2 KB
/
index.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
'use strict';
var express = require('express'),
busboy = require('connect-busboy'),
fs = require('fs.extra'),
app = express();
var UPLOAD_FOLDER = 'bundles/nodecg-follow/view/uploads/',
SETTINGS_FILE = 'bundles/nodecg-follow/settings.json';
var settings;
module.exports = function(nodecg) {
app.use(busboy()); // For file uploading
/**
* Settings
*/
// Read
fs.readFile(SETTINGS_FILE, 'utf8', function (err, data) {
settings = JSON.parse(data);
// Declare
nodecg.Replicant('settings', {
defaultValue: settings,
persistent: false
}).on('change', function (oldVal, s) {
var settingsString = JSON.stringify(s, null, 2);
fs.writeFile(SETTINGS_FILE, settingsString, function (err) {
if (err) nodecg.log.error(err);
});
});
});
// Upload
app.post('/nodecg-follow/upload', function(req, res) {
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
nodecg.log.info('Uploading: ' + filename);
fstream = fs.createWriteStream(UPLOAD_FOLDER + filename);
file.pipe(fstream);
fstream.on('close', function() {
res.status(200).json({
status: 'success',
data: filename
});
});
});
});
nodecg.mount(app);
};