Storages.getStorage(session.selectedCloud).callbackHandler(req.query, (creds => { if (!creds) { res.end("Error"); return; } session.clouds[session.selectedCloud].creds = creds; session.save(); res.redirect('/'); }));
client.on("selectCloud", (data) => { var cloud = data.cloud; if (session.clouds[cloud]) { session.selectedCloud = cloud; session.save(); client.emit('setObj', { name: 'selectedCloud', value: session.clouds[session.selectedCloud] }); } });
client.on("updateConfig", config => { session.config = config; session.save(); })
client.on("toggleIncognito", () => { session.incognito = !session.incognito; session.save(); });
io.on('connection', function (client) { var sessionID = client.conn.request.sessionID; var session = client.conn.request.session; //Process Session if (!session.clouds) { session.clouds = Storages.getTemplate(); //an object like : {"Gdrive":{displayName:"..",url:".."},"..":{displayName:"..","url":".."}} session.selectedCloud = "GDrive"; //config session.config = { clientDownload: { value: false, displayName: "Stream downloads to user", type: "checkbox", title: "Choose whether to stream file to client while catching downloads or not, if unchecked windows will close after download is captured." }, csHead: { value: true, displayName: "Show cloud selection button in main menu", type: "checkbox" }, askForName: { value: true, displayName: "Ask for filename when uploading files", type: "checkbox" } } session.save(); } //send config client.emit('setObj', { name: "config", value: session.config }) //send clouds client.emit('setObj', { name: 'clouds', value: session.clouds }); client.emit('setObj', { name: 'selectedCloud', value: session.clouds[session.selectedCloud] }); //send downloads client.emit('setObj', { name: 'visitedPages', value: visitedPages }); //send torrrents client.emit('setObj', { name: 'torrents', value: torrents }); client.emit('setObj', { name: 'incognito', value: session.incognito ? session.incognito : false }); client.on('clearVisitedPages', () => { Object.keys(visitedPages).forEach((id) => { clearVisitedPage(id); }); }); client.on('clearTorrents', () => { Object.keys(torrents).forEach((id) => { clearTorrent(id); }); }); client.on('delete', data => { data.isTorrent ? clearTorrent(data.id) : clearVisitedPage(data.id); }); client.on('saveToDrive', (data) => { saveToDriveHandler(session, data); }); client.on('pin', (data) => { if (data.isTorrent) { torrents[data.page.id].pinned = true; sendTorrentsUpdate(io, data.page.id, ["pinned"]); return false; } visitedPages[data.page.id].pinned = true; sendVisitedPagesUpdate(io, data.page.id, ["pinned"]); }); client.on('unpin', (data) => { if (data.isTorrent) { torrents[data.page.id].pinned = false; sendTorrentsUpdate(io, data.page.id, ["pinned"]); return false; } visitedPages[data.page.id].pinned = false; sendVisitedPagesUpdate(io, data.page.id, ["pinned"]); }); client.on('pirateSearch', (data) => { var query = data.query; var page = data.page; scrapeIt(`https://thepiratebay.org/search/${encodeURIComponent(query)}/${page}/7/0`, { result: { listItem: "tr:not(.header)", data: { name: "a.detLink", size: { selector: ".detDesc", convert: x => { return x.match(/Size (.*),/)[1]; } }, seeders: { selector: "td", eq: 2 }, leechers: { selector: "td", eq: 3 }, magnetLink: { selector: "a", eq: 3, attr: "href" }, link: { selector: "a.detLink", attr: "href", convert: x => `https://thepiratebay.org${x}` } } } }).then(data => { client.emit('setObj', { name: 'search', value: { results: data.result, loading: false } }); }); }); client.on('addTorrent', (data) => { var dupes = Object.keys(torrents).filter((key) => { return magnet.decode(data.magnet).infoHash == torrents[key].infoHash; }); if (dupes.length > 0) { return false; } var uniqid = shortid(); if (!data.magnet.startsWith("magnet")) { //try to load magnet magnetLink(data.magnet, (err, link) => { if (err) { debug(`Failed to load magnet from torrent: ${err.message}`); client.emit("setObj", { name: 'magnetLoading', value: false }); client.emit("alert", "Unable to load the .torrent"); return; } //all good !! add magnet addTorrent(link, uniqid, client); }); return; } addTorrent(data.magnet, uniqid, client); }); client.on('getDirStructure', (data) => { var id = data.id; var dirStructure = torrentObjs[id].getDirObj(); torrents[id].gettingDirStructure = false; torrents[id].dirStructure = dirStructure; torrents[id].msg = 'Got directory structure'; torrents[id].showFiles = true; sendTorrentsUpdate(client, id); //fix directory structure not hidden after page reload torrents[id].showFiles = false; }); client.on("uploadDirToDrive", (data) => { uploadDirToDrive(session, data); }); client.on("zip", (data) => { //exclusively for torrents var id = data.id; if (torrents[id].zipping || torrents[id].progress < 100) { //invalid context return false; } var zippedLength = 0; //no need to check if zip exists //event will emit only if zipExists is not set var output = FILE.createWriteStream(path.join(FILES_PATH, id + ".zip")); var archive = archiver('zip', { store: true // Sets the compression method to STORE. }); // listen for all archive data to be written output.on('close', function () { debug("Zipped %s successfully", id); torrents[id].zipping = false; torrents[id].msg = "Zipped Successfully" torrents[id].zipExists = true; sendTorrentsUpdate(io, id); }); archive.on('error', function (err) { debug("Error while zipping %s : %s", id, err); }); // pipe archive data to the file archive.pipe(output); archive.directory(path.join(FILES_PATH, id), false); archive.finalize(); var percent = 0; //listen for progress archive.on("data", (chunk) => { zippedLength += chunk.length; var percentNow = percentage(zippedLength / torrents[id].length); if ((percentNow - percent) > 0.1 || percentNow == 100) { percent = percentNow; torrents[id].msg = "Zipping : " + percentNow + "%"; torrents[id].zipping = true; sendTorrentsUpdate(io, id); } }); }); client.on("toggleIncognito", () => { session.incognito = !session.incognito; session.save(); }); client.on("uploadZipToCloud", (data) => { var id = data.id; var name = data.name; var loc = path.join(FILES_PATH, id + ".zip"); var cloud = Storages.getStorage(session.selectedCloud); var cloudInstance = new cloud(session.clouds[session.selectedCloud].creds); if (!cloudInstance.uploadFile) { visitedPages[id].msg = "Feature Unavailable"; sendVisitedPagesUpdate(io, id); return; } cloudInstance.uploadFile(FILE.createReadStream(loc), FILE.statSync(loc).size, mime.lookup(loc), name, false); cloudInstance.on("progress", (data) => { torrents[id].msg = "Uploading Zip: " + percentage(data.uploaded / data.size) + "%"; torrents[id].zipping = true; sendTorrentsUpdate(io, id); }); cloudInstance.on("fileUploaded", (data) => { torrents[id].msg = "Uploaded Zip Successfully"; torrents[id].zipping = false; sendTorrentsUpdate(io, id); }); }); client.on("selectCloud", (data) => { var cloud = data.cloud; if (session.clouds[cloud]) { session.selectedCloud = cloud; session.save(); client.emit('setObj', { name: 'selectedCloud', value: session.clouds[session.selectedCloud] }); } }); client.on("updateConfig", config => { session.config = config; session.save(); }) });