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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | 8 8 8 8 8 8 8 | var events = require('events'); var torrents = module.exports = new events.EventEmitter(); var async = require('async'); var parse = require('parse-torrent'); var Archive = require('zip-stream'); var request = require('request'); var torrentStream = require('torrent-stream'); var File = require("./file"); var backend = require('./backend'); torrents.filesDownloading = 0; var list = torrents.list = []; //============= // every second, check the status of all active torrents setInterval(function() { var changed = false; var filesDownloading = 0; for(var i = 0; i < list.length; i++) { var t = list[i]; if(!t.$engine) continue; //check torrent speed var swarm = t.$engine.swarm; var status = { down: swarm.downloaded, downps: swarm.downloadSpeed(), up: swarm.uploaded, upps: swarm.uploadSpeed() }; for(var k in status) if(status[k] !== t.status[k]) changed = true; if(t.zipping) filesDownloading++; //check file status t.files.forEach(function(f) { if(f.uploading) filesDownloading++; }); t.status = status; } if(torrents.filesDownloading !== filesDownloading) { torrents.filesDownloading = filesDownloading; changed = true; } if(changed) torrents.emit("update"); }, 1000); //============= var fs = require("fs"); var rm = require("rimraf"); var path = require("path"); var TMP_DIR = path.resolve("./tmp"); var TS_DIR = path.join(TMP_DIR, "torrent-stream"); //on start, reopen existing torrents setTimeout(function() { if(!fs.existsSync(TS_DIR)) return; var files = fs.readdirSync(TS_DIR); if(!files) return; files.filter(function(f) { return /\.torrent$/.test(f); }).forEach( function(f) { var buff = fs.readFileSync(path.join(TS_DIR, f)); load(parse(buff), function(err) { if(!err) console.log("Restored torrent", f); }); }); }); //============= //helpers var findTorrent = function(hash) { for(var i = 0; i < list.length; i++) { var t = list[i]; if(t.hash === hash) return t; } return null; }; var findFile = function(torrent, path) { for(var i = 0; i < torrent.files.length; i++) { var f = torrent.files[i]; if(f.path === path) return f; } return null; }; //============= var load = function(t, callback) { if(!t) return callback("Invalid torrent"); if(!t.infoHash) return callback("Missing hash"); var torrent = findTorrent(t.infoHash); if(torrent) return callback("Torrent already exists"); torrent = { $engine: null, hash: t.infoHash, name: t.name, trackers: t.announce, magnet: parse.toMagnetURI(t), files: [], status: {} }; list.push(torrent); torrents.emit("update"); //loaded, now open it torrents.open({ hash:torrent.hash }, callback); }; torrents.load = function(data, callback) { if(data.magnet) { load(parse(data.magnet), callback); } else if(data.torrent) { request({ method: "GET", url: data.torrent, gzip: true, encoding: null //buffer! }, function(err, resp, body) { if(err) return callback("Invalid URL"); var t; try { t = parse(body) ; } catch(e) { return callback("Failed to parse torrent"); } load(t, callback); }); } else { return callback("Invalid request"); } }; torrents.open = function(data, callback) { var torrent = findTorrent(data.hash); if(!torrent) return callback("Torrent missing"); if(torrent.$engine) return callback("Torrent already open"); //dont wait - open torrent stream, mark openning and callback var engine = torrentStream(torrent.magnet, { connections: 100, uploads: 0, //TODO should upload, though we can be highly CPU/mem bound tmp: TMP_DIR, verify: true, dht: true }); torrent.$engine = engine; torrent.openning = true; torrents.emit("update"); callback(null); engine.on('error', function(err) { //TODO destroy torrent console.error("torrent '%s' error: %s", torrent.name, err); }); engine.on('ready', function() { //overwrite magnet name with real name torrent.name = engine.torrent.name; torrent.files = engine.files.map(function(f, i) { return new File(f, i, torrent); }); torrent.openning = false; torrent.open = true; torrents.emit("update"); }); }; torrents.close = function(data, callback) { var torrent = findTorrent(data.hash); if(!torrent) return callback("Torrent missing"); if(!torrent.$engine) return callback("Torrent not open"); torrent.$engine.destroy(function() { //ensure all files are stopped if(torrent.files) { torrent.files.forEach(function(f) { f.cancel(); }); } torrent.files = null; torrent.open = false; torrent.$engine = null; torrents.emit("update"); callback(null); }); }; torrents.remove = function(data, callback) { var torrent = findTorrent(data.hash); if(!torrent) return callback("Torrent missing"); if(torrent.$engine) return callback("Torrent is still open"); var i = list.indexOf(torrent); list.splice(i, 1); torrents.emit("update"); //clear torrent files and torrent rm(path.join(TS_DIR, torrent.hash), function(err) { if(err) console.log("failed to delete: %s", torrent.hash); }); rm(path.join(TS_DIR, torrent.hash+".torrent"), function(err) { if(err) console.log("failed to delete: %s.torrent", torrent.hash); }); callback(null); }; torrents.downloadFile = function(data, callback) { var torrent = findTorrent(data.hash); if(!torrent) return callback("Missing torrent"); var file = findFile(torrent, data.path); if(!file) return callback("Missing file"); if(file.downloading) return callback("Already downloading"); //callback to user early since uploads can take hours... //user receives updates via websockets callback(null); file.uploading = true; torrents.emit("update"); //pass copy of file to backend backend.upload({ path: file.path, length: file.length, createReadStream: file.createReadStream.bind(file) }, function(err) { file.uploading = false; //receive result from backend if(err && err !== "cancelled") { file.downloadError = "Backend Error"; torrents.emit("update"); return console.error("backend error: ", err); } torrents.emit("update"); //success, now re-list backend.list(function(err, files) { if(err) return console.error("failed to list"); torrents.emit("update", files); }); }); }; torrents.cancelFile = function(data, callback) { var torrent = findTorrent(data.hash); if(!torrent) return callback("Missing torrent"); var file = findFile(torrent, data.path); if(!file) return callback("Missing file"); if(!file.downloading) return callback("Not downloading"); var success = file.cancel(); callback(success ? null : "Failed to close file"); }; torrents.zipAll = function(data, callback) { var torrent = findTorrent(data.hash); if(!torrent) return callback("Missing torrent"); var files = torrent.files; var archive = new Archive(); archive.on('error', function(err) { console.error("zip error:", err); }); async.series(files.map(function(f, i) { return function(cb) { archive.entry(f.createReadStream(), { name: f.path }, function(err) { if(err) return cb(err); cb(null); }); }; }), function(err) { if(err) { torrent.zipping = false; torrents.emit("update"); return console.error("zip archive error:", err); } archive.finish(); }); torrent.zipping = true; torrents.emit("update"); //callback to user early since uploads can take hours... //user receives updates via websockets callback(null); //pass zip stream to backend backend.upload({ path: torrent.name + ".zip", length: files.reduce(function(len, f) { return len+f.length; }, 0), createReadStream: function() { return archive; } }, function(err) { torrent.zipping = false; torrents.emit("update"); if(err) { return console.error("zip upload error:", err); } backend.list(function(err, files) { if(err) return console.error("failed to list"); torrents.emit("update", files); }); }); }; torrents.trash = function(data, callback) { if(!data.path) return callback("Missing path"); backend.remove(data.path, function(err) { if(err) return callback("Failed to trash: " + err); backend.list(function(err, files) { if(err) return callback("Failed to list: " + err); torrents.emit("update", files); callback(null); }); }); }; |