-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
144 lines (137 loc) · 4.25 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
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
var express = require('express'),
app = express(),
appPort = (process.env.PORT || 5000),
server = app.listen(appPort, function() {
console.log("Node app is running at localhost:" + appPort);
})
parse = require('url-parse');
var io = require('socket.io')(server),
request = require('request'),
path = require('path'),
http = require('http'),
fs = require('fs'),
crypto = require('crypto'),
mega = JSON.parse(fs.readFileSync('mega.json', 'utf8'));
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/accounts', function(req, res) {
var ex = require('child_process').execSync;
var acc = ex('megals -u [email protected] -p bmsce123 -e /Root').toString();
res.setHeader("Content-type", "text/plain");
res.send(acc);
});
var cloudObj = function() {
this.init = function(url) {
this.URL = url;
this.hash = crypto.createHash('md5').update(url).digest('hex');
};
this.downloadFile = function(onAdded, onData, onEnd, onError) {
console.log("Downloading file: "+this.URL);
var fname = this.URL.split('/').pop();
var tmpFile = __dirname+"/tmp/"+fname,
cur = 0,
len = 0,
total = 0,
prev = 0;
console.log("LOG::Temporary File: "+tmpFile);
var wStream = fs.createWriteStream(tmpFile);
var req = request({
method: 'GET',
uri: this.URL
});
req.on('data', function (chunk) {
cur += chunk.length;
var percentComplete = Math.floor(100.0 * cur / len),
mbComplete = (cur / 1048576).toFixed(2);
if(percentComplete-prev > 0)
{
prev = percentComplete;
onData(percentComplete, mbComplete);
}
});
req.on('response', function(data){
len = parseInt(data.headers['content-length'], 10);
total = (len/1048576).toFixed(2);
onAdded(fname, total);
});
req.on('end', function() {
// global.gc();
onEnd(tmpFile);
});
req.pipe(wStream);
};
this.uploadFile = function(tmpFile, onProgress, onErr, onComplete) {
var cp = require('child_process'),
spawn = cp.spawn,
child = spawn('megacmd', ['put',tmpFile,'mega:/']),
fname = tmpFile.split('/').pop();
child.stdout.on('data', function (data) {
onProgress(data.toString());
console.log("Spawn child stdout:"+ data.toString());
});
child.stdout.on('end', function () {
fs.unlink(tmpFile);
onComplete();
});
child.stderr.on('data', function(data) {
var shareLink = cp.execSync("megals -u "+mega.accounts[0].username+" -p bmsce123 -e /Root/"+fname).toString();
console.log("MegaLS:" +shareLink);
console.log("Spawn child error:"+ data.toString());
onErr(data.toString());
onComplete(shareLink.match(/(.*?) \/Root(.*?)/)[1].trim(), fname);
return;
});
};
},
cloudObjManager = function() {
this.listOfObj = [];
this.addlink = function(cObj) {
this.listOfObj.push(cObj);
};
this.deletelink = function(cHash) {
for(var i=0;i<this.listOfObj.length;i++)
{
if(this.listOfObj[i].hash === cHash)
{
delete this.listOfObj[i];
}
}
};
};
io.on('connection', function(socket) {
var CloudManager = new cloudObjManager();
socket.on('addlink', function(msg){
var https = /^https/;
if(https.test(msg.link))
{
socket.emit('linkerror',{message:"The link cannot be https"});
return;
}
var c = new cloudObj();
c.init(msg.link);
c.downloadFile(function(name, size){
socket.emit('linkadded',{message:"Added", hash: c.hash, filesize: size, filename: name});
}, function(pc,mc){
socket.emit('linkdownloadprogress',{message:"Download Progress", hash:c.hash, pComplete:pc,mComplete:mc});
}, function(file) {
socket.emit('linkdownloadcomplete',{message:"Download Complete", hash:c.hash});
c.uploadFile(file, function(data){
socket.emit('linkuploadprogress',{message:data, hash: c.hash});
}, function(data) {
socket.emit('linkuploaderror', {message: data, hash: c.hash});
}, function(slink, fname) {
socket.emit('linkuploadcomplete',{message:"Upload Complete", hash:c.hash, link: slink, filename:fname});
});
}, function(err){
socket.emit('linkdownloaderror',{message:"Download Error", hash:c.hash, error:err.message});
});
CloudManager.addlink(c);
});
socket.on('deletelink', function(msg){
var cHash = msg.hash;
CloudManager.deletelink(msg.hash);
socket.emit('linkdeleted', { message:"deleted", hash:cHash});
});
});