-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploader.js
94 lines (82 loc) · 2.1 KB
/
uploader.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
'use strict'
var fs = require('fs');
var util = require('./util');
var request = require('request');
const timerInterval = 10000;
const chkfile = "updata.dat";
const lockfile = "uploader.lock";
var execFlag = 0;
var execGetFlag = 0;
var files = [];
var curDate = util.getCurrentDate();
var path = "./data/" + curDate;
var lastFile = "";
var url=""
var formData = {
"timeout": 180000
}
exports.startUploader = function(){
if(util.isExistLockfile(lockfile)){
console.log("Other request is executing now. Cancel this request");
return;
}else{
fs.writeFileSync(lockfile, "1");
}
files = fs.readdirSync(path);
console.log(files);
var timer1 = setInterval(() => {
if(execFlag != 0){
console.log("Other proc is executing now. Skip this proc");
return;
}
if(execGetFlag != 0){
console.log("Get proc is executing now. Skip this proc");
return;
}
execFlag = 1;
console.log("Exec...");
fs.readFile(chkfile, 'utf-8', function(err, data){
if(err){
console.log(err);
} else {
data = data.replace(/\r?\n/g,"");
var pos = files.indexOf(data);
if(pos != files.length - 1){
files.splice(0, pos + 1);
console.log(files);
if(execGetFlag == 0){
execGetFlag = 1;
uploadProc();
} else {
console.log("Still executing get proc. Only update list.");
}
}else{
console.log("No data to upload");
clearInterval(timer1);
fs.unlinkSync(lockfile);
};
};
});
execFlag = 0;
}, timerInterval);
}
function uploadProc(){
var filename = path + "/" + files[0];
console.log(filename);
formData.file = fs.createReadStream(filename);
request.post({url:url, formData:formData}, function(err, response, body){
if(err){
console.log(err)
}else{
console.log(body)
fs.writeFileSync(chkfile, files[0]);
files.shift();
if(files.length != 0){
uploadProc();
}else{
execGetFlag = 0;
console.log("uploadProc end.");
};
};
});
};