Skip to content

Commit

Permalink
server index added, download, status, list api done
Browse files Browse the repository at this point in the history
  • Loading branch information
patheticGeek committed Feb 17, 2020
1 parent 119e125 commit 778e921
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 282 deletions.
1 change: 1 addition & 0 deletions downloads/sample.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sample file.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const express = require("express");
const next = require("next");
const compression = require("compression");
const bodyParser = require("body-parser");
const serveIndex = require("serve-index");

const humanTime = require("./utils/humanTime");
const keepalive = require("./utils/keepalive");
Expand All @@ -27,8 +28,6 @@ keepalive();
server.use(compression());
server.use(bodyParser.json());

server.use("/api/v1/downloads", express.static("downloads"));

server.use("/api/v1/torrent", torrent);

server.use("/api/v1/search", search);
Expand All @@ -54,6 +53,12 @@ keepalive();
res.send(currStatus);
});

server.use(
"/downloads",
express.static("downloads"),
serveIndex("downloads", { icons: true })
);

server.all("*", (req, res) => {
handle(req, res);
});
Expand Down
95 changes: 39 additions & 56 deletions lib/torrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,9 @@ if (!site) console.log("SET SITE ENVIORMENT VARIABLE. READ DOCS");

class Torrent {
constructor() {
this.downloads = downloadsLoad;
this.downloads = {};
this.client = new WebTorrent();
}

updateDownloads = (id, data) => {
this.downloads[id] = { ...this.downloads[id], ...data };
const downloads = JSON.stringify(this.downloads);
fs.writeFileSync("./downloads.json", downloads);
};

statusLoader = torrent => {
return {
infoHash: torrent.infoHash,
Expand All @@ -34,76 +27,66 @@ class Torrent {
progress: parseInt(torrent.progress * 100),
timeRemaining: parseInt(torrent.timeRemaining),
redableTimeRemaining: humanTime(torrent.timeRemaining),
files: torrent.files.map(file => ({
name: file.name,
downloaded: prettyBytes(file.downloaded),
total: prettyBytes(file.length),
progress: parseInt(file.progress * 100),
path: file.path,
downloadLink: `${site}api/v1/downloads/${torrent.infoHash}/${file.path}`
}))
noOfFiles: torrent.files.length,
downloadLink: `${site}downloads/${torrent.infoHash}`
};
};

setDownload = (torrent, additional) => {
this.downloads[torrent.infoHash] = {
...this.statusLoader(torrent),
...additional
};
};

addTorrent = (link, cb1, cb2) => {
onDownloadStart = torrent => {
this.setDownload(torrent, { status: "Downloading Metadata..." });
};

onDownloadProgress = torrent => {
this.setDownload(torrent, { status: "Downloading..." });
};

onDownloadComplete = torrent => {
this.setDownload(torrent, { status: "Saving to disk..." });
this.saveFiles(torrent);
};

download = (link, onStart, onDone) => {
if (!this.client.get(link)) {
const torrent = this.client.add(link);
if (cb1) {
torrent.on("metadata", () => cb1(this.statusLoader(torrent)));
}
torrent.once("done", () => {
this.saveFiles(torrent);
if (cb2) cb2(this.statusLoader(torrent));

torrent.on("metadata", () => {
this.onDownloadStart(torrent);
if (onStart) onStart(this.get(torrent.infoHash));
});
}
};

removeTorrent = link => {
this.client.get(link) ? this.client.remove(link) : undefined;
};
torrent.once("done", () => {
this.onDownloadComplete(torrent);
if (onDone) onDone(this.get(torrent.infoHash));
});

getTorrent = link => {
let torrent = this.client.get(link);
if (torrent) {
const status = this.statusLoader(torrent);
if (this.downloads[status.infoHash]) {
return this.downloads[status.infoHash];
} else {
return status;
}
} else {
return null;
torrent.on("download", () => this.onDownloadProgress(torrent));
}
};

listTorrents = () => {
return this.client.torrents.map(torrent => this.statusLoader(torrent));
remove = infoHash => {
this.client.get(infoHash) ? this.client.remove(infoHash) : undefined;
this.downloads[infoHash] = undefined;
return null;
};

listDownloads = () => {
return Object.entries(this.downloads).map(v => v[1]);
};
get = infoHash => this.downloads[infoHash];

saveFiles = torrent => {
const torrentStatus = this.statusLoader(torrent);
this.updateDownloads(torrent.infoHash, {
status: "Saving files...",
speed: "",
...torrentStatus
});
torrent.files.forEach((file, i) => {
const filePath = "./downloads/" + torrent.infoHash + "/" + file.path;
mkfile(filePath);
let toFile = fs.createWriteStream(filePath);
let torrentFile = file.createReadStream();
torrentFile.pipe(toFile);
this.updateDownloads(torrent.infoHash, {
status: `${i + 1} of ${torrent.files.length} saved`
});
});
this.updateDownloads(torrent.infoHash, {
status: `Files saved`
});
this.setDownload(torrent, { status: "Downloaded" });
};
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"puppeteer": "^2.0.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"serve-index": "^1.9.1",
"webtorrent": "^0.107.17"
}
}
23 changes: 21 additions & 2 deletions routes/torrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,27 @@ router.get("/download", (req, res) => {
} else if (link.indexOf("magnet:") !== 0) {
res.send({ error: true, errorMessage: "Link is not a magnet link" });
} else {
torrent.addTorrent(link);
res.send({ error: false, link });
torrent.download(link, torrent =>
res.send({ error: false, link, infoHash: torrent.infoHash })
);
}
});

router.get("/status", (req, res) => {
const infoHash = req.query.infoHash;

if (!infoHash) {
res.send({ error: true, errorMessage: "No infoHash provided" });
} else {
res.send({ error: false, status: torrent.get(infoHash) });
}
});

router.get("/list", (req, res) => {
try {
res.json(torrent.downloads);
} catch (e) {
res.json({ error: true, message: e.message });
}
});

Expand Down
Loading

0 comments on commit 778e921

Please sign in to comment.