From 88ce7aff3c6c43599d1cd2c26d6b15362cc1e94a Mon Sep 17 00:00:00 2001 From: John Hiesey Date: Wed, 24 Mar 2021 18:09:35 -0700 Subject: Add opts.destroyStoreOnDestroy to torrent --- docs/api.md | 4 +++- lib/torrent.js | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/docs/api.md b/docs/api.md index 23a467b..db7441e 100644 --- a/docs/api.md +++ b/docs/api.md @@ -93,6 +93,7 @@ If `opts` is specified, then the default options (shown below) will be overridde path: String, // Folder to download files to (default=`/tmp/webtorrent/`) private: Boolean, // If true, client will not share the hash with the DHT nor with PEX (default is the privacy of the parsed torrent) store: Function // Custom chunk store (must follow [abstract-chunk-store](https://www.npmjs.com/package/abstract-chunk-store) API) + destroyStoreOnDestroy: Boolean // If truthy, client will delete the torrent's chunk store (e.g. files on disk) when the torrent is destroyed } ``` @@ -171,7 +172,8 @@ Always listen for the 'error' event. Remove a torrent from the client. Destroy all connections to peers and delete all saved file metadata. -If `opts.destroyStore` is truthy, `store.destroy()` will be called, which will delete the torrent's files from the disk. +If `opts.destroyStore` is specified, it will override `opts.destroyStoreOnDestroy` passed when the torrent was added. +If truthy, `store.destroy()` will be called, which will delete the torrent's files from the disk. If `callback` is provided, it will be called when the torrent is fully destroyed, i.e. all open sockets are closed, and the storage is either closed or destroyed. diff --git a/lib/torrent.js b/lib/torrent.js index db3bebe..ca58842 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -74,6 +74,7 @@ class Torrent extends EventEmitter { this.skipVerify = !!opts.skipVerify this._store = opts.store || FSChunkStore this._preloadedStore = opts.preloadedStore || null + this._destroyStoreOnDestroy = opts.destroyStoreOnDestroy || false this._getAnnounceOpts = opts.getAnnounceOpts // if defined, `opts.private` overrides default privacy of torrent @@ -701,8 +702,12 @@ class Torrent extends EventEmitter { } if (this.store) { + let destroyStore = this._destroyStoreOnDestroy + if (opts && opts.destroyStore !== undefined) { + destroyStore = opts.destroyStore + } tasks.push(cb => { - if (opts && opts.destroyStore) { + if (destroyStore) { this.store.destroy(cb) } else { this.store.close(cb) -- cgit v1.2.3