Add p_addTorrentFromTorrentFile

This commit is contained in:
John Hiesey 2019-03-19 16:24:11 -07:00
parent 91e1c40db2
commit c60a31510e
2 changed files with 21 additions and 3 deletions

View File

@ -117,7 +117,7 @@ class TransportWEBTORRENT extends Transport {
return { torrentId, path } return { torrentId, path }
} }
async p_webtorrentadd(torrentId) { async p_webtorrentadd(torrentId, opts) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Check if this torrentId is already added to the webtorrent client // Check if this torrentId is already added to the webtorrent client
let torrent = this.webtorrent.get(torrentId); let torrent = this.webtorrent.get(torrentId);
@ -128,7 +128,7 @@ class TransportWEBTORRENT extends Transport {
//let testid = "magnet:?xt=urn:btih:ELHVM7F4VEOTZQFDHCX7OZXUXKINUIPJ&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Fcommute"; //let testid = "magnet:?xt=urn:btih:ELHVM7F4VEOTZQFDHCX7OZXUXKINUIPJ&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&tr=wss%3A%2F%2Ftracker.fastcast.nz&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Fcommute";
//let testidnewtrack = "magnet:?xt=urn:btih:ELHVM7F4VEOTZQFDHCX7OZXUXKINUIPJ&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org:6969&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Fcommute"; //let testidnewtrack = "magnet:?xt=urn:btih:ELHVM7F4VEOTZQFDHCX7OZXUXKINUIPJ&tr=http%3A%2F%2Fbt1.archive.org%3A6969%2Fannounce&tr=http%3A%2F%2Fbt2.archive.org%3A6969%2Fannounce&tr=wss%3A%2F%2Fdweb.archive.org:6969&ws=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Fdownload%2F&xs=https%3A%2F%2Fdweb.me%2Farc%2Farchive.org%2Ftorrent%2Fcommute";
//if (torrentId === testid) torrentId = testidnewtrack; //if (torrentId === testid) torrentId = testidnewtrack;
torrent = this.webtorrent.add(torrentId); torrent = this.webtorrent.add(torrentId, opts);
torrent.once("error", (err) => { torrent.once("error", (err) => {
reject(new errors.TransportError("Torrent encountered a fatal error " + err.message)); reject(new errors.TransportError("Torrent encountered a fatal error " + err.message));
@ -223,6 +223,24 @@ class TransportWEBTORRENT extends Transport {
} }
async p_addTorrentFromTorrentFile(torrentFilePath, filesPath) {
// TODO: doc
try {
const opts = { path: filesPath };
const oldTorrent = this.webtorrent.get(torrentId);
if (oldTorrent) {
oldTorrent.rescanFiles();
} else {
const torrent = await this.p_webtorrentadd(torrentFilePath, opts);
torrent.deselect(0, torrent.pieces.length - 1, false); // Dont download entire torrent as will pull just the file we want (warning - may give problems if multiple reads from same webtorrent)
}
} catch(err) {
// Logged by Transports
throw(err);
}
}
async p_f_createReadStream(url, {wanturl=false}={}) { async p_f_createReadStream(url, {wanturl=false}={}) {
/* /*
Fetch bytes progressively, using a node.js readable stream, based on a url of the form: Fetch bytes progressively, using a node.js readable stream, based on a url of the form:

View File

@ -20,7 +20,7 @@
"node-fetch": "^2.3.0", "node-fetch": "^2.3.0",
"readable-stream": "^3.1.1", "readable-stream": "^3.1.1",
"webpack": "^4.29.3", "webpack": "^4.29.3",
"webtorrent": "^0.103.0", "webtorrent": "git://github.com/jhiesey/webtorrent.git#rescan",
"y-array": "^10.1.4", "y-array": "^10.1.4",
"y-indexeddb": "^8.1.9", "y-indexeddb": "^8.1.9",
"y-ipfs-connector": "^2.3.0", "y-ipfs-connector": "^2.3.0",