Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add local file deletion on `torrent.destroy` #1102

Open
wants to merge 4 commits into
base: master
from
Open
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Create a `torrent.delete` method to erase data

  • Loading branch information
Rowern committed Apr 13, 2017
commit da6763cc1c08cdde9efb227b93475fa996f6adae
@@ -167,12 +167,12 @@ Always listen for the 'error' event.
Remove a torrent from the client. Destroy all connections to peers and delete all saved
file data. If `callback` is specified, it will be called when file data is removed.

*Note: This method does not currently delete torrent data (in e.g. `/tmp/webtorrent/...`, see the `path` option to `client.add`). Until this is fixed, please implement it yourself (consider using the `rimraf` npm package).
## `client.destroy([function callback (err) {}])`

Destroy the client, including all torrents and connections to peers. If `callback` is specified, it will be called when the client has gracefully closed.

*Note: Destroying the client does not result in the torrent data suppression. You need to explicitly call `client.remove` or implement it yourself.
## `client.torrents[...]`
An array of all torrents in the client.
@@ -268,6 +268,14 @@ Alias for `client.remove(torrent)`. If `callback` is provided, it will be called
the torrent is fully destroyed, i.e. all open sockets are closed, and the storage is
closed.

## `torrent.delete([callback])`

Does the same as `torrent.destroy([callback])`. If `callback` is provided, it will be
called when the torrent is fully destroyed, i.e. all open sockets are closed, the
storage is closed and *torrent data are deleted*.

*Note: Torrent data are only deleted in node applications.
## `torrent.addPeer(peer)`
Add a peer to the torrent swarm. This is advanced functionality. Normally, you should not
@@ -389,7 +389,7 @@ WebTorrent.prototype._remove = function (torrentId, cb) {
var torrent = this.get(torrentId)
if (!torrent) return
this.torrents.splice(this.torrents.indexOf(torrent), 1)
torrent.destroy(cb)
torrent.delete(cb)
}

WebTorrent.prototype.address = function () {
@@ -638,6 +638,28 @@ Torrent.prototype._onStore = function () {
self._updateSelections()
}

Torrent.prototype.delete = function (cb) {
var self = this
self._delete(cb)
}

Torrent.prototype._delete = function (cb) {
var self = this
self.destroy(function (err) {
if (err) return cb(err)
// We won't delete torrent created from local file
// We also can't delete file created on a browser
if (typeof window === 'undefined' && !self._fromLocalFile && self.path) {
rimraf(self.path, function (err) {
if (!cb) return
cb(err)
})
} else {
cb()
}
})
}

Torrent.prototype.destroy = function (cb) {
var self = this
self._destroy(null, cb)
@@ -687,14 +709,6 @@ Torrent.prototype._destroy = function (err, cb) {
})
}

// We won't delete torrent created from local file
// We also can't delete file created on a browser
if (typeof window === 'undefined' && !self._fromLocalFile && self.path) {
tasks.push(function (cb) {
rimraf(self.path, cb)
})
}

parallel(tasks, cb)

if (err) {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.