-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (56 loc) · 2.27 KB
/
index.js
File metadata and controls
60 lines (56 loc) · 2.27 KB
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
import WebTorrent from 'webtorrent'
import Hydrafiles from 'hydrafiles'
/**
* WebTorrent Client
* @param {Object=} opts
*/
class HydraTorrent extends WebTorrent {
constructor (webtorrentOpts = {}, hydrafilesOpts = {}) {
const hydrafiles = new Hydrafiles(hydrafilesOpts)
super(webtorrentOpts)
this.Hydrafiles = hydrafiles
}
/**
* Start downloading a new torrent. Aliased as `client.download`.
* @param {string|Buffer|Object} torrentId
* @param {Object} opts torrent-specific options
* @param {function=} ontorrent called when the torrent is ready (has metadata)
*/
add (torrentId, opts = {}, ontorrent = () => {}) {
super.add(torrentId, opts, async (torrent) => {
const nodes = client.Hydrafiles.nodes.getNodes({ includeSelf: false })
const webseeds = nodes.map((node => `${node.host}/infohash/${torrent.infoHash}`))
for (let i = 0; i < webseeds.length; i++) {
torrent.addWebSeed(webseeds)
}
let files = await this.search({ where: { infohash: torrent.infoHash } }, false)
for (let i = 0; i < files.length; i++) {
files = [...files, await this.search({ where: { hash: files[i].hash } }, false)]
}
for (let i = 0; i < files.length; i++) {
const file = await this.Hydrafiles.FileHandler.init(files[0], this.Hydrafiles)
const webseeds = [...nodes.map((node => `${node.host}/sha256/${file.hash}`)), ...nodes.map((node => `${node.host}/infohash/${file.infohash}`))]
for (let i = 0; i < webseeds.length; i++) {
torrent.addWebSeed(webseeds[i])
}
}
ontorrent(torrent)
})
}
/**
* Start seeding a new file/folder.
* @param {string|File|FileList|Buffer|Array.<string|File|Buffer>} input
* @param {Object=} opts
* @param {function=} onseed called when torrent is seeding
*/
seed (input, opts, onseed) {
super.seed(input, opts, (torrent) => {
const file = torrent.files[0]
console.log(file.path)
onseed(torrent)
})
}
async search (where, cache) {
await this.HydraTorrent.search(where, cache)
}
}