From 9832edb910b1f1ed33df6a1447eff43c7e6a812e Mon Sep 17 00:00:00 2001 From: David Ludwig Date: Mon, 19 Apr 2021 22:07:28 -0500 Subject: [PATCH] Correct typings. Add logging. Remove unused ENV variables --- .env.example | 4 ---- src/services/IpcInterface.ts | 6 ++++-- src/services/TorrentClient.ts | 6 ++++-- src/typings/webtorrent-hybrid/index.d.ts | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index d97facc..011cb1e 100644 --- a/.env.example +++ b/.env.example @@ -5,8 +5,4 @@ DB_USER = root DB_PASSWORD_FILE = /run/secrets/mysql_root_password DB_DATABASE = autoplex_torrent -DEFAULT_STORAGE_PATH = ./.data - -REST_PORT = 3000 - IPC_SOCKET_PATH = /tmp/torrent_client.sock diff --git a/src/services/IpcInterface.ts b/src/services/IpcInterface.ts index 909dc28..12a8b57 100644 --- a/src/services/IpcInterface.ts +++ b/src/services/IpcInterface.ts @@ -78,7 +78,7 @@ export default class IpcInterface /** * Add a torrent to the client */ - protected async addTorrent(torrentInfo: IAddTorrent) { + protected async addTorrent(torrentInfo: IAddTorrent, downloadPath?: string) { let torrent: WebTorrent.Torrent; if (typeof torrentInfo == "string") { torrent = await this.torrentClient.add(torrentInfo); @@ -93,7 +93,9 @@ export default class IpcInterface * @param message Add */ protected async removeTorrent(torrentId: string) { - await this.torrentClient.remove(torrentId); + try { + await this.torrentClient.remove(torrentId); + } catch(e) {} } protected async listTorrents() { diff --git a/src/services/TorrentClient.ts b/src/services/TorrentClient.ts index 5aea1a4..31fee8a 100644 --- a/src/services/TorrentClient.ts +++ b/src/services/TorrentClient.ts @@ -126,6 +126,7 @@ export default class TorrentClient protected addTorrent(torrent: MagnetUri.Instance, downloadPath: string, selectOnly?: number[]) { // Select only: Select no files by default due to broken selection mechanism in Webtorrent torrent.so = selectOnly ?? []; + console.log("Torrent added:", torrent.infoHash); return this.__webtorrent.add(torrent, { path: downloadPath }); } @@ -134,6 +135,7 @@ export default class TorrentClient */ protected removeTorrent(torrent: WebTorrent.Torrent) { this.deleteTorrent(torrent); + // console.log("Torrent removed:", torrent.infoHash); this.__webtorrent.remove(torrent); } @@ -169,7 +171,7 @@ export default class TorrentClient assert(this.__webtorrent.get(torrentInfo.infoHash) === null, "Torrent has already been added"); // Add the torrent to the client - let torrent = this.addTorrent(torrentInfo, options.downloadPath ?? process.env["DEFAULT_STORAGE_PATH"]); + let torrent = this.addTorrent(torrentInfo, options.downloadPath ?? "/storage/default"); // When the metadata has beened fetched, select the files to download and store the torrent torrent.once("metadata", () => { @@ -192,7 +194,7 @@ export default class TorrentClient // Get the torrent and ensure it exists it exists let torrent = this.__webtorrent.get(torrentInfo.infoHash); - assert(torrent !== undefined, "Torrent has not been added"); + assert(torrent !== null, "Torrent has not been added"); // Remove the torrent this.removeTorrent(torrent); diff --git a/src/typings/webtorrent-hybrid/index.d.ts b/src/typings/webtorrent-hybrid/index.d.ts index bd31a07..208156f 100644 --- a/src/typings/webtorrent-hybrid/index.d.ts +++ b/src/typings/webtorrent-hybrid/index.d.ts @@ -82,7 +82,7 @@ declare module "webtorrent-hybrid" { readonly torrents: Torrent[]; - get(torrentId: Torrent | string | Buffer): Torrent | void; + get(torrentId: Torrent | string | Buffer): Torrent | null; readonly downloadSpeed: number;