webtorrent / create-torrent

Create .torrent files
https://webtorrent.io
MIT License
341 stars 93 forks source link

Filename is overridden by the torrent name when seeding a single file #260

Closed LostBeard closed 4 months ago

LostBeard commented 4 months ago

Posted in the webtorrent repo issues as webtorrent/webtorrent#2808

What version of this package are you using? WebTorrent 2.3.6

What operating system, Node.js, and npm version? Chrome 64 bit

What happened? When seeding a torrent with 1 file, the file's name in the torrent is set to the name of the torrent.

I am using WebTorrent below, but internally it is calling createTorrent

const client = new WebTorrent();
let file = new File(['sometext'], 'filename.txt');
client.seed([ file ], { name: 'MyTorrent' }, (torrent) => {
  // below prints 'filename.txt' as expected
  console.log(file.name);
  // the below should print 'filename.txt'
  // but instead prints 'MyTorrent'
  console.log(torrent.files[0].name);
});

What did you expect to happen? I expected the file to keep its own name and not take on the name of the torrent.

Are you willing to submit a pull request to fix this bug? If I can find the bug, yes.

Maybe this is expected behavior for single file torrents?

LostBeard commented 4 months ago

The file names work as expected with 2 or more files.

SilentBot1 commented 4 months ago

Hi @LostBeard

The issue you're seeing appears to be the intended behaviour of create-torrent, with the logic defined here: https://github.com/webtorrent/create-torrent/blob/510d917abbc3b61818427bd9911c3c86a9251ef0/index.js#L83

When one file is seeded, the code above will change the file's name to opts.name if a name isn't passed through with the file.

If this is not desirable, let me know, and we can possibly look at implementing an option to override/ignore this.

LostBeard commented 4 months ago

When one file is seeded, the code above will change the file's name to opts.name if a name isn't passed through with the file.

A File object has a name.

LostBeard commented 4 months ago

I can understand for all of the other inputs that can be passed in.

LostBeard commented 4 months ago

As you can see in my example, the name from the torrent is being used even though File has a name property. The code you are showing seems to show that the name property from File should be used, but that is not what is happening.

SilentBot1 commented 4 months ago

The above logic should respect the input object's name property if one exists. I'll look into this and see what's causing it to get mangled.

LostBeard commented 4 months ago

Thank you.

SilentBot1 commented 4 months ago

After looking into this further, the reason why this occurs is that:

This is the documented and expected behaviour for a .torrent when operating in single file mode and multiple file mode as documented in BEP 3:

In the single file case, the name key is the name of a file, in the multiple file case, it's the name of a directory.

Sources: https://www.bittorrent.org/beps/bep_0003.html https://wiki.theory.org/BitTorrentSpecification#Info_in_Single_File_Mode

LostBeard commented 4 months ago

It is expected behavior. Thank you for the information. I have been looking at the code and noticed the isSingleFileTorrent bool assuming it meant special handling of the torrent.

I suppose if I want the single file's name kept I will have to have 2+ files or just name the torrent the file's name. Makes sense though.

Thank you again. 👍