From 2313c0f0aa5b4e4314dd8ebae46e259a92df61e1 Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Wed, 5 May 2021 23:21:29 -0500 Subject: [PATCH] Clean up torrent client interface --- services/torrent/src/services/IpcInterface.ts | 48 ++++++++++++------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/services/torrent/src/services/IpcInterface.ts b/services/torrent/src/services/IpcInterface.ts index eef7dd4..b99222a 100644 --- a/services/torrent/src/services/IpcInterface.ts +++ b/services/torrent/src/services/IpcInterface.ts @@ -38,10 +38,11 @@ export default class IpcInterface extends IpcServerService * Install the the event handlers */ 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)); } @@ -50,8 +51,7 @@ export default class IpcInterface extends IpcServerService /** * 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; if (typeof payload.torrent == "string") { 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({ - name: torrent.name, + name : torrent.name, infoHash: torrent.infoHash, 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[]; if (torrentIds.length == 0) { torrents = this.torrentClient.torrents; } else { torrents = torrentIds.map(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 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 -------------------------------------------------------------- /**