forked from ratwix/PhotoTwixNodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_copyUsb.js
70 lines (61 loc) · 1.66 KB
/
server_copyUsb.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
var fs = require('fs');
var fs = require('fs-extra');
var path = require('path');
var copysync = require('copysync');
var g_files;
var g_socket;
var g_current_copy = 0;
var g_max_copy = 0;
var drive = "";
var startCopy = function() {
drive = "";
//Find the USB key
g_current_copy = 0;
for (var i = 100; i <= 120; i++) {
console.log('Test du disque ' + String.fromCharCode(i) + ':/');
if (fs.existsSync(String.fromCharCode(i) + ':/')) {
console.log("Key on drive " + String.fromCharCode(i));
drive = String.fromCharCode(i) + ':/';
if (!fs.existsSync(String.fromCharCode(i) + ':/phototwix')) {
fs.mkdirSync(drive + 'phototwix');
}
break;
}
}
if (drive == "") {
//TODO : mettre une erreur
return;
}
g_files = fs.readdirSync('./public/photos/result');
g_max_copy = g_files.length;
g_current_copy = 0;
copy_next();
}
function copy_next() {
console.log("Copy file ./public/photos/result/" + g_files[g_current_copy] + " to " + drive + 'phototwix/' + path.basename(g_files[g_current_copy]));
if (g_current_copy < g_max_copy) {
fs.copy('./public/photos/result/' + g_files[g_current_copy], drive + 'phototwix/' + path.basename(g_files[g_current_copy]), function(err) {
if (err) {
g_socket.emit('copyUsbError');
} else {
sendMessage();
g_current_copy++;
if (g_current_copy < g_max_copy) {
copy_next();
}
}
});
}
}
function setSocket(socket) {
console.log('Socket set USB ');
g_socket = socket;
}
function sendMessage() {
g_socket.emit('copyUsbProgress', {
current:g_current_copy + 1,
total:g_max_copy
}); //Send a message
}
exports.setSocket = setSocket;
exports.startCopy = startCopy;