-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
file.deselect supposedly not working #164
Comments
I also didn't see an option to download torrent metadata only as well as a ways to decide which files/parts should (not) be downloaded. |
@feross The problem is in the conditional in If you put a debug line inside the conditional and then attempt to download only particular files in a torrent via deselecting all of the files which you do not want, you will never see it -- because the conditional is never met! The issue appears to be that the conditional is trying to match the piece range of the file you wish to deselect, against the stored piece range. However, it will never find a match, as the only stored range is the piece range of the torrent in its entirety. A way to demonstrate how this is broken (and a hacky fix to get the effective functionality of You also might want to change the readme section for I did notice that unfortunately, you will get "false" copies of the files on either side of the piece range you selected, due to the nature of files not beginning or ending exactly where pieces do. This should probably be opened as a separate bug unless you can easily add in code for cleanup of those / prevent the creation of those false-files during this bugfix. Credit goes to AlliedEnvy for figuring this out. |
Yeah, the You have to deselect the whole torrent first, then select individual files. Here's how we made it work in WebTorrent Desktop:
@feross we should probably improve this API before WebTorrent 1.0, because afterward we don't want to change it. Is there a reason to let people deselect individual blocks within a file? What if the API only allowed selecting which files in a torrent to download? Here's an API proposalRemove the Let users specify which files they want when adding a torrent: client.add('magnet://...', { fileSelections: [true, true, false, true] }) When you do that, the files that aren't selected are never created on disk. Let users specify which files they want after adding a torrent: torrent.setFileSelections([true, true, false, true]) Doing that doesn't delete any files. Partially downloaded files just stay there, unless the user deletes them separately. This lets you pause and resume individual files in a torrent. This alternate API would require the library to do a bit more work to keep track of pieces overlapping a file boundary. It's simpler, though. You always declare what you want to download for the whole torrent, instead of calling |
Or maybe: torrent.setFileSelections([12,23]); So if a torrent have big number of files, you don't have to do [false, false, false, false, false, false, ... , true]; torrent.setFileDeselections([4,6,9]); |
I like the idea that @kocoten1992 has, which is how the transmission rpc is also. You define an array of wanted/unwanted file indices. Having this option when adding torrents would be amazing! |
Some news? |
I started implementing @dcposch proposal, in the fashion described by @kocoten1992, with a list of wanted files. |
@dcposch - Yes there could be. Given that torrent runs in a browser with limited storage, it might be useful to distribute storage of a single large file, eg a hi-def video, across a swarm. Therefore nodes in a swarm only download and maintain a smaller set of blocks within a file rather than the whole, providing them as needed. |
I can confirm in v0.108 that torrent.deselect(0, torrent.pieces.length - 1, false) doesn't work. And I remember this line working time ago. Is there any alternative to this? any workaround? |
To support this idea with more info, in the line of @Wingman4l7 the problem is the selection. The callback we get on .add is already late to execute deselect, this is the code:
the state of wt.add(..., function(){}) it's "done", so it's late to mark them with deselect.
The following code, doesn't help either:
This might be a pretty easy fix @feross , and will help us to keep all-in-one package the content. Any progress with this? or any tip/tick to solve this situation? |
Doing tests and tests and tests, I can confirm this seems to work:
|
Fixed by this: const file = torrent.files[fileIndex];
// Deselect all files on initial download
torrent.files.forEach(file => file.deselect());
torrent.deselect(0, torrent.pieces.length - 1, false);
// Select file with provided index
if (file) torrent.select(file._startPiece, file._endPiece, false); |
Thanks @PavelShar I just tried that with a full example https://codepen.io/Jolg42/pen/qBOeYej?editors=1010 const client = new WebTorrent()
const torrentId = 'https://webtorrent.io/torrents/wired-cd.torrent'
client.add(torrentId, function (torrent) {
torrent.on('done', function(){
console.log('torrent finished downloading')
})
torrent.on('download', function (bytes) {
console.log('total downloaded: ' + torrent.downloaded)
console.log('progress: ' + torrent.progress)
})
console.log('Torrent name:', torrent.name)
console.log('Files:')
torrent.files.forEach(file => {
console.log('- ' + file.name)
})
// Deselect all files on initial download
torrent.files.forEach(file => file.deselect());
torrent.deselect(0, torrent.pieces.length - 1, false);
// Torrents can contain many files. Let's use the .mp4 file
const file = torrent.files.find(function (file) {
console.log(`We will only download and play ${file.name}`)
file.select()
return file.name.endsWith('.mp3')
})
// Display the file by adding it to the DOM. Supports video, audio, image, etc. files
file.appendTo('body', {autoplay: true, muted: true})
}) |
While I combed through the source, I found a block which could enable selection of part of the torrent. // https://github.com/webtorrent/webtorrent/blob/7aee819796c540df0b247fec1853098f9a591d4c/lib/torrent.js#L482
if (this.so) {
// this block is never executed
const selectOnlyFiles = parseRange(this.so)
this.files.forEach((v, i) => {
if (selectOnlyFiles.includes(i)) this.files[i].select(true)
})
} I tried many things and I found kind of a workaround, maybe. const meta = parseTorrent(magnet);
meta.so = '-1'; // to deselect all files
client.add(meta, onTorrent); NoteThis thing, works fine. But before all files are deselected with |
Regarding #164 (comment) Setting My code looks something like client.add({
infoHash,
so: '-1'
}, opts, onTorrent); |
@RangerMauve Unfortunately you aren't going to be able to completely prevent the files themselves from being created. The real problem is that Webtorrent actually has no concept of selecting a file. Instead, you are only allowed to select the pieces of the torrent which you wish to download. Since a piece can overlap multiple files (a piece may contain the end of one file and the beginning of the next), Webtorrent will end up writing the bytes to their corresponding files, which will likely result in creating files that are directly adjacent to the selected files. So the result is that you may get some extra files created that you didn't want, but the size of them should be fairly negligible as they shouldn't really be more than a few kb in size (256kb in the worst case if I'm correct, but I could be wrong). Your best bet is to maintain the selected files yourself (you'll have to anyway to figure out which are selected), and just throw the other files away when you're done with them. If you plan on seeding the torrent, you'll want to keep them around as they have parts of the pieces. I also want to note that I had found issues with using |
Are there any developments on this? I desperately need this to work and all of the solutions provided above do not work. The reason deselecting is so useful is because some torrents might include bloat or additional packages. For example, I'm trying to host a server and you can shave down the file size significantly from 12GB -> 2GB by removing/deselecting certain things because the stuff you can remove is additional content. As a result, this would be very useful to have as it would help me dramatically decrease DL times. This is what I have, and I have added a print after the file selection (removed here). It does only print the ones I want but downloads other files, the ones which I have ignored. Additionally, it fully downloads supposed deselected files. const torrent = TorrentClient.add(TorrentURL, {
path: Path,
}, function (torrent) {
// Make sure is server - for ignoring
if (Server == false) return
// Deselect everything
torrent.files.forEach(file => file.deselect())
torrent.deselect(0, torrent.pieces.length - 1, <any>false)
//
for (const file of torrent.files) {
// Check path
const ParentPath = path.basename(path.dirname(file.path))
// Removing files
const EnglishFileCheck = ParentPath == "english" && KeepEnglish.includes(file.name) == false
const AllFileCheck = ParentPath == "all" && KeepAll.includes(file.name) == false
const FolderCheck = RemoveFolders.includes(ParentPath)
if (AllFileCheck || EnglishFileCheck || FolderCheck) {
file.deselect()
continue
}
// File is wanted, add to selections
file.select()
}
}) |
so its been over 8 years since this issue was reported and still no proper solution was implemented?! 🤣 |
Unfortunately. I've found that even if you deselect, it still downloads 10% of the deselected files. |
well actually that doesn't sound so bad... even other clients such as qBittorrent does that |
@feross I'm currently trying to fix this issue, by introducing a discrete interval list data type to store the selections. this could help us fix the issue with I do have a question: { from: 100, to: 150, priority: 10 } Now we want to insert a new selection, that looks like this: { from: 125, to: 175, priority: 5 } We want to keep our selections non-overlapping, so we will have to split one off. But which approach do you consider "correct" for webtorrent? [
{ from: 100, to: 124, priority: 10 }
{ from: 125, to: 175, priority: 5 } // newest insertion is saved as it is, old selection is modified accordingly
] B) [
{ from: 100, to: 150, priority: 10 }
{ from: 151, to: 175, priority: 5 } // newest insertion is modified, since part of it overlapped with existing selection
] To me option A) looks like the expected one, since the latest insert overrides the existing data, but maybe it's not the best fit for webtorrent(?). Idk, please let me know what you think. |
Just to give my two cents your comment above @detarkende, I would say A and B are both valid depending on the scenario. Given a torrent which has the piece layout:
Calling Calling Say we had the further piece layout:
Calling Both I believe Alex's changes are an attempt to allow overlapping ranges and priorities, so that multiple selections can include the same piece, and the algorithm used will flatten this to the linear ranges based on ranges and priorities required, e.g. In the second example above, calling [{ from:1, to: 1, priority: 0 }, { from: 2, to: 3, priority: 1 }] But calling [{ from:1, to: 1, priority: 0 }, { from: 2, to: 3, priority: 0 }]
// or
[{ from:1, to: 2, priority: 0 }, { from: 3, to: 3, priority: 0 }]
// Note: It's probably easier to continue from the existing selection as to not have to update + insert. And when deselecting content, all individual ranges are evaluated, so calling // With priorities
[{ from:1, to: 1, priority: 0 }, { from: 2, to: 3, priority: 1 }]
// to
[{ from: 2, to: 3, priority: 1 }]
// Without priorities
[{ from:1, to: 2, priority: 0 }, { from: 3, to: 3, priority: 0 }]
// to
[{ from: 2, to: 3, priority: 0 }] and calling // With priorities
[{ from:1, to: 2, priority: 0 }, { from: 3, to: 3, priority: 1 }]
// to
[{ from:1, to: 2, priority: 0 }, { from: 3, to: 3, priority: 1 }] // Same
// Without priorities
[{ from:1, to: 2, priority: 0 }, { from: 3, to: 3, priority: 0 }]
// to
[{ from:1, to: 2, priority: 0 }] Note: The lack of changes in If that all makes sense? |
Heard from someone on IRC that after calling
file.deselect
on a file, it still gets fully downloaded.The text was updated successfully, but these errors were encountered: