-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.js
163 lines (129 loc) · 3.9 KB
/
watcher.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
*
* Watcher
*
* @description :: Client side directory watcher to perform folder sync
* @help ::
* */
var chokidar = require('chokidar');
var fs = require('fs-extra');
var fsPath = require('path');
var request = require('request');
var uuid = require('node-uuid');
var deepEqual = require('deep-equal');
var SailsIo = require('sailsjs-socket.io-client');
// Get User home directory
var homeDirectory = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
// Setup uploader home directory
var ImLinkUpDirectory = homeDirectory + '/ImlinkUp';
// Set API end point
var apiURL = 'http://192.168.21.32/client/';
// Generate session key
var sessionUUID = uuid.v4();
// Connect socket client to server
// =============== SOCKET ================
SailsIo.connect('http://192.168.21.32', {}, function (socket) {
socket.on('connecting', function () {
console.info('Connecting to websocket server on ', apiURL);
});
socket.on('connect', function () {
console.info('connected to', apiURL);
socket.on(sessionUUID, function (data) {
console.log('Emiting at ' + data.speedMbps + ' mbps ' + data.percent + '% uploaded');
});
socket.on('disconnect', function (reason) {
console.info('Disconnected from websocket server on ', apiURL, reason);
});
});
socket.on('error', function (reason) {
console.error('(EE) Error connecting to server: ' + reason);
process.exit(code = 0);
});
});
// =============== SOCKET ================
// Create ImlinkUp Directory to watch
fs.mkdirs(ImLinkUpDirectory, function (err) {
var watcher;
if (err) return console.error(err);
console.log(
'ImlinkUp Watch Directory Created: ',
ImLinkUpDirectory,
'\n--------------------------------');
// Start Watching directory
watcher = chokidar.watch(ImLinkUpDirectory, {
ignored : /[\/\\]\./,
persistent : true,
interval : 250,
binaryInterval: 600,
depth : 100
});
watcher
.on('add', function (path) {
doFSEvent(path, 'add');
})
.on('addDir', function (path) {
doFSEvent(path, 'addDir');
})
.on('change', function (path) {
doFSEvent(path, 'change');
})
.on('unlink', function (path) {
doFSEvent(path, 'unlink');
})
.on('unlinkDir', function (path) {
doFSEvent(path, 'unlinkDir');
})
.on('error', function (error) {
console.log('Error happened', error);
})
.on('ready', function () {
// Get remote & local volume tree
var localTree;
var remoteTree;
console.log('Initial scan complete. Ready for changes.');
// Todo: Perform sync with remote DB
// compare local tree with remote tree
deepEqual(localTree, remoteTree);
// Sync Files
// Upload to remote
// Download from remote
})
.on('raw', function (event, path, details) {
//log('Raw event info:', event, path, details);
});
});
function doFSEvent(path, event) {
var formData;
// Upload files data
formData = {
clientPath : ImLinkUpDirectory,
type : '',
size : '',
name : fsPath.basename(path),
company : 'imtech',
path : path.replace(ImLinkUpDirectory, '').length ? path.replace(ImLinkUpDirectory, '') : '/',
sessionUUID: sessionUUID
};
console.info('Uploading file: ', formData.name);
switch (event) {
case 'add':
formData.type = 'file';
// Upload only if this is a file
formData['files'] = fs.createReadStream(path);
doHttp(event, formData);
break;
case 'addDir':
formData.type = 'directory';
doHttp(event, formData);
break;
default:
doHttp(event, formData);
break;
}
}
function doHttp(event, formData) {
request.post({url: apiURL + event, formData: formData}, function (err, httpResponse, body) {
if (err) return console.error(event, formData, err);
console.log(event, ': ', body);
});
}