|
|
@ -5,8 +5,9 @@ import parseTorrent from "parse-torrent"; |
|
|
|
import { extname, join, sep } from "path"; |
|
|
|
import Torrent from "../database/entities/Torrent"; |
|
|
|
import rimraf from "rimraf"; |
|
|
|
import { ISerializedTorrent, TorrentState } from "../common"; |
|
|
|
|
|
|
|
interface AddOptions { |
|
|
|
interface IAddOptions { |
|
|
|
downloadPath?: string; |
|
|
|
extensions?: string | string[]; |
|
|
|
} |
|
|
@ -73,8 +74,13 @@ export default class TorrentClient |
|
|
|
protected async loadTorrents() { |
|
|
|
let torrents = await Torrent.find(); |
|
|
|
for (let torrent of torrents) { |
|
|
|
let torrentInfo = parseTorrent(torrent.torrentFile); |
|
|
|
this.addTorrent(torrentInfo, torrent.downloadPath, torrent.selectedFiles()); |
|
|
|
try { |
|
|
|
let torrentInfo = parseTorrent(torrent.torrentFile); |
|
|
|
this.addTorrent(torrentInfo, torrent.downloadPath, torrent.selectedFiles()); |
|
|
|
} catch(e) { |
|
|
|
torrent.remove(); |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -154,10 +160,10 @@ export default class TorrentClient |
|
|
|
/** |
|
|
|
* Add a torrent to the client |
|
|
|
*/ |
|
|
|
public async add(info: string | Buffer, options: AddOptions = {}) { |
|
|
|
public async add(info: string | Buffer, options: IAddOptions = {}) { |
|
|
|
// Parse the torrent
|
|
|
|
let torrentInfo = parseTorrent(info); |
|
|
|
assert(typeof torrentInfo.infoHash === "string"); |
|
|
|
assert(typeof torrentInfo.infoHash === "string" && torrentInfo.infoHash.length > 0, "Invalid magnet link provided"); |
|
|
|
|
|
|
|
// If the torrent already exists, skip it
|
|
|
|
assert(this.__webtorrent.get(torrentInfo.infoHash) === null, "Torrent has already been added"); |
|
|
@ -179,7 +185,7 @@ export default class TorrentClient |
|
|
|
/** |
|
|
|
* Remove a torrent from the client |
|
|
|
*/ |
|
|
|
public async remove(info: string | Buffer, withData: false) { |
|
|
|
public async remove(info: string | Buffer, withData: boolean = false) { |
|
|
|
// Parse the torrent
|
|
|
|
let torrentInfo = parseTorrent(info); |
|
|
|
assert(typeof torrentInfo.infoHash === "string"); |
|
|
@ -196,4 +202,48 @@ export default class TorrentClient |
|
|
|
this.removeTorrentFiles(torrent); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public has(infoHash: string) { |
|
|
|
return this.__webtorrent.get(infoHash) != null; |
|
|
|
} |
|
|
|
|
|
|
|
public get(infoHash: string) { |
|
|
|
return this.__webtorrent.get(infoHash); |
|
|
|
} |
|
|
|
|
|
|
|
public torrentState(torrent: WebTorrent.Torrent) { |
|
|
|
let state = 0; |
|
|
|
state |= <number>(torrent.ready && TorrentState.Ready); |
|
|
|
state |= <number>(torrent.paused && TorrentState.Paused); |
|
|
|
state |= <number>(torrent.done && TorrentState.Done); |
|
|
|
return <TorrentState>state; |
|
|
|
} |
|
|
|
|
|
|
|
public serializeTorrent(torrent: WebTorrent.Torrent) { |
|
|
|
return <ISerializedTorrent>{ |
|
|
|
name: torrent.name, |
|
|
|
infoHash: torrent.infoHash, |
|
|
|
downloaded: torrent.downloaded, |
|
|
|
uploaded: torrent.uploaded, |
|
|
|
ratio: torrent.ratio, |
|
|
|
size: torrent.length, |
|
|
|
downloadSpeed: torrent.downloadSpeed, |
|
|
|
uploadSpeed: torrent.uploadSpeed, |
|
|
|
numPeers: torrent.numPeers, |
|
|
|
progress: torrent.progress, |
|
|
|
path: torrent.path, |
|
|
|
state: this.torrentState(torrent), |
|
|
|
files: torrent.files.map(file => Object({ |
|
|
|
path: file.path, |
|
|
|
size: file.length, |
|
|
|
downloaded: file.downloaded, |
|
|
|
progress: file.progress, |
|
|
|
selected: file.isSelected |
|
|
|
})) |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
get torrents() { |
|
|
|
return this.__webtorrent.torrents; |
|
|
|
} |
|
|
|
} |