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

Bug: when a MP4 file is appendTo() the DOM, callback of getBlob() isn't invoked after the file is downloaded #1543

Open
joshuajazleung opened this issue Nov 20, 2018 · 3 comments

Comments

@joshuajazleung
Copy link

@joshuajazleung joshuajazleung commented Nov 20, 2018

The issue is similar to what I made #1542 a few days ago. But now the problem is related to getBlob() instead of getBlobURL()

     this.client.add(this.torrentId, torrent => {
                this.filename = path.basename(torrent.name, path.extname(torrent.name)) + '.zip';
                let zip = new JSZip();
                this.addedFiles = true;
                let hasVideoPlayed = false;

                torrent.files.forEach(file => {
                    this.files.push(file);

                    file.getBlobURL(function(err, url) {
                        if (err) {
                            console.log('error:' + err);
                        }

                        file.url = url;
                        console.log('url: ' + url);
                    });

                    if (file.name.endsWith('.mp4') && !hasVideoPlayed) {
                        file.appendTo(
                            '#videoList',
                            {
                                muted: true,
                                maxBlobLength: 2 * 1000 * 1000 * 1000 // 2 GB
                            },
                            function(err) {
                                if (err) {
                                    console.log(err);
                                }
                            }
                        );
                        hasVideoPlayed = true;
                    }

                    file.getBlob((err, blob) => {
                        downloadedFiles += 1;
                        console.log(downloadedFiles);
                        if (err) {
                            console.log(err);
                            return;
                        }

                        // add file to zip
                        zip.file(file.path, blob);

                        // start the download when all files have been added
                        if (downloadedFiles === torrent.files.length) {
                            if (torrent.files.length > 1) {
                                // generate the zip relative to the torrent folder
                                zip = zip.folder(torrent.name);
                            }
                            zip.generateAsync({ type: 'blob' }).then(blob => {
                                this.downloadURLs.direct = URL.createObjectURL(blob);
                                setTimeout(() => {
                                    URL.revokeObjectURL(this.downloadURLs.direct);
                                    this.downloadURLs.direct = '';
                                }, 30 * 1000);
                            }, console.error);
                        }
                    });
                });
            });

When the if (file.name.endsWith('.mp4') && !hasVideoPlayed) {} block is present and files in MP4 format, callback in file.getBlob() isn't invoked.

If I remove the code block, everything works as expected. Therefore, for some reason, getBlob() and getBlobURL() don't work well together with appendTo().

@joshuajazleung joshuajazleung changed the title Bug: when a MP4 file is appendTo() the DOM, getBlob() isn't invoked after the file is downloaded Bug: when a MP4 file is appendTo() the DOM, callback of getBlob() isn't invoked after the file is downloaded Nov 20, 2018
@joshuajazleung

This comment has been minimized.

Copy link
Author

@joshuajazleung joshuajazleung commented Nov 21, 2018

I found a solution that works.

You will need to getBlob() and getBlobURL() inside torrent.on('done').

Before that, I simply looped through all the files and ran getBlob() and getBlobURL() once the torrent ID was added, hoping the callback would be called when files were finished downloading. Turns out, you will need to run the methods inside torrent.on('done').

I don't know if it's a bug or not, but as long as it's working now :)

@stale

This comment has been minimized.

Copy link

@stale stale bot commented Feb 20, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale label Feb 20, 2019
@stale stale bot closed this Feb 27, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jun 2, 2019
@feross

This comment has been minimized.

Copy link
Member

@feross feross commented Sep 5, 2019

This sounds like a bug. Re-opening until we can confirm/disconfirm this as a bug.

@webtorrent webtorrent unlocked this conversation Sep 5, 2019
@feross feross added the bug label Sep 5, 2019
@feross feross reopened this Sep 5, 2019
@stale stale bot removed the stale label Sep 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

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