Browse Source

Clean up torrent client interface

dev
David Ludwig 4 years ago
parent
commit
2313c0f0aa
1 changed files with 31 additions and 17 deletions
  1. +31
    -17
      services/torrent/src/services/IpcInterface.ts

+ 31
- 17
services/torrent/src/services/IpcInterface.ts View File

@ -38,10 +38,11 @@ export default class IpcInterface extends IpcServerService
* Install the the event handlers * Install the the event handlers
*/ */
protected installMessageHandlers() { protected installMessageHandlers() {
this.addMessageHandler("add", this.addTorrent);
this.addMessageHandler("remove", this.removeTorrent);
this.addMessageHandler("list", this.listTorrents);
this.addMessageHandler("details", this.torrentDetails);
this.addMessageHandler("add", this.add);
this.addMessageHandler("remove", this.remove);
this.addMessageHandler("list", this.list);
this.addMessageHandler("details", this.details);
this.addMessageHandler("has", this.has);
this.torrentClient.on("torrent_finished", this.torrentFinished.bind(this)); this.torrentClient.on("torrent_finished", this.torrentFinished.bind(this));
} }
@ -50,8 +51,7 @@ export default class IpcInterface extends IpcServerService
/** /**
* Add a torrent to the client * Add a torrent to the client
*/ */
// protected async addTorrent(torrentInfo: IAddTorrent, downloadPath?: string) {
protected async addTorrent(payload: { torrent: IAddTorrent, downloadPath?: string }) {
protected async add(payload: { torrent: IAddTorrent, downloadPath?: string }) {
let torrent: WebTorrent.Torrent; let torrent: WebTorrent.Torrent;
if (typeof payload.torrent == "string") { if (typeof payload.torrent == "string") {
torrent = await this.torrentClient.add(payload.torrent, { downloadPath: payload.downloadPath }); torrent = await this.torrentClient.add(payload.torrent, { downloadPath: payload.downloadPath });
@ -62,38 +62,52 @@ export default class IpcInterface extends IpcServerService
} }
/** /**
* Remove a torrent from the
* @param message Add
* Remove a torrent from the client
*/ */
protected async removeTorrent(torrentId: string) {
try {
await this.torrentClient.remove(torrentId);
} catch(e) {}
protected async remove(torrentId: string) {
if (!this.torrentClient.has(torrentId)) {
return false;
}
await this.torrentClient.remove(torrentId);
return true;
} }
protected async listTorrents() {
/**
* List the torrents in the client
*/
protected async list() {
return this.torrentClient.torrents.map(torrent => Object({ return this.torrentClient.torrents.map(torrent => Object({
name: torrent.name,
name : torrent.name,
infoHash: torrent.infoHash, infoHash: torrent.infoHash,
progress: torrent.progress, progress: torrent.progress,
state: this.torrentClient.torrentState(torrent)
state : this.torrentClient.torrentState(torrent)
})); }));
} }
protected async torrentDetails(torrentIds: string[]) {
/**
* Get the details of the provided torrents
*/
protected async details(torrentIds: string[]) {
let torrents: WebTorrent.Torrent[]; let torrents: WebTorrent.Torrent[];
if (torrentIds.length == 0) { if (torrentIds.length == 0) {
torrents = this.torrentClient.torrents; torrents = this.torrentClient.torrents;
} else { } else {
torrents = torrentIds.map(torrentId => { torrents = torrentIds.map(torrentId => {
let torrent = this.torrentClient.get(torrentId); let torrent = this.torrentClient.get(torrentId);
assert(torrent != null, `Unknown torrent ID provided: ${torrentId}`);
assert(torrent !== null, `Unknown torrent ID provided: ${torrentId}`);
return torrent; return torrent;
}); });
} }
return torrents.map(torrent => this.torrentClient.serializeTorrent(torrent)); return torrents.map(torrent => this.torrentClient.serializeTorrent(torrent));
} }
/**
* Determine if the torrent client has the given torrent
*/
protected async has(torrentId: string) {
return this.torrentClient.get(torrentId) !== null;
}
// Subscription Interface Methods -------------------------------------------------------------- // Subscription Interface Methods --------------------------------------------------------------
/** /**


Loading…
Cancel
Save