-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
165 lines (152 loc) · 4.51 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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');
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("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 spawn = require('child_process').spawn,
child = spawn('megacmd', ['put',tmpFile,'mega:/']);
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) {
console.log("Spawn child error:"+ data.toString());
onErr(data.toString());
onComplete();
return;
});
// var storage = mega({email:'[email protected]', password:'bmsce123', keepalive: false}),
// fname = path.basename(tmpFile),
// fsize = fs.statSync(tmpFile).size,
// prev = 0;
// var up = storage.upload({
// name: fname,
// size: fsize // removing this causes data buffering.
// });
// up.on('progress', function(stats){
// var percentComplete = Math.floor(100.0 * stats.bytesLoaded/fsize),
// mbComplete = (stats.bytesLoaded/1048576).toFixed(2);
// if(percentComplete-prev > 0) {
// onProgress(percentComplete, mbComplete);
// prev = percentComplete;
// }
// });
// up.on('complete', function(){
// fs.unlink(tmpFile);
// onComplete();
// });
// up.on('error', function(e){
// console.log(e.message);
// });
// fs.createReadStream(tmpFile, {
// 'bufferSize': 4 * 1024
// }).pipe(up);
};
},
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];
}
}
};
};
app.use(express.static(__dirname + '/public'));
app.get('/', function(request, response) {
response.sendFile(__dirname + '/index.html');
});
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() {
socket.emit('linkuploadcomplete',{message:"Upload Complete", hash:c.hash});
});
}, 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});
});
});