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

Creating a download link to a file within the .torrent #1366

Closed
Nashorn opened this issue Apr 28, 2018 · 6 comments
Closed

Creating a download link to a file within the .torrent #1366

Nashorn opened this issue Apr 28, 2018 · 6 comments

Comments

@Nashorn
Copy link

@Nashorn Nashorn commented Apr 28, 2018

I've been trying a simple demo to show a download link, but the getBlobUrl() seems to never run, can't get my debugger; to hit.

Example posted by another user:
https://gist.github.com/DiegoRBaquero/4235c7283e7ff579cdd093305803e799

This is what I am trying:

window.addEventListener("DOMContentLoaded", function(e) {
                var downloadBtn = document.querySelector("button#download-btn");
                downloadBtn.addEventListener("click", startDownload, false);
        	}, false);



            function startDownload(){
                var torrentId   = document.querySelector("textarea#magnet-uri").value;
                var client = new WebTorrent();
                    client.add(torrentId, onTorrentReady);
            }

            function onTorrentReady (torrent) {
              torrent.files[0].getBlobURL(function(err, url) {
                debugger;
                  if (err) return console.log(err)
                  var a = document.createElement('a')
                  a.target = '_blank'
                  a.download = torrent.files[0].name
                  a.href = url
                  a.textContent = 'Download ' + torrent.files[0].name
                  document.body.appendChild(a)
              })
            }
@Nashorn

This comment has been minimized.

Copy link
Author

@Nashorn Nashorn commented Apr 28, 2018

I've now replaced the hardcoded index check for the forEach just incase I might be missing the file, still not able to get the getBlobURL() to run.

torrent.files.forEach(function (file) { file.getBlobURL(function(err, url) { debugger; if (err) return console.log(err) var a = document.createElement('a') a.target = '_blank' a.download = file.name a.href = url a.textContent = 'Download ' + file.name document.body.appendChild(a) }) })

@Nashorn

This comment has been minimized.

Copy link
Author

@Nashorn Nashorn commented Apr 28, 2018

Here is a live code snippet showing that the download links are never rendered because getBlobURL does not work.

http://embed.plnkr.co/6Jr7qH/

@Nashorn

This comment has been minimized.

Copy link
Author

@Nashorn Nashorn commented Apr 28, 2018

OK more info on this issue. I left the download demo running in my tab in Chrome, and roughly 10 mins later the getBlobURL() fires off and was able to render my download link which did work.

I also opened the seeding tab and my download demo tab on FF, about 10 mins later the getBlobURL fired off and worked.....

Chrome also gave this error:

29webtorrent.js:7 Uncaught DOMException: Failed to construct 'RTCPeerConnection': Cannot create so many PeerConnections
at new r (file:///Users/xxx/Documents/SublimeWorkspace/WebTorrent/webtorrent.js:7:10096)
at r._createPeer (file:///Users/xxx/Documents/SublimeWorkspace/WebTorrent/webtorrent.js:3:1086)
at r._onAnnounceResponse (file:///Users/xxx/Documents/SublimeWorkspace/WebTorrent/webtorrent.js:2:30590)
at r._onSocketData (file:///Users/xxx/Documents/SublimeWorkspace/WebTorrent/webtorrent.js:2:29620)
at r.e._onSocketDataBound (file:///Users/xxx/Documents/SublimeWorkspace/WebTorrent/webtorrent.js:2:28859)
at r.emit (file:///Users/xxx/Documents/SublimeWorkspace/WebTorrent/webtorrent.js:4:7236)
at s (file:///Users/xxx/Documents/SublimeWorkspace/WebTorrent/webtorrent.js:6:5613)
at r.i.push (file:///Users/xxx/Documents/SublimeWorkspace/WebTorrent/webtorrent.js:6:9364)
at r._onMessage (file:///Users/xxx/Documents/SublimeWorkspace/WebTorrent/webtorrent.js:7:24456)
at WebSocket.t._ws.onmessage (file:///Users/xxx/Documents/SublimeWorkspace/WebTorrent/webtorrent.js:7:22598)

@alxhotel

This comment has been minimized.

Copy link
Member

@alxhotel alxhotel commented Apr 28, 2018

OK more info on this issue. I left the download demo running in my tab in Chrome, and roughly 10 mins later the getBlobURL() fires off and was able to render my download link which did work.

The callback of getBlobURL() is called when the file is fully downloaded. That is why you need to wait a while to get the blob URL.

Chrome also gave this error:
webtorrent.js:7 Uncaught DOMException: Failed to construct 'RTCPeerConnection': Cannot create so many PeerConnections
...

From what I see in your code snippet you are using an old version of Webtorrent (0.27.3). Try using the latest one (0.99.3):

https://cdn.jsdelivr.net/npm/webtorrent@latest/webtorrent.min.js

@Nashorn

This comment has been minimized.

Copy link
Author

@Nashorn Nashorn commented Apr 28, 2018

I'll get latest. What's a way to generate a download link will waiting for the full file to be downloaded?

@feross

This comment has been minimized.

Copy link
Member

@feross feross commented Apr 30, 2018

@Nashorn You can show a download link on your site however you want to. This is not really a WebTorrent concern.

WebTorrent can't generate the Blob URL until the data is fully downloaded. This is just how Blobs work in the browser. You need all the data available at the time you create the Blob.

So, I would show some UI that let's the user know the file is still downloading before you show the link. Or, you can show a link and let them click it, and then show the download progress until the callback is called.

You can check the progress by checking file.progress (from 0 to 1)

@feross feross closed this Apr 30, 2018
@lock lock bot locked as resolved and limited conversation to collaborators Jul 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.